-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocation.java
More file actions
423 lines (345 loc) · 15.7 KB
/
Location.java
File metadata and controls
423 lines (345 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package javaxt.rss;
import org.w3c.dom.*;
import java.math.BigDecimal;
//******************************************************************************
//** Location Class
//******************************************************************************
/**
* Used to represent a location information associated with an RSS feed or
* entry. Supports GeoRSS and W3C Basic Geometry.
*
******************************************************************************/
public class Location {
private org.w3c.dom.Node node;
private Object geometry;
private BigDecimal lat;
private BigDecimal lon;
private Boolean hasGeometry = null; //Has 3 states: true, false, and null
private static String[] SupportedGeometryTypes = new String[]{
"Point", "Line", "Polygon", "LineString", "Box", "Envelope",
"MultiPoint", "MultiLine", "MultiPolygon", "MultiLineString"
};
/** GeoRSS NameSpace */
private String georss = "georss";
/** GML NameSpace */
private String gml = "gml";
//**************************************************************************
//** Constructor
//**************************************************************************
/** Creates a new instance of this class using an XML node. */
protected Location(org.w3c.dom.Node node, java.util.HashMap<String, String> namespaces) {
this.node = node;
String georss = namespaces.get("http://www.georss.org/georss");
if (georss!=null) this.georss = georss;
String gml = namespaces.get("http://www.opengis.net/gml");
if (gml==null) this.gml = gml;
}
//**************************************************************************
//** Constructor
//**************************************************************************
/** Creates a new instance of this class using a point. */
public Location(BigDecimal lat, BigDecimal lon){
this.lat = lat;
this.lon = lon;
}
protected Location(String lat, String lon){
this(new BigDecimal(lat), new BigDecimal(lon));
}
/*
public String toGML(){
return null;
}*/
//**************************************************************************
//** toWKT
//**************************************************************************
/** Used to return a Well-known Text (WKT) representation of the location.
*/
public String toWKT(){
if (lat!=null && lon!=null){
return "POINT(" + lon + " " + lat + ")";
}
return (getGeometry()==null ? null : getGeometry().toString());
}
//**************************************************************************
//** toString
//**************************************************************************
/** Used to return a Well-known Text (WKT) representation of the location.
*/
public String toString(){
return toWKT();
}
//**************************************************************************
//** getGeometry
//**************************************************************************
/** Used convert the location into a geometry object.
* @return Returns a javaxt.geospatial.geometry.Geometry or a
* com.vividsolutions.jts.geom.Geometry, depending on which library is found
* in the classpath. If both libraries are present, will return a
* javaxt.geospatial.geometry.Geometry object.
*/
public Object getGeometry(){
if (hasGeometry==null){
if (lat!=null && lon!=null){
String nodeName = "Point";
String nodeValue =
"<gml:" + nodeName + ">" +
"<gml:coordinates cs=\" \" ts=\",\">" + lon + " " + lat +
"</gml:coordinates>" +
"</gml:" + nodeName + ">";
geometry = getGeometry(nodeName, nodeValue);
}
else{
String nodeName = node.getNodeName().toLowerCase();
String nodeValue = Parser.getNodeValue(node).trim();
if (nodeName.equals("where") || nodeName.equals(georss + ":where")){
NodeList nodes = node.getChildNodes();
for (int j=0; j<nodes.getLength(); j++){
node = nodes.item(j);
if (node.getNodeType()==1){
nodeName = node.getNodeName();
if (isGeometryNode(nodeName.toLowerCase(), gml, georss)){
geometry = getGeometry(nodeName, Parser.getNodeValue(node));
if (geometry!=null) break;
}
}
}
}
else if(isGeometryNode(nodeName, gml, georss)){
geometry = getGeometry(nodeName, nodeValue);
}
}
hasGeometry = (geometry==null);
}
return geometry;
}
//**************************************************************************
//** isLocationNode
//**************************************************************************
/** Protected method used to help determine whether a node represents a
* location.
*/
protected static boolean isLocationNode(String nodeName, java.util.HashMap<String, String> namespaces){
String georss = namespaces.get("http://www.georss.org/georss");
if (georss==null) georss = "georss";
String gml = namespaces.get("http://www.opengis.net/gml");
if (gml==null) gml = "gml";
return (nodeName.equals("where") || nodeName.equals(georss + ":where") ||
isGeometryNode(nodeName, gml, georss));
}
//**************************************************************************
//** isGeometryNode
//**************************************************************************
/** Private method used to determine whether a node represents a geometry.
* @param gml GML NameSpace
* @param georss GeoRSS NameSpace
*/
private static boolean isGeometryNode(String nodeName, String gml, String georss){
String namespace = null;
if (nodeName.contains(":")){
namespace = nodeName.substring(0, nodeName.lastIndexOf(":"));
nodeName = nodeName.substring(nodeName.lastIndexOf(":")+1);
}
if (namespace==null || namespace.equals(gml) || namespace.equals(georss)){
for (String geometryType : SupportedGeometryTypes){
if (nodeName.equalsIgnoreCase(geometryType)) return true;
}
}
return false;
}
//**************************************************************************
//** getGeometry
//**************************************************************************
/** Calls javaxt-gis or jts to try to parse location information.
* @param nodeName XML node name (e.g. "gml:Point" or "Point"). This
* parameter is required to instantiate the JTS Parser. Note that the
* namespace is ignored.
*/
private Object getGeometry(String nodeName, String nodeValue){
if (nodeValue!=null){
nodeValue = nodeValue.trim();
if (nodeValue.length()==0) nodeValue = null;
}
if (nodeValue==null) return null;
try{
//Try to parse the geometry using the javaxt-gis library
Class CoordinateParser = new ClassLoader("javaxt.geospatial.coordinate.Parser", "javaxt-gis.jar").load();
java.lang.reflect.Constructor constructor = CoordinateParser.getDeclaredConstructor(new Class[] {String.class});
java.lang.reflect.Method method = CoordinateParser.getDeclaredMethod("getGeometry");
Object instance = constructor.newInstance(new Object[] { nodeValue });
return method.invoke(instance);
}
catch(java.lang.ClassNotFoundException e){
//Try to parse the geometry using JTS
try{
//Hack for JTS Parser to deal with GeoRSS Simple Geometries
if (!nodeValue.startsWith("<")){
String Attributes = "";
if (nodeName.contains(":")){
nodeName = nodeName.substring(nodeName.indexOf(":")+1);
}
if (nodeName.equals("point")) nodeName = "Point";
else if(nodeName.equals("line")) nodeName = "LineString";
else if(nodeName.equals("polygon")) nodeName = "Polygon";
String p1 = (nodeName.equals("Polygon") ? "<gml:outerBoundaryIs><gml:LinearRing>" : "" );
String p2 = (nodeName.equals("Polygon") ? "</gml:LinearRing></gml:outerBoundaryIs>" : "" );
nodeValue =
"<gml:" + nodeName + Attributes + ">" + p1 +
"<gml:coordinates cs=\" \" ts=\",\">" + fixCoords(nodeValue) +
"</gml:coordinates>" + p2 +
"</gml:" + nodeName + ">";
}
//Hack for JTS Parser to deal with GML pos and posList tags
for (String pos : new String[]{"pos>", "posList>"}){
if (nodeValue.contains(pos)){
StringBuffer str = new StringBuffer();
String[] arr = nodeValue.split(pos);
for (int n=0; n<arr.length; n++){
str.append(arr[n]);
if (n<arr.length-1){
if ((n % 2 == 0)){
str.append("coordinates cs=\" \" ts=\",\">");
String coords = arr[n+1];
arr[n+1] = coords.substring(coords.indexOf("<"));
str.append(fixCoords(coords.substring(0, coords.indexOf("<"))));
}
else str.append("coordinates>");
}
}
nodeValue = str.toString().trim();
}
}
if (nodeValue.startsWith("<")){ //GML
Class GMLReader = new ClassLoader("com.vividsolutions.jts.io.gml2.GMLReader", "jts").load();
for (java.lang.reflect.Method method : GMLReader.getDeclaredMethods()){
if (method.getName().equals("read")){
Class[] parameters = method.getParameterTypes();
if (parameters.length==2){
if (parameters[0].getCanonicalName().equals("java.lang.String") &&
parameters[1].getCanonicalName().equals("com.vividsolutions.jts.geom.GeometryFactory") ){
Object instance = GMLReader.newInstance();
return method.invoke(instance, new Object[] { nodeValue, parameters[1].newInstance() });
}
}
}
}
}
}
catch(java.lang.reflect.InvocationTargetException ex){
Throwable cause = ex.getCause();
if (cause != null){
String msg = cause.getLocalizedMessage();
if (msg!=null) System.err.println(cause.getLocalizedMessage());
}
}
catch(Exception ex){
//ex.printStackTrace();
}
}
catch(java.lang.InstantiationException e){}
catch(java.lang.NoSuchMethodException e){}
catch(java.lang.IllegalAccessException e){}
catch(java.lang.reflect.InvocationTargetException e){}
return null;
}
//**************************************************************************
//** fixCoords
//**************************************************************************
/** Used to add commas between coordinate tuples. */
private static String fixCoords(String coords){
coords = coords.trim();
StringBuffer str = new StringBuffer();
String[] arr = coords.split(" ");
for (int n=0; n<arr.length; n++){
str.append(arr[n]);
if (n<arr.length-1){
if ((n % 2 == 0)) str.append(" ");
else str.append(", ");
}
}
return str.toString().trim();
}
}
//******************************************************************************
//** ClassLoader
//******************************************************************************
/**
* Simple class loader. Loads a class with a given name.
*
******************************************************************************/
class ClassLoader {
private String className;
private String jarFile;
public ClassLoader(String className){
this.className = className;
}
//**************************************************************************
//** Constructor
//**************************************************************************
/** Used to dynamically Load a jar file along with the the given a class.
* @param jarFile Base file name of a jar file. Assumes the file location
* is in the same directory as this jar file.
*/
public ClassLoader(String className, String jarFile){
this.className = className;
this.jarFile = jarFile;
}
public Class load() throws java.lang.ClassNotFoundException {
try{
return Class.forName(className);
}
catch(java.lang.ClassNotFoundException e){
if (jarFile!=null){
try{
java.io.File jar = findJar(jarFile);
java.net.URLClassLoader child = new java.net.URLClassLoader(
new java.net.URL[]{jar.toURL()}, this.getClass().getClassLoader());
return Class.forName(className, true, child);
}
catch(Exception ex){}
}
throw e;
}
}
private java.io.File findJar(String prefix){
java.lang.Class Class = this.getClass();
java.lang.Package Package = Class.getPackage();
java.io.File file = null;
//Find physical path of this jar file
String path = Package.getName().replace((CharSequence)".",(CharSequence)"/");
String url = Class.getClassLoader().getResource(path).toString();
url = url.replace((CharSequence)" ",(CharSequence)"%20");
try{
java.net.URI uri = new java.net.URI(url);
if (uri.getPath()==null){
path = uri.toString();
if (path.startsWith("jar:file:")){
//Update Path and Define Zipped File
path = path.substring(path.indexOf("file:/"));
path = path.substring(0,path.toLowerCase().indexOf(".jar")+4);
if (path.startsWith("file://")){ //UNC Path
path = "C:/" + path.substring(path.indexOf("file:/")+7);
path = "/" + new java.net.URI(path).getPath();
}
else{
path = new java.net.URI(path).getPath();
}
file = new java.io.File(path);
}
}
else{
file = new java.io.File(uri);
}
}
catch(Exception e){
e.printStackTrace();
}
for (String fileName : file.getParentFile().list()){
if (fileName.toLowerCase().startsWith(prefix.toLowerCase()) &&
fileName.toLowerCase().endsWith(".jar"))
{
return new java.io.File(file.getParentFile(), fileName);
}
}
return null;
}
}