Skip to content

Instantly share code, notes, and snippets.

@Sawchord
Created June 16, 2017 21:16
Show Gist options
  • Save Sawchord/f1a2e94b707244b82b96d651cca826a0 to your computer and use it in GitHub Desktop.
Save Sawchord/f1a2e94b707244b82b96d651cca826a0 to your computer and use it in GitHub Desktop.
osmimport
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="osmimport" />
</profile>
</annotationProcessing>
<bytecodeTargetLevel>
<module name="osmimport" target="1.8" />
</bytecodeTargetLevel>
</component>
</project>
<component name="libraryTable">
<library name="Maven: jfree:jcommon:1.0.15">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/jfree/jcommon/1.0.15/jcommon-1.0.15.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/jfree/jcommon/1.0.15/jcommon-1.0.15-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/jfree/jcommon/1.0.15/jcommon-1.0.15-sources.jar!/" />
</SOURCES>
</library>
</component>
<component name="libraryTable">
<library name="Maven: jfree:jfreechart:1.0.12">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/jfree/jfreechart/1.0.12/jfreechart-1.0.12.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/jfree/jfreechart/1.0.12/jfreechart-1.0.12-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/jfree/jfreechart/1.0.12/jfreechart-1.0.12-sources.jar!/" />
</SOURCES>
</library>
</component>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ClientPropertiesManager">
<properties class="javax.swing.AbstractButton">
<property name="hideActionText" class="java.lang.Boolean" />
</properties>
<properties class="javax.swing.JComponent">
<property name="html.disable" class="java.lang.Boolean" />
</properties>
<properties class="javax.swing.JEditorPane">
<property name="JEditorPane.w3cLengthUnits" class="java.lang.Boolean" />
<property name="JEditorPane.honorDisplayProperties" class="java.lang.Boolean" />
<property name="charset" class="java.lang.String" />
</properties>
<properties class="javax.swing.JList">
<property name="List.isFileList" class="java.lang.Boolean" />
</properties>
<properties class="javax.swing.JPasswordField">
<property name="JPasswordField.cutCopyAllowed" class="java.lang.Boolean" />
</properties>
<properties class="javax.swing.JSlider">
<property name="Slider.paintThumbArrowShape" class="java.lang.Boolean" />
<property name="JSlider.isFilled" class="java.lang.Boolean" />
</properties>
<properties class="javax.swing.JTable">
<property name="Table.isFileList" class="java.lang.Boolean" />
<property name="JTable.autoStartsEdit" class="java.lang.Boolean" />
<property name="terminateEditOnFocusLost" class="java.lang.Boolean" />
</properties>
<properties class="javax.swing.JToolBar">
<property name="JToolBar.isRollover" class="java.lang.Boolean" />
</properties>
<properties class="javax.swing.JTree">
<property name="JTree.lineStyle" class="java.lang.String" />
</properties>
<properties class="javax.swing.text.JTextComponent">
<property name="caretAspectRatio" class="java.lang.Double" />
<property name="caretWidth" class="java.lang.Integer" />
</properties>
</component>
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/osmimport.iml" filepath="$PROJECT_DIR$/osmimport.iml" />
</modules>
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
TODO:
Export OsmWay and OsmNode into their own classes
Implement Hashmap of Nodes in OsmWay
Implement WayIterator
Implement NodeIterator
Implement SetOfAttributes
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: jfree:jfreechart:1.0.12" level="project" />
<orderEntry type="library" name="Maven: jfree:jcommon:1.0.15" level="project" />
</component>
</module>
<?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>com.dt</groupId>
<artifactId>osm-import</artifactId>
<version>0.0.1</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.0.12</version>
</dependency>
</dependencies>
</project>
import org.jfree.chart.*;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.*;
import org.jfree.chart.plot.*;
import org.jfree.data.category.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
public class OsmDisplay extends ApplicationFrame{
public OsmDisplay(OsmNodeMap map) {
super ("Display");
//ArrayList<DefaultCategoryDataset> data;
XYSeriesCollection dataset = new XYSeriesCollection();
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
int series = 0;
for (Map.Entry<Long, OsmWay> entry: map.getWays().entrySet()) {
XYSeries data = new XYSeries("Blob");
for (int i = 0; i < entry.getValue().getNodes().size(); i++) {
data.add(entry.getValue().getNodes().get(i).getLon(),
entry.getValue().getNodes().get(i).getLat()
);
}
dataset.addSeries(data);
renderer.setSeriesLinesVisible(series, true);
renderer.setSeriesShapesVisible(series, false);
renderer.setSeriesPaint(series, Color.black);
series ++;
}
JFreeChart chart = ChartFactory.createXYLineChart("Map", "Lon", "Lat",
dataset, PlotOrientation.VERTICAL, false, true, true);
chart.setBackgroundPaint(Color.white);
XYPlot plot = chart.getXYPlot();
plot.setRenderer(renderer);
ChartPanel panel = new ChartPanel(chart);
panel.setPreferredSize(new java.awt.Dimension(800,600));
setContentPane(panel);
}
}
import org.jfree.ui.RefineryUtilities;
public class OsmImport {
public static void main (String [] args) {
System.out.println("Hello World");
OsmNodeMap map;
//map = OsmNodeMap.importXml(args[0]);
//System.out.println("Parsed " + map.getWays().size() + " ways");
map = OsmNodeMap.importXml(args[0], x -> {
String hw = x.get("highway");
if (hw == null) return false;
if (hw.equals("motorway")) return true;
if (hw.equals("motorway_link")) return true;
if (hw.equals("primary")) return true;
if (hw.equals("secondary")) return true;
if (hw.equals("tertiary")) return true;
if (hw.equals("residential")) return true;
return false;
}
);
System.out.println("Parsed " + map.getWays().size() + " ways");
OsmDisplay display = new OsmDisplay(map);
RefineryUtilities.centerFrameOnScreen(display);
display.setVisible(true);
}
}
import java.util.*;
public class OsmNode {
private long id;
private double lon;
private double lat;
private Map<String,String> attributes;
OsmNode(long id, double lon, double lat) {
this.id = id;
this.lon = lon;
this.lat = lat;
this.attributes = new HashMap<String, String>();
}
public void updatePos(double lon, double lat) {
this.lon = lon;
this.lat = lat;
}
public long getId() { return this.id; }
public double getLon() { return this.lon; }
public void setLon(double lon) { this.lon = lon; }
public double getLat() { return this.lat; }
public void setLat(double lat) { this.lat = lat; }
public String getAttribute(String key) {
return this.attributes.get(key);
}
public Map<String, String> getAttributes() { return this.attributes; }
public void setAtt(String key, String value) {
this.attributes.put(key, value);
}
}
import java.util.*;
import javax.xml.parsers.*;
import java.io.*;
import java.util.function.Function;
public class OsmNodeMap {
private Map<Long, OsmWay> map;
OsmNodeMap() {
this.map = new HashMap<Long, OsmWay>();
}
public void addWay(OsmWay way) {
this.map.put(way.getId(), way);
}
public OsmWay getWay(long id) {
return this.map.get(id);
}
public Map<Long, OsmWay> getWays() { return this.map; }
public static OsmNodeMap importXml(String path) {
return importXml(path, a -> true);
}
public static OsmNodeMap importXml(String path, Function<Map<String, String>, Boolean> filter) {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
OsmXmlHandler handler = new OsmXmlHandler();
handler.Init(filter);
handler.setPass(0);
parser.parse(path, handler);
handler.setPass(1);
parser.parse(path, handler);
OsmNodeMap m = handler.getMap();
handler.Finalize();
return m;
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (org.xml.sax.SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
import java.util.*;
public class OsmWay {
private long id;
private List<OsmNode> nodes;
private Map<String, String> attribute;
OsmWay(long id) {
this.id = id;
this.nodes = new ArrayList<OsmNode>();
this.attribute = new HashMap<String, String>();
}
public long getId(){ return this.id; }
public List<OsmNode> getNodes() {
return this.nodes;
}
public String getAttribute(String key) {
return this.attribute.get(key);
}
public Map<String, String> getAttrs() { return this.attribute; }
public void setAtt(String key, String value) {
this.attribute.put(key, value);
}
void addNode(OsmNode node) {
this.nodes.add(node);
}
}
import java.util.*;
import java.util.function.Function;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class OsmXmlHandler extends DefaultHandler{
private OsmNodeMap map;
// TODO: Use enum here
// NOTE: 0 NONE 1 NODE 2 WAY
private int state;
private int pass;
private Map<Long, List<OsmWay>> wantedNodes;
private OsmNode currentNode;
private OsmWay currentWay;
private Function<Map<String, String>, Boolean> filter;
public void setPass(int pass) {
this.pass = pass;
}
public void Init(Function<Map<String, String>, Boolean> filter) {
map = new OsmNodeMap();
wantedNodes = new HashMap<Long, List<OsmWay>>();
this.filter = filter;
}
public void Finalize() {
wantedNodes = null;
}
public OsmNodeMap getMap() {
return map;
}
@Override
public void startDocument() throws SAXException {
state = 0;
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
//System.out.println(qName);
switch (qName) {
case "node":
if (pass != 1) return;
if (state != 0) {
throw new SAXException("Corrupt OSM File in Pass 0");
}
long id = Long.parseLong(attributes.getValue("id"));
double lon = Double.parseDouble(attributes.getValue("lon"));
double lat = Double.parseDouble(attributes.getValue("lat"));
currentNode = new OsmNode(id, lon, lat);
state = 1;
return;
case "way":
if (pass != 0) return;
if (state != 0) {
throw new SAXException("Corrupt OSM File in Pass 1");
}
id = Long.parseLong(attributes.getValue("id"));
currentWay = new OsmWay(id);
state = 2;
return;
case "nd":
if (pass != 0) return;
if (state != 2) {
throw new SAXException("Corrupt OSM File in nd Value");
}
long nodeId = Long.parseLong(attributes.getValue("ref"));
currentWay.addNode(new OsmNode(nodeId, Double.NaN, Double.NaN));
// Check if we have seen ways wanting the position of that node
// If not create new list, if yes just append
List<OsmWay> wantedList = wantedNodes.get(nodeId);
if (wantedNodes.get(nodeId) == null) {
wantedList = new ArrayList<OsmWay>();
wantedList.add(currentWay);
wantedNodes.put(nodeId, wantedList);
}
else {
wantedList.add(currentWay);
}
return;
case "tag":
if (!((pass == 0 && state == 2) || (pass == 1 && state == 1))) return;
String key = attributes.getValue("k");
String value = attributes.getValue("v");
if (state == 1) {
currentNode.setAtt(key, value);
}
else {
currentWay.setAtt(key, value);
}
return;
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
switch (qName) {
case "node":
if (pass != 1) return;
if (state != 1) {
throw new SAXException("Currupt OSM in Node end");
}
long nodeId = currentNode.getId();
List<OsmWay> wantedList = wantedNodes.get(nodeId);
if (wantedList == null) {
currentNode = null;
}
else {
double lon = currentNode.getLon();
double lat = currentNode.getLat();
for (OsmWay w : wantedList) {
for (OsmNode n : w.getNodes()) {
if (n.getId() == currentNode.getId()) {
n.setLon(currentNode.getLon());
n.setLat(currentNode.getLat());
}
}
}
}
state = 0;
return;
case "way":
if (pass != 0) return;
if (state != 2) {
throw new SAXException("Currupt OSM in Way end");
}
if (this.filter.apply(currentWay.getAttrs())) {
map.addWay(currentWay);
}
state = 0;
return;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment