-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added a node to download from geofabrik related #74
- Loading branch information
1 parent
a979111
commit 0b4ce77
Showing
13 changed files
with
960 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 169 additions & 0 deletions
169
.../ch/res_ear/samthiriot/knime/shapefilesaswkt/read/read_from_geofabrik/GeofabrikUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} | ||
|
||
|
||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
...hiriot/knime/shapefilesaswkt/read/read_from_geofabrik/ReadWKTFromGeofabrikNodeDialog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
)); | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.