read osm format longitude and latitude bounds, separate ways and buildings
- read longitude and lattude boundaries - separate ways ( open ways ) from buildings ( closed ) - save as .imc and .drawing ( raw format ) [ since .imc seems buggy ) - read uncompressed drawing format
This commit is contained in:
@@ -397,6 +397,26 @@ implements StatusListener
|
|||||||
|
|
||||||
loadMenu.add(loadImcButton);
|
loadMenu.add(loadImcButton);
|
||||||
|
|
||||||
|
Action loadDrawingAction = new AbstractAction() {
|
||||||
|
public void actionPerformed(ActionEvent evt) {
|
||||||
|
//
|
||||||
|
addStatus("load Drawing");
|
||||||
|
|
||||||
|
String filename = loadName.getText();
|
||||||
|
|
||||||
|
if ((filename.length() > 0)) {
|
||||||
|
setMazeName(filename);
|
||||||
|
loadDrawing(false);
|
||||||
|
refresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
JButton loadDrawingButton = new JButton(labels.getString("load" ) + " Drawing");
|
||||||
|
loadDrawingButton.addActionListener(loadDrawingAction);
|
||||||
|
|
||||||
|
loadMenu.add(loadDrawingButton);
|
||||||
|
|
||||||
Action loadOsmAction = new AbstractAction() {
|
Action loadOsmAction = new AbstractAction() {
|
||||||
public void actionPerformed(ActionEvent evt) {
|
public void actionPerformed(ActionEvent evt) {
|
||||||
//
|
//
|
||||||
@@ -701,9 +721,9 @@ implements StatusListener
|
|||||||
FileInputStream inputStream = null;
|
FileInputStream inputStream = null;
|
||||||
try {
|
try {
|
||||||
// TODO really use InputStream and not pathname
|
// TODO really use InputStream and not pathname
|
||||||
OsmToDrawing converter = new OsmToDrawing(43.6399000, 7.0058300, 10000,10000);
|
|
||||||
OsmReader reader = new OsmReader(infile.getCanonicalPath());
|
OsmReader reader = new OsmReader(infile.getCanonicalPath());
|
||||||
reader.read();
|
reader.read();
|
||||||
|
OsmToDrawing converter = new OsmToDrawing(reader, 25000,25000);
|
||||||
Drawing drawing = converter.getDrawing(reader.getWays());
|
Drawing drawing = converter.getDrawing(reader.getWays());
|
||||||
statusEnable = false;
|
statusEnable = false;
|
||||||
maze.addDrawing(drawing,add);
|
maze.addDrawing(drawing,add);
|
||||||
@@ -760,6 +780,40 @@ implements StatusListener
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void loadDrawing(boolean add) {
|
||||||
|
if ( maze != null ) {
|
||||||
|
new Thread() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
File infile = new File(params.getSaveDir(), params.getName() + ".drawing");
|
||||||
|
FileInputStream inputStream = null;
|
||||||
|
try {
|
||||||
|
inputStream = new FileInputStream(infile);
|
||||||
|
// TODO
|
||||||
|
// model = new MazePersistRaw().parseInputStream("raw",inputStream);
|
||||||
|
Drawing drawing = new Drawing();
|
||||||
|
drawing.loadLines(new DataInputStream(inputStream));
|
||||||
|
statusEnable = false;
|
||||||
|
maze.addDrawing(drawing,add);
|
||||||
|
statusEnable = true;
|
||||||
|
} catch (IOException io) {
|
||||||
|
io.printStackTrace(System.err);
|
||||||
|
statusField.setText("[ERROR] Can't load " + infile.getAbsolutePath());
|
||||||
|
} finally {
|
||||||
|
if (inputStream != null) {
|
||||||
|
// cleanup
|
||||||
|
try {
|
||||||
|
inputStream.close();
|
||||||
|
} catch (Exception any) {
|
||||||
|
// don't care really
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private JButton addDirection(final JPanel panel, final String direction, String key, Action goAction) {
|
private JButton addDirection(final JPanel panel, final String direction, String key, Action goAction) {
|
||||||
String actionName = "go" + direction;
|
String actionName = "go" + direction;
|
||||||
JButton button = new JButton(direction);
|
JButton button = new JButton(direction);
|
||||||
|
|||||||
@@ -1,2 +1,4 @@
|
|||||||
package org.artisanlogiciel.games.minetest;public class Node {
|
package org.artisanlogiciel.games.minetest;
|
||||||
|
|
||||||
|
public class Node {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,10 +18,38 @@ public class OsmReader {
|
|||||||
private HashMap<NodeRef,Node> refs;
|
private HashMap<NodeRef,Node> refs;
|
||||||
private List<Way> ways;
|
private List<Way> ways;
|
||||||
|
|
||||||
|
public List<Way> getBuildings() {
|
||||||
|
return buildings;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Way> buildings;
|
||||||
|
|
||||||
|
public double getMinlat() {
|
||||||
|
return minlat;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getMinlon() {
|
||||||
|
return minlon;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getMaxlat() {
|
||||||
|
return maxlat;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getMaxlon() {
|
||||||
|
return maxlon;
|
||||||
|
}
|
||||||
|
|
||||||
|
double minlat = 0;
|
||||||
|
double minlon = 0;
|
||||||
|
double maxlat = 180;
|
||||||
|
double maxlon = 180;
|
||||||
|
|
||||||
public OsmReader(String sample) {
|
public OsmReader(String sample) {
|
||||||
this.sample = sample;
|
this.sample = sample;
|
||||||
refs = new HashMap<>();
|
refs = new HashMap<>();
|
||||||
ways = new ArrayList<>();
|
ways = new ArrayList<>();
|
||||||
|
buildings = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Way> getWays() {
|
public List<Way> getWays() {
|
||||||
@@ -38,6 +66,25 @@ public class OsmReader {
|
|||||||
Document doc = builder.parse(new FileInputStream(sample));
|
Document doc = builder.parse(new FileInputStream(sample));
|
||||||
Element root = doc.getDocumentElement();
|
Element root = doc.getDocumentElement();
|
||||||
System.out.println(root);
|
System.out.println(root);
|
||||||
|
// <bounds minlat="43.6399000" minlon="7.0058300" maxlat="43.6435000" maxlon="7.0111700"/>
|
||||||
|
NodeList boundList = doc.getElementsByTagName("bounds");
|
||||||
|
for (int temp = 0; temp < boundList.getLength(); temp++) {
|
||||||
|
org.w3c.dom.Node nNode = boundList.item(temp);
|
||||||
|
if (nNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
|
||||||
|
Element e = (Element) nNode;
|
||||||
|
minlat = Double.valueOf(e.getAttribute("minlat"));
|
||||||
|
minlon = Double.valueOf(e.getAttribute("minlon"));
|
||||||
|
maxlat = Double.valueOf(e.getAttribute("maxlat"));
|
||||||
|
maxlon = Double.valueOf(e.getAttribute("maxlon"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int boundserror = 0;
|
||||||
|
System.out.println("minlat " + minlat);
|
||||||
|
System.out.println("maxlat " + maxlat);
|
||||||
|
System.out.println("minlon " + minlon);
|
||||||
|
System.out.println("maxlon " + maxlon);
|
||||||
|
|
||||||
NodeList nList = doc.getElementsByTagName("node");
|
NodeList nList = doc.getElementsByTagName("node");
|
||||||
for (int temp = 0; temp < nList.getLength(); temp++) {
|
for (int temp = 0; temp < nList.getLength(); temp++) {
|
||||||
org.w3c.dom.Node nNode = nList.item(temp);
|
org.w3c.dom.Node nNode = nList.item(temp);
|
||||||
@@ -46,6 +93,26 @@ public class OsmReader {
|
|||||||
long id = Long.valueOf(e.getAttribute("id"));
|
long id = Long.valueOf(e.getAttribute("id"));
|
||||||
double lat = Double.valueOf(e.getAttribute("lat"));
|
double lat = Double.valueOf(e.getAttribute("lat"));
|
||||||
double lon = Double.valueOf(e.getAttribute("lon"));
|
double lon = Double.valueOf(e.getAttribute("lon"));
|
||||||
|
if (lat < minlat)
|
||||||
|
{
|
||||||
|
boundserror ++;
|
||||||
|
System.err.println("lat < minlat " + lat );
|
||||||
|
}
|
||||||
|
else if (lat > maxlat)
|
||||||
|
{
|
||||||
|
boundserror ++;
|
||||||
|
System.err.println("lat > minlat " + lat);
|
||||||
|
}
|
||||||
|
if (lon < minlon)
|
||||||
|
{
|
||||||
|
boundserror ++;
|
||||||
|
System.err.println("lon < minlon " + lon);
|
||||||
|
}
|
||||||
|
else if (lon > maxlon)
|
||||||
|
{
|
||||||
|
boundserror ++;
|
||||||
|
System.err.println("lon > minlon " + lon);
|
||||||
|
}
|
||||||
Node node = new Node(new NodeRef(id), lat, lon);
|
Node node = new Node(new NodeRef(id), lat, lon);
|
||||||
refs.put(node.getRef(),node);
|
refs.put(node.getRef(),node);
|
||||||
}
|
}
|
||||||
@@ -75,9 +142,20 @@ public class OsmReader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if ( nodeRefList.size() > 0 )
|
||||||
|
{
|
||||||
Way way = new Way(id, nodeRefList);
|
Way way = new Way(id, nodeRefList);
|
||||||
|
// closed way ?
|
||||||
|
if (nodeRefList.get(0).equals(nodeRefList.get(nodeRefList.size() -1)))
|
||||||
|
{
|
||||||
|
buildings.add(way);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
ways.add(way);
|
ways.add(way);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
catch (Exception bad)
|
catch (Exception bad)
|
||||||
{
|
{
|
||||||
System.err.println("Too bad way");
|
System.err.println("Too bad way");
|
||||||
@@ -98,12 +176,5 @@ public class OsmReader {
|
|||||||
System.out.println(refs);
|
System.out.println(refs);
|
||||||
System.out.println(ways);
|
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,9 +4,14 @@ import org.artisanlogiciel.graphics.Drawing;
|
|||||||
import org.artisanlogiciel.graphics.DrawingLine;
|
import org.artisanlogiciel.graphics.DrawingLine;
|
||||||
import org.artisanlogiciel.osm.Node;
|
import org.artisanlogiciel.osm.Node;
|
||||||
import org.artisanlogiciel.osm.NodeRef;
|
import org.artisanlogiciel.osm.NodeRef;
|
||||||
|
import org.artisanlogiciel.osm.OsmReader;
|
||||||
import org.artisanlogiciel.osm.Way;
|
import org.artisanlogiciel.osm.Way;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -20,9 +25,28 @@ public class OsmToDrawing {
|
|||||||
int mulx = 1;
|
int mulx = 1;
|
||||||
int muly = 1;
|
int muly = 1;
|
||||||
|
|
||||||
public OsmToDrawing()
|
double minlat = 0;
|
||||||
{
|
double minlon = 0;
|
||||||
|
double maxlat = 180;
|
||||||
|
double maxlon = 180;
|
||||||
|
|
||||||
|
int boundserror = 0;
|
||||||
|
|
||||||
|
public OsmToDrawing(OsmReader reader, int mulx, int muly)
|
||||||
|
{
|
||||||
|
this.refx = reader.getMinlat();
|
||||||
|
this.minlat = reader.getMinlat();
|
||||||
|
this.refy = reader.getMinlon();
|
||||||
|
this.minlon = reader.getMinlon();
|
||||||
|
this.maxlat = reader.getMaxlat();
|
||||||
|
this.maxlon = reader.getMaxlon();
|
||||||
|
this.mulx = mulx;
|
||||||
|
this.muly = muly;
|
||||||
|
|
||||||
|
System.out.println("minlat " + minlat);
|
||||||
|
System.out.println("maxlat " + maxlat);
|
||||||
|
System.out.println("minlon " + minlon);
|
||||||
|
System.out.println("maxlon " + maxlon);
|
||||||
}
|
}
|
||||||
|
|
||||||
public OsmToDrawing(int mulx, int muly) {
|
public OsmToDrawing(int mulx, int muly) {
|
||||||
@@ -43,7 +67,29 @@ public class OsmToDrawing {
|
|||||||
for (NodeRef nr : way.getNdlist())
|
for (NodeRef nr : way.getNdlist())
|
||||||
{
|
{
|
||||||
Node node = nr.getNode();
|
Node node = nr.getNode();
|
||||||
drawingLine.addPoint(new Point((int) ( (node.getLat() - refx)* mulx ) , (int) ( (node.getLon() - refy ) * muly) ));
|
double lat = node.getLat();
|
||||||
|
double lon = node.getLon();
|
||||||
|
if (lat < minlat)
|
||||||
|
{
|
||||||
|
boundserror ++;
|
||||||
|
System.err.println("lat < minlat " + lat );
|
||||||
|
}
|
||||||
|
else if (lat > maxlat)
|
||||||
|
{
|
||||||
|
boundserror ++;
|
||||||
|
System.err.println("lat > minlat " + lat);
|
||||||
|
}
|
||||||
|
if (lon < minlon)
|
||||||
|
{
|
||||||
|
boundserror ++;
|
||||||
|
System.err.println("lon < minlon " + lon);
|
||||||
|
}
|
||||||
|
else if (lon > maxlon)
|
||||||
|
{
|
||||||
|
boundserror ++;
|
||||||
|
System.err.println("lon > minlon " + lon);
|
||||||
|
}
|
||||||
|
drawingLine.addPoint(new Point((int) ( (lat - refx)* mulx ) , (int) ( (lon - refy ) * muly) ));
|
||||||
}
|
}
|
||||||
return drawingLine;
|
return drawingLine;
|
||||||
}
|
}
|
||||||
@@ -52,8 +98,57 @@ public class OsmToDrawing {
|
|||||||
{
|
{
|
||||||
Drawing drawing = new Drawing();
|
Drawing drawing = new Drawing();
|
||||||
for (Way way : ways) {
|
for (Way way : ways) {
|
||||||
drawing.addLine(getDrawingLine(way));
|
int currenterrors = boundserror;
|
||||||
|
DrawingLine line = getDrawingLine(way);
|
||||||
|
if ( currenterrors == boundserror ) {
|
||||||
|
drawing.addLine(line);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
System.err.println("skipping one line out of bound nodes");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return drawing;
|
return drawing;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void main(String pArgs[])
|
||||||
|
{
|
||||||
|
if ( pArgs.length > 0 ) {
|
||||||
|
String sample = pArgs[0];
|
||||||
|
OsmReader osmreader = new OsmReader(sample);
|
||||||
|
osmreader.read();
|
||||||
|
osmreader.dump();
|
||||||
|
OsmToDrawing converter = new OsmToDrawing(osmreader, 25000,25000);
|
||||||
|
Drawing drawing = converter.getDrawing(osmreader.getWays());
|
||||||
|
String savename = sample + ".ways";
|
||||||
|
saveImc(savename, drawing);
|
||||||
|
saveDrawing(savename, drawing);
|
||||||
|
Drawing drawing2 = converter.getDrawing(osmreader.getBuildings());
|
||||||
|
savename = sample + ".buildings";
|
||||||
|
saveImc(savename, drawing2);
|
||||||
|
saveDrawing(savename, drawing2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void saveDrawing(String sample, Drawing drawing) {
|
||||||
|
try {
|
||||||
|
FileOutputStream out = new FileOutputStream(sample + ".drawing");
|
||||||
|
drawing.saveLines(new DataOutputStream(out));
|
||||||
|
out.flush();
|
||||||
|
out.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void saveImc(String sample, Drawing drawing) {
|
||||||
|
try {
|
||||||
|
FileOutputStream out = new FileOutputStream(sample + ".imc");
|
||||||
|
drawing.saveLinesKompressed(new DataOutputStream(out));
|
||||||
|
out.flush();
|
||||||
|
out.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user