Skip to content

Commit

Permalink
added a node to download from geofabrik related #74
Browse files Browse the repository at this point in the history
  • Loading branch information
samthiriot committed Apr 18, 2022
1 parent a979111 commit 0b4ce77
Show file tree
Hide file tree
Showing 13 changed files with 960 additions and 76 deletions.
5 changes: 5 additions & 0 deletions ch.res_ear.samthiriot.knime.shapefilesaswkt/.classpath
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry exported="true" kind="lib" path="lib/gt-geojsondatastore-26.3.jar"/>
<classpathentry exported="true" kind="lib" path="lib/jackson-annotations-2.10.5.jar"/>
<classpathentry exported="true" kind="lib" path="lib/jackson-databind-2.10.5.1.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="module" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
Expand Down Expand Up @@ -167,6 +171,7 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="tests"/>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
Expand Down
13 changes: 11 additions & 2 deletions ch.res_ear.samthiriot.knime.shapefilesaswkt/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,24 @@ Bundle-ClassPath: target/classes/,
lib/txw2-2.4.0-b180830.0438.jar,
lib/unit-api-2.0.jar,
lib/uom-lib-common-2.0.jar,
lib/xml-commons-resolver-1.2.jar
lib/xml-commons-resolver-1.2.jar,
lib/gt-geojson-26.3.jar,
lib/hamcrest-core-1.1.jar,
lib/json-simple-1.1.1.jar,
lib/junit-4.10.jar,
lib/gt-geojsondatastore-26.3.jar,
lib/jackson-annotations-2.10.5.jar,
lib/jackson-databind-2.10.5.1.jar
Bundle-Activator: ch.res_ear.samthiriot.knime.shapefilesaswkt.ShapefileAsWKTNodePlugin
Bundle-Vendor: EIFER (European Institute for Energy Research)
Require-Bundle: org.knime.workbench.core;bundle-version="[3.7.0,5.0.0)",
org.knime.base;bundle-version="[3.7.0,5.0.0)",
org.knime.product;bundle-version="[3.7.0,5.0.0)",
org.knime.workbench;bundle-version="[3.7.0,5.0.0)",
org.knime.testing;bundle-version="[3.7.0,5.0.0)",
org.eclipse.core.resources
org.eclipse.core.resources,
org.junit,
org.junit.jupiter.api
Bundle-ActivationPolicy: lazy
Export-Package: .,
au.com.objectix.jgridshift,
Expand Down
10 changes: 6 additions & 4 deletions ch.res_ear.samthiriot.knime.shapefilesaswkt/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ bin.includes = plugin.xml,\
src/ch/res_ear/samthiriot/knime/shapefilesaswkt/create/pointfrom2d/shapefilesAsWKT.png,\
src/ch/res_ear/samthiriot/knime/shapefilesaswkt/transform/randompoint/RandomPointInShapeNodeFactory.xml,\
src/ch/res_ear/samthiriot/knime/shapefilesaswkt/transform/randompoint/shapefilesAsWKT.png,\
lib/,\
lib/aircompressor-0.20.jar,\
target/classes/
target/classes/,\
lib/
src.includes = icons/,\
pom.xml,\
src/**/*.xml,\
Expand Down Expand Up @@ -218,4 +217,7 @@ jars.extra.classpath = lib/aircompressor-0.20.jar,\
lib/txw2-2.4.0-b180830.0438.jar,\
lib/unit-api-2.0.jar,\
lib/uom-lib-common-2.0.jar,\
lib/xml-commons-resolver-1.2.jar
lib/xml-commons-resolver-1.2.jar,\
lib/gt-geojsondatastore-26.3.jar,\
lib/jackson-annotations-2.10.5.jar,\
lib/jackson-databind-2.10.5.1.jar
7 changes: 7 additions & 0 deletions ch.res_ear.samthiriot.knime.shapefilesaswkt/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@
category-path="/community/spatialAsWKT/readSpatialAsWKT"
factory-class="ch.res_ear.samthiriot.knime.shapefilesaswkt.read.read_from_gml.ReadGMLAsWKTNodeFactory"
id="ch.res_ear.samthiriot.knime.shapefilesAsWKT.readFromGML.ReadGMLAsWKTNodeFactory">
</node>
<node
category-path="/community/spatialAsWKT/readSpatialAsWKT"
deprecated="false"
factory-class="ch.res_ear.samthiriot.knime.shapefilesaswkt.read.read_from_geofabrik.ReadWKTFromGeofabrikNodeFactory"
hidden="false"
id="ch.res_ear.samthiriot.knime.shapefilesaswkt.read.read_from_geofabrik.ReadWKTFromGeofabrikNodeFactory">
</node>
<node category-path="/community/spatialAsWKT/viewSpatialAsWKT" deprecated="false" factory-class="ch.res_ear.samthiriot.knime.shapefilesaswkt.view.DisplaySpatialPopulationNodeFactory" id="ch.res_ear.samthiriot.knime.shapefilesAsWKT.view.DisplaySpatialPopulationNodeFactory">
</node>
Expand Down
10 changes: 8 additions & 2 deletions ch.res_ear.samthiriot.knime.shapefilesaswkt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,13 @@
<artifactId>gt-image</artifactId>
<version>${geotools.version}</version>
</dependency>


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

