Cities Generator Logo Cities Generator Logo

cities-generator

Generate a file with the cities for a country

Cities Generator Library

Java library for generating descriptor files with cities data. The library can generate JSON files containing hierarchical location data (regions, provinces, municipalities) for various countries.

Features

Command Line Usage

Basic Parameters

Enabling DEBUG logs

To see DEBUG level logs (more detailed information), use the system property -Dorg.slf4j.simpleLogger.defaultLogLevel=debug:

java -Dorg.slf4j.simpleLogger.defaultLogLevel=debug -jar cities-generator-1.2.8.jar -p EXTRA_GEONAMES -c IT

Available log levels: trace, debug, info, warn, error (default: info)

Remote Debugging

To enable remote debugging, use the -agentlib:jdwp parameter:

java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -jar cities-generator-1.2.8.jar -p EXTRA_GEONAMES -c IT -l en

Or with quotes (for zsh compatibility):

java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address='*:5005' -jar cities-generator-1.2.8.jar -p EXTRA_GEONAMES -c IT -l en

Parameters:

Then connect your IDE debugger to localhost:5005.

Example with DEBUG logs and remote debugging:

java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -Dorg.slf4j.simpleLogger.defaultLogLevel=debug -jar cities-generator-1.2.8.jar -p EXTRA_GEONAMES -c IT -l en

Usage Modes

To generate the cities, you can choose between 3 modes:

Mode 1: Command Line (Maven)

By a command line shell digit:

mvn org.apache.maven.plugins:maven-dependency-plugin:3.6.0:copy -Dartifact=it.vige.cities:cities-generator:1.2.8:jar -DoutputDirectory=. && java -jar cities-generator-1.2.8.jar -c GB -l en

It will return a json file inside the ${user.home}/cities-generator/EU/GB/en.json (structure: {continent}/{country}/{language}.json)

Mode 2: Build from Source

Download the source and execute:

cd library
./gradlew build
java -jar build/libs/cities-generator-1.2.8.jar -c IT -l it

Mode 3: Java API

Through API java follow the instructions:

  1. On maven add the following script in the pom.xml file:
<dependency>
    <groupId>it.vige.cities</groupId>
    <artifactId>cities-generator</artifactId>
    <version>1.2.8</version>
</dependency>

or on gradle in the build.gradle file:

   compile('it.vige.cities:cities-generator:1.2.8')
  1. Execute the following java instructions:
import it.vige.cities.Generator;
import it.vige.cities.Countries;
import it.vige.cities.Languages;
import it.vige.cities.result.Nodes;
...
Configuration configuration = new Configuration();
configuration.setCountry(Countries.IT.name());
configuration.setLanguage(Languages.IT); // Using enum (recommended)
// Or use string: configuration.setLanguage("it"); // Optional, defaults to "it"
Generator generator = new Generator(configuration, true);
Nodes result = generator.generate();
System.out.println(result.getZones());

you can also generate a file through the instruction:

...
import it.vige.cities.Result;
...
Result result = generator.generateFile();

You will find the file EU/IT/it.json in the ${user.home}/cities-generator directory (structure: {continent}/{country}/{language}.json)

For detailed API documentation, see the Javadoc.

Publishing to Git

After generating the JSON files, you can automatically publish them to a Git repository using the --git-config parameter. This feature clones the repository, copies the generated JSON file, commits, and pushes the changes.

Basic Usage

# Publish with default repository (https://github.com/flashboss/cities-generator.git)
java -jar cities-generator-1.2.8.jar -c GB -p OPENSTREETMAP --git-config "repo=https://github.com/user/repo.git"

# Publish with custom repository and branch
java -jar cities-generator-1.2.8.jar -c GB -p OPENSTREETMAP --git-config "repo=https://github.com/user/repo.git,branch=main"

# Publish with all options
java -jar cities-generator-1.2.8.jar -c GB -p OPENSTREETMAP --git-config "repo=https://github.com/user/repo.git,branch=main,dir=data,username=myuser,token=ghp_xxxxxxxxxxxxx,message=Update cities data"

Configuration Format

The --git-config parameter accepts a CSV string with key=value pairs:

Authentication

You can provide credentials in two ways:

  1. Via command line:

    java -jar cities-generator-1.2.8.jar -c GB -p OPENSTREETMAP --git-config "repo=https://github.com/user/repo.git,username=myuser,token=ghp_xxx"
    
  2. Via environment variables:

    export GIT_USERNAME=myuser
    export GIT_TOKEN=ghp_xxxxxxxxxxxxx
    java -jar cities-generator-1.2.8.jar -c GB -p OPENSTREETMAP --git-config "repo=https://github.com/user/repo.git"
    

For GitHub, use a Personal Access Token (PAT) as the token. Create one at: https://github.com/settings/tokens

Examples

# Minimal: only repository URL
--git-config "repo=https://github.com/user/repo.git"

# With branch and directory
--git-config "repo=https://github.com/user/repo.git,branch=develop,dir=cities"

# With authentication
--git-config "repo=https://github.com/user/repo.git,username=myuser,token=ghp_xxx"

# Complete example
--git-config "repo=https://github.com/user/repo.git,branch=main,dir=_db,username=myuser,token=ghp_xxx,message=Update cities for GB"

Geonames Registration

If you use GEONAMES or EXTRAGEONAMES, you use a default username. In the long run this default username may be inactive, so you will need a new username to specify in the configuration field seen above. To get the new username you must register through the site: https://www.geonames.org/login

OpenStreetMap Provider

The OPENSTREETMAP provider uses the Overpass API to retrieve administrative boundaries (regions, provinces, and municipalities) from OpenStreetMap data. This provider:

Note: OpenStreetMap queries may take longer than other providers due to the number of API calls required (approximately 1 + number of regions + number of provinces queries per country).

Data Output

Actually samples of generated cities can be found online:

The structure is {continent}/{country}/{language}.json (e.g., EU/IT/it.json, EU/GB/en.json)

Maven Central

This library is available on Maven Central:

<dependency>
    <groupId>it.vige.cities</groupId>
    <artifactId>cities-generator</artifactId>
    <version>1.2.8</version>
</dependency>

See the Maven Central page for more information.