Ticket #7515: JTSConversion.java

File JTSConversion.java, 3.0 KB (added by joshdoe, 13 years ago)

converting to JTS geometries

Line 
1package org.openstreetmap.josm.plugins.conflation;
2
3import com.vividsolutions.jts.geom.*;
4import com.vividsolutions.jts.geom.impl.CoordinateArraySequence;
5import com.vividsolutions.jump.feature.AbstractBasicFeature;
6import com.vividsolutions.jump.feature.AttributeType;
7import com.vividsolutions.jump.feature.Feature;
8import com.vividsolutions.jump.feature.FeatureSchema;
9import java.util.List;
10import java.util.Map;
11import org.openstreetmap.josm.data.coor.LatLon;
12import org.openstreetmap.josm.data.osm.Node;
13import org.openstreetmap.josm.data.osm.OsmPrimitive;
14import org.openstreetmap.josm.data.osm.Relation;
15import org.openstreetmap.josm.data.osm.Way;
16
17/**
18 * Methods to convert JOSM geometry to JTS geometry
19 */
20public final class JTSConversion {
21
22 public final OsmPrecisionModel osmPrecisionModel = new OsmPrecisionModel();
23 public final GeometryFactory osmGeometryFactory = new GeometryFactory(getPrecisionModel());
24
25 public OsmPrecisionModel getPrecisionModel() {
26 return osmPrecisionModel;
27 }
28
29 public GeometryFactory getGeometryFactory() {
30 return osmGeometryFactory;
31 }
32
33 /**
34 * Simple subclass to match precision with the OSM data model (7 decimal
35 * places)
36 */
37 public class OsmPrecisionModel extends com.vividsolutions.jts.geom.PrecisionModel {
38
39 public OsmPrecisionModel() {
40 super(10000000);
41 }
42 }
43
44 public Coordinate convertNodeToCoordinate(Node node) {
45 LatLon ll = node.getCoor();
46 return new Coordinate(ll.lon(), ll.lat());
47 }
48
49 public CoordinateSequence convertNodesToCoordinateSequence(List<Node> nodes) {
50 Coordinate coords[] = new Coordinate[nodes.size()];
51 for (int i = 0; i < nodes.size(); i++) {
52 coords[i] = convertNodeToCoordinate(nodes.get(i));
53 }
54 return new CoordinateArraySequence(coords);
55 }
56
57 public Point convertNode(Node node) {
58 Coordinate coords[] = {convertNodeToCoordinate(node)};
59 return new com.vividsolutions.jts.geom.Point(new CoordinateArraySequence(coords), getGeometryFactory());
60 }
61
62 public Geometry convertWay(Way way) {
63 CoordinateSequence coordSeq = convertNodesToCoordinateSequence(way.getNodes());
64
65 // TODO: need to check tags to determine whether area or not
66 if (way.isClosed()) {
67 LinearRing ring = new LinearRing(coordSeq, getGeometryFactory());
68 return new Polygon(ring, null, getGeometryFactory());
69 } else {
70 return new LineString(coordSeq, getGeometryFactory());
71 }
72 }
73
74 public Geometry convert(OsmPrimitive prim) {
75 if (prim instanceof Node) {
76 return convertNode((Node) prim);
77 } else if (prim instanceof Way) {
78 return convertWay((Way) prim);
79 } else if (prim instanceof Relation) {
80 throw new UnsupportedOperationException("Relations not supported yet.");
81 } else {
82 throw new UnsupportedOperationException("Unknown primitive.");
83 }
84 }
85}