<dependency>
<groupId>org.geotools.jdbc</groupId>
<artifactId>gt-jdbc-postgis</artifactId>
Expand Down Expand Up @@ -155,7 +161,7 @@
<artifactId>gt-xsd-gml3</artifactId>
<version>${geotools.version}</version>
</dependency>

<!-- for geocoding -->
<dependency>
<groupId>com.google.maps</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package ch.res_ear.samthiriot.knime.shapefilesaswkt.read.read_from_geofabrik;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.io.IOUtils;
import org.geotools.data.FeatureReader;
import org.geotools.data.FileDataStore;
import org.geotools.data.geojson.GeoJSONDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.opengis.feature.simple.SimpleFeature;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class GeofabrikUtils {

public static final String URL_INDEX_GEOFABRIK = "https://download.geofabrik.de/index-v1.json";
//

// https://download.geofabrik.de/index-v1-nogeom.json

public static File readGeofabrikIndexIntoFile() {


try {

// load the file first
URL url = new URL(URL_INDEX_GEOFABRIK);
InputStream inputStream = url.openStream();

File f = new File("/tmp/test.json");
if (f.exists())
return f;

FileWriter fileWriter = new FileWriter(f);

IOUtils.copy(inputStream, fileWriter, StandardCharsets.UTF_8);

return f;

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException("unable to read the file");
}

}

private static Map<String,String> cachedName2Url = null;

public static Map<String,String> fetchListOfDataExtracts() {

if (cachedName2Url != null)
return cachedName2Url;

GeoJSONDataStoreFactory factory = new GeoJSONDataStoreFactory();

FileDataStore store = null;
SimpleFeatureIterator it = null;
try {

/*Map<String,?> params = new HashMap<>();
params.put(GeoJSONDataStoreFactory.URL_PARAM.key, new URL(URL_INDEX_GEOFABRIK));
*/

File f = readGeofabrikIndexIntoFile();

store = factory.createDataStore(f);

SimpleFeatureCollection features = store.getFeatureSource(store.getTypeNames()[0]).getFeatures();

it = features.features();

Map<String,String> name2url = new HashMap<>();

Map<String,String> id2parent = new HashMap<>();
Map<String,String> id2name = new HashMap();

// decode features
while (it.hasNext()) {
SimpleFeature ft = it.next();
String id = ft.getAttribute("id").toString();
String name = ft.getAttribute("name").toString();

id2name.put(id, name);

// find parent (optional)
String parent = (String) ft.getAttribute("parent");
if (parent != null) {
id2parent.put(id, parent);
//System.out.println(id+" has for parent "+parent);
} else {
//System.out.println(id+" has no parent");
}

// find url
ObjectNode urls = (ObjectNode)ft.getAttribute("urls");
JsonNode nodeShp = urls.findValue("shp");
// skip index without shp
if (nodeShp == null)
continue;

String shp = nodeShp.asText();

name2url.put(id, shp);

/*
System.out.println(ft);
System.out.println(id);
System.out.println(name);
System.out.println(shp);
System.out.println();
*/
}

// find parents
Map<String,String> hierarchy2url = new TreeMap<>();
for (String id: name2url.keySet()) {

final String url = name2url.get(id);

String newId = id2name.get(id);

String parent = id2parent.get(id);
while (parent != null) {
newId = id2name.get(parent) +"/" + newId;
id = parent;
parent = id2parent.get(id);
}

hierarchy2url.put(newId, url);
}

cachedName2Url = Collections.unmodifiableMap(hierarchy2url);
return cachedName2Url;

} catch (MalformedURLException e) {
e.printStackTrace();
throw new RuntimeException("The index URL seems corrupted: "+URL_INDEX_GEOFABRIK, e);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("unable to access the list of data from "+URL_INDEX_GEOFABRIK, e);
} finally {
if (it != null)
it.close();

if (store != null)
store.dispose();

}


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*******************************************************************************
* Copyright (c) 2019 EIfER[1] (European Institute for Energy Research).
* This program and the accompanying materials
* are made available under the terms of the GNU GENERAL PUBLIC LICENSE
* which accompanies this distribution, and is available at
* https://www.gnu.org/licenses/gpl-3.0.html
*
* Contributors:
* Samuel Thiriot - original version and contributions
*******************************************************************************/
package ch.res_ear.samthiriot.knime.shapefilesaswkt.read.read_from_geofabrik;

import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;

import org.knime.core.node.defaultnodesettings.DefaultNodeSettingsPane;
import org.knime.core.node.defaultnodesettings.DialogComponentStringSelection;
import org.knime.core.node.defaultnodesettings.SettingsModelString;

/**
* This is an example implementation of the node dialog of the
* "ReadWKTFromDatabase" node.
*
* This node dialog derives from {@link DefaultNodeSettingsPane} which allows
* creation of a simple dialog with standard components. If you need a more
* complex dialog please derive directly from
* {@link org.knime.core.node.NodeDialogPane}. In general, one can create an
* arbitrary complex dialog using Java Swing.
*
* @author Samuel Thiriot
*/
public class ReadWKTFromGeofabrikNodeDialog extends DefaultNodeSettingsPane {


/**
* New dialog pane for configuring the node. The dialog created here
* will show up when double clicking on a node in KNIME Analytics Platform.
*/
protected ReadWKTFromGeofabrikNodeDialog() {
super();

{
SettingsModelString m_nameToLoad = new SettingsModelString("name_to_load", "Europe/Germany/Baden-Württemberg/Regierungsbezirk Karlsruhe");

Collection<String> names = GeofabrikUtils.fetchListOfDataExtracts().keySet();

addDialogComponent(new DialogComponentStringSelection(
m_nameToLoad,
"Zone to load",
names
));
}

{
SettingsModelString m_layerToLoad = new SettingsModelString(
"layer_to_load",
"buildings (Building outlines)");

Collection<String> layers = new LinkedList<>(Arrays.asList(
"buildings (Building outlines)",
"landuse (Forests, residential areas, industrial areas,...)",
"natural (Natural features)",
"places (Cities, towns, suburbs, villages,...)",
"pofw (Places of worship such as churches, mosques, ...)",
"pois (Points of Interest)",
"railways (Railway, subways, light rail, trams, ...)",
"roads (Roads, tracks, paths, ...)",
"traffic",
"transport (Parking lots, petrol (gas) stations, ...)",
"water (Lakes, ...)",
"waterways (Rivers, canals, streams, ...)"
));
addDialogComponent(new DialogComponentStringSelection(
m_layerToLoad,
"layer to load",
layers
));
}
}
}

Loading

0 comments on commit 0b4ce77

Please sign in to comment.