Skip to content

Commit

Permalink
Merge branch 'release/0.0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
richardkoszegi committed Jul 8, 2020
2 parents d76c7af + cfd3618 commit 7504f87
Show file tree
Hide file tree
Showing 7 changed files with 749 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.iml
.idea
target/
41 changes: 40 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,41 @@
# shp2pbf
Shapefile -> OSM/PBF Converter

It converts `.shp` formatted files to `.osm.pbf` format. It supports only `MultiLineString` geometry.

### Parameters
* inputFile: Contains the input `.shp` file.
* outputFile: Contains the output file in `.osm.pbf` format. If the file exists, the tool overrides it.
* charset (optional): Contains the shapefile charset encoding, default is `UTF-8`.

### Configuration
It can be run from command line (after Maven build):
```
shp2pbf --inputFile /path/to/input.shp --outputFile /path/to/output.osm.pbf
```

### TagTransform after run

The created tags could be in wrong format, so they have to be converted. The best way to do this is with `osmosis`.

Example run:
```
osmosis --read-pbf file=output.osm.pbf --tag-transform file=TRANSFORM.xml --write-pbf file=transformed.osm.pbf
```

Transform xml example data ([TagTransform documentation](https://wiki.openstreetmap.org/wiki/Osmosis/TagTransform#Running_a_transform)):
```xml
<?xml version="1.0"?>
<translations>
<translation>
<name>Name transform</name>
<description>Maps tags with old_name key to name key in ways</description>
<match type="way">
<tag k="old_name" match_id="n" v=".*"/>
</match>
<output>
<copy-unmatched/>
<tag from_match="n" k="name" v="{0}"/>
</output>
</translation>
</translations>
```
204 changes: 204 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.realcity</groupId>
<artifactId>shp2pbf</artifactId>
<version>0.0.1</version>

<properties>
<geotools.version>21.2</geotools.version>

<!-- Other properties -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>11</release>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>io.realcity.shp2pbf.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.skife.maven</groupId>
<artifactId>really-executable-jar-maven-plugin</artifactId>
<version>1.4.1</version>

<configuration>
<flags>-Xmx4G</flags>
<programFile>shp2pbf</programFile>
</configuration>

<executions>
<execution>
<phase>package</phase>
<goals>
<goal>really-executable-jar</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>

<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>git-property-file</id>
<goals>
<goal>revision</goal>
</goals>
<phase>validate</phase>
<configuration>
<verbose>false</verbose>
<skipPoms>true</skipPoms>
<dateFormat>yyyy-MM-dd'T'HH:mm:ssZ</dateFormat>
<excludeProperties>true</excludeProperties>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
</configuration>
</execution>
<execution>
<id>git-property-values</id>
<goals>
<goal>revision</goal>
</goals>
<phase>validate</phase>
<configuration>
<verbose>false</verbose>
<skipPoms>true</skipPoms>
<dateFormat>yyyy-MM-dd'T'HH:mm:ssZ</dateFormat>
<generateGitPropertiesFile>false</generateGitPropertiesFile>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>external.atlassian.jgitflow</groupId>
<artifactId>jgitflow-maven-plugin</artifactId>
<version>1.0-m5.1</version>
<configuration>
<flowInitContext>
<versionTagPrefix>v</versionTagPrefix>
</flowInitContext>
<noDeploy>true</noDeploy>
<allowUntracked>true</allowUntracked>
<autoVersionSubmodules>true</autoVersionSubmodules>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<!-- Command line parameter parsing -->
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>1.78</version>
</dependency>

<dependency>
<groupId>org.openstreetmap.osmosis</groupId>
<artifactId>osmosis-pbf</artifactId>
<version>0.48.0</version>
</dependency>

<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>${geotools.version}</version>
</dependency>

<!-- provides EPSG database for projections (shapefile loading) -->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-epsg-hsql</artifactId>
<version>${geotools.version}</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.13</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>2.23.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<repositories>
<!-- Hack to force maven to check central first. Maven central is inherited from the superpom,
but ends up at the end of the list. Though most of the time the artifact is in central,
Maven tries to download from every other repository and fails before checking central.
Do not change the id from central2 to central, otherwise the entry will be ignored. -->
<repository>
<id>central2</id>
<name>Check central first to avoid a lot of not found warnings</name>
<url>https://repo.maven.apache.org/maven2</url>
</repository>
<repository>
<id>osgeo</id>
<name>Open Source Geospatial Foundation Repository</name>
<url>https://repo.osgeo.org/repository/release/</url>
</repository>
</repositories>


</project>
27 changes: 27 additions & 0 deletions src/main/java/io/realcity/shp2pbf/CommandLineParameters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.realcity.shp2pbf;

import com.beust.jcommander.IStringConverter;
import com.beust.jcommander.Parameter;

import java.io.File;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class CommandLineParameters {

@Parameter(names = {"--charset"}, description = "Character set", converter = CharsetParser.class)
public Charset charset = StandardCharsets.UTF_8;

@Parameter(names = {"--inputFile"}, required = true, description = "Input File.")
public File inputFile;

@Parameter(names = {"--outputFile"}, required = true, description = "Output File.")
public File outputFile;

private static class CharsetParser implements IStringConverter<Charset> {
@Override
public Charset convert(String charsetName) {
return Charset.forName(charsetName);
}
}
}
101 changes: 101 additions & 0 deletions src/main/java/io/realcity/shp2pbf/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package io.realcity.shp2pbf;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.ParameterException;
import lombok.extern.slf4j.Slf4j;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.referencing.CRS;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class Main {


public static void main(String... args) throws IOException {
CommandLineParameters parameters = parseCommandLineParameters(args);

ShpToOsmConverter converter = new ShpToOsmConverter();

File outputFile = parameters.outputFile;
if (outputFile.createNewFile()) {
log.info("File created: " + outputFile.getName());
} else {
log.info("File: " + outputFile.getName() + " will be overwritten");
}

ShapefileDataStore dataStore;
MathTransform transform;
try {
dataStore = getDataStore(parameters.inputFile, parameters.charset);
transform = createMathTransform(dataStore);

} catch (IOException | FactoryException e) {
throw new RuntimeException("Could not generate datastore or transform.", e);
}

converter.processTypeNamesFromDataStore(dataStore, transform);

converter.writeCreatedItemsToPbf(outputFile);

log.info("Finished conversion.");
}

private static CommandLineParameters parseCommandLineParameters(String[] args) {
log.info("Parsing command line arguments");
CommandLineParameters params = new CommandLineParameters();
try {
JCommander jCommander = new JCommander(params, null, args);
if (params.inputFile == null || params.outputFile == null) {
jCommander.usage();
System.exit(1);
}
return params;
} catch (ParameterException pex) {
log.error(pex.getMessage());
System.exit(1);
}

return null;
}

private static ShapefileDataStore getDataStore(File shapeFile, Charset charset) throws IOException {
Map<String, Serializable> connectParameters = new HashMap<>();

connectParameters.put("url", shapeFile.toURI().toURL());
connectParameters.put("create spatial index", false);
connectParameters.put("charset", charset.name());
return (ShapefileDataStore) DataStoreFinder.getDataStore(connectParameters);
}

private static MathTransform createMathTransform(ShapefileDataStore dataStore) throws IOException, FactoryException {
CoordinateReferenceSystem sourceCRS = dataStore.getSchema().getCoordinateReferenceSystem();
CoordinateReferenceSystem targetCRS = buildTargetCRS();

if (sourceCRS == null) {
throw new RuntimeException("Could not determine the shapefile's projection. " +
"More than likely, the .prj file was not included.");
}

log.info("Converting from " + sourceCRS + " to " + targetCRS);
return CRS.findMathTransform(sourceCRS, targetCRS, true);
}

private static CoordinateReferenceSystem buildTargetCRS() {
try {
return CRS.decode("EPSG:4326", true);
} catch (FactoryException e) {
throw new RuntimeException("Could not found CRS.", e);
}
}
}

Loading

0 comments on commit 7504f87

Please sign in to comment.