first .osm format import
- import osm convert it to Drawing and use it as resolved path. - Not ready , first test over lab/valbonne_oct_2020.osm
This commit is contained in:
34
java/org/artisanlogiciel/osm/Node.java
Normal file
34
java/org/artisanlogiciel/osm/Node.java
Normal file
@@ -0,0 +1,34 @@
|
||||
package org.artisanlogiciel.osm;
|
||||
|
||||
/**
|
||||
* https://wiki.openstreetmap.org/wiki/Node
|
||||
*/
|
||||
public class Node {
|
||||
NodeRef ref;
|
||||
double lat;
|
||||
double lon;
|
||||
|
||||
public NodeRef getRef() {
|
||||
return ref;
|
||||
}
|
||||
|
||||
public Node(NodeRef ref, double lat, double lon) {
|
||||
this.ref = ref;
|
||||
ref.setNode(this);
|
||||
this.lat = lat;
|
||||
this.lon = lon;
|
||||
}
|
||||
|
||||
public double getLat() {
|
||||
return lat;
|
||||
}
|
||||
|
||||
public double getLon() {
|
||||
return lon;
|
||||
}
|
||||
|
||||
public String toString()
|
||||
{
|
||||
return "(id=" + ref.id + " lon=" + lon + " lat=" + lat + ")";
|
||||
}
|
||||
}
|
||||
37
java/org/artisanlogiciel/osm/NodeRef.java
Normal file
37
java/org/artisanlogiciel/osm/NodeRef.java
Normal file
@@ -0,0 +1,37 @@
|
||||
package org.artisanlogiciel.osm;
|
||||
|
||||
public class NodeRef {
|
||||
long id;
|
||||
Node node;
|
||||
|
||||
public NodeRef(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void setNode(Node node) {
|
||||
this.node = node;
|
||||
}
|
||||
|
||||
public Node getNode()
|
||||
{
|
||||
return node;
|
||||
}
|
||||
|
||||
public String toString()
|
||||
{
|
||||
return node != null ? node.toString(): "nd:" + id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Long.hashCode(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( o instanceof NodeRef ) {
|
||||
return ((NodeRef) o).id == id;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
109
java/org/artisanlogiciel/osm/OsmReader.java
Normal file
109
java/org/artisanlogiciel/osm/OsmReader.java
Normal file
@@ -0,0 +1,109 @@
|
||||
package org.artisanlogiciel.osm;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import java.io.FileInputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class OsmReader {
|
||||
|
||||
private String sample;
|
||||
|
||||
private HashMap<NodeRef,Node> refs;
|
||||
private List<Way> ways;
|
||||
|
||||
public OsmReader(String sample) {
|
||||
this.sample = sample;
|
||||
refs = new HashMap<>();
|
||||
ways = new ArrayList<>();
|
||||
}
|
||||
|
||||
public List<Way> getWays() {
|
||||
return ways;
|
||||
}
|
||||
|
||||
public void read()
|
||||
{
|
||||
/* TODO get minlat and minlon **/
|
||||
DocumentBuilderFactory factory =
|
||||
DocumentBuilderFactory.newInstance();
|
||||
try {
|
||||
DocumentBuilder builder = factory.newDocumentBuilder();
|
||||
Document doc = builder.parse(new FileInputStream(sample));
|
||||
Element root = doc.getDocumentElement();
|
||||
System.out.println(root);
|
||||
NodeList nList = doc.getElementsByTagName("node");
|
||||
for (int temp = 0; temp < nList.getLength(); temp++) {
|
||||
org.w3c.dom.Node nNode = nList.item(temp);
|
||||
if (nNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
|
||||
Element e = (Element) nNode;
|
||||
long id = Long.valueOf(e.getAttribute("id"));
|
||||
double lat = Double.valueOf(e.getAttribute("lat"));
|
||||
double lon = Double.valueOf(e.getAttribute("lon"));
|
||||
Node node = new Node(new NodeRef(id), lat, lon);
|
||||
refs.put(node.getRef(),node);
|
||||
}
|
||||
}
|
||||
NodeList wList = doc.getElementsByTagName("way");
|
||||
for (int temp = 0; temp < wList.getLength(); temp++) {
|
||||
org.w3c.dom.Node wNode = wList.item(temp);
|
||||
if (wNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
|
||||
Element e = (Element) wNode;
|
||||
try {
|
||||
long id = Long.valueOf(e.getAttribute("id"));
|
||||
List<NodeRef> nodeRefList = new ArrayList<>();
|
||||
NodeList nrList = e.getElementsByTagName("nd");
|
||||
for (int temp2 = 0; temp2 < nrList.getLength(); temp2++) {
|
||||
org.w3c.dom.Node nrNode = nrList.item(temp2);
|
||||
if (nrNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
|
||||
Element nd = (Element) nrNode;
|
||||
long ref = Long.valueOf(nd.getAttribute("ref"));
|
||||
NodeRef nodeRef = new NodeRef(ref);
|
||||
Node node = refs.get(nodeRef);
|
||||
if ( node == null )
|
||||
{
|
||||
System.out.println("unknown node id " + nodeRef);
|
||||
}
|
||||
else {
|
||||
nodeRefList.add(node.getRef());
|
||||
}
|
||||
}
|
||||
}
|
||||
Way way = new Way(id, nodeRefList);
|
||||
ways.add(way);
|
||||
}
|
||||
catch (Exception bad)
|
||||
{
|
||||
System.err.println("Too bad way");
|
||||
bad.printStackTrace(System.err);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(Exception any)
|
||||
{
|
||||
System.err.println("Too bad");
|
||||
any.printStackTrace(System.err);
|
||||
}
|
||||
}
|
||||
|
||||
public void dump()
|
||||
{
|
||||
System.out.println(refs);
|
||||
System.out.println(ways);
|
||||
}
|
||||
public static void main(String pArgs[])
|
||||
{
|
||||
String sample = "/home/plhardy/valbonne3D/valbonne_oct_2020.osm";
|
||||
|
||||
OsmReader osmreader = new OsmReader(sample);
|
||||
osmreader.read();
|
||||
osmreader.dump();
|
||||
}
|
||||
}
|
||||
22
java/org/artisanlogiciel/osm/Way.java
Normal file
22
java/org/artisanlogiciel/osm/Way.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package org.artisanlogiciel.osm;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Way {
|
||||
long id;
|
||||
|
||||
public Way(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Way(long id, List<NodeRef> ndlist) {
|
||||
this.id = id;
|
||||
this.ndlist = ndlist;
|
||||
}
|
||||
|
||||
public List<NodeRef> getNdlist() {
|
||||
return ndlist;
|
||||
}
|
||||
|
||||
List<NodeRef> ndlist;
|
||||
}
|
||||
59
java/org/artisanlogiciel/osm/convert/OsmToDrawing.java
Normal file
59
java/org/artisanlogiciel/osm/convert/OsmToDrawing.java
Normal file
@@ -0,0 +1,59 @@
|
||||
package org.artisanlogiciel.osm.convert;
|
||||
|
||||
import org.artisanlogiciel.graphics.Drawing;
|
||||
import org.artisanlogiciel.graphics.DrawingLine;
|
||||
import org.artisanlogiciel.osm.Node;
|
||||
import org.artisanlogiciel.osm.NodeRef;
|
||||
import org.artisanlogiciel.osm.Way;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Take a way with resolved ref ( ie with NodeRef having non null getNode() )
|
||||
* and create a Drawing from it.
|
||||
*/
|
||||
public class OsmToDrawing {
|
||||
|
||||
double refx = 0;
|
||||
double refy = 0;
|
||||
int mulx = 1;
|
||||
int muly = 1;
|
||||
|
||||
public OsmToDrawing()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public OsmToDrawing(int mulx, int muly) {
|
||||
this.mulx = mulx;
|
||||
this.muly = muly;
|
||||
}
|
||||
|
||||
public OsmToDrawing(double refx, double refy, int mulx, int muly) {
|
||||
this.refx = refx;
|
||||
this.refy = refy;
|
||||
this.mulx = mulx;
|
||||
this.muly = muly;
|
||||
}
|
||||
|
||||
public DrawingLine getDrawingLine(Way way)
|
||||
{
|
||||
DrawingLine drawingLine = new DrawingLine();
|
||||
for (NodeRef nr : way.getNdlist())
|
||||
{
|
||||
Node node = nr.getNode();
|
||||
drawingLine.addPoint(new Point((int) ( (node.getLat() - refx)* mulx ) , (int) ( (node.getLon() - refy ) * muly) ));
|
||||
}
|
||||
return drawingLine;
|
||||
}
|
||||
|
||||
public Drawing getDrawing(List<Way> ways)
|
||||
{
|
||||
Drawing drawing = new Drawing();
|
||||
for (Way way : ways) {
|
||||
drawing.addLine(getDrawingLine(way));
|
||||
}
|
||||
return drawing;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user