001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-2006, GeoTools Project Managment Committee (PMC)
005: * (C) 2005 by David Blasby and The Open Planning Project
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation;
010: * version 2.1 of the License.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * (C) 2005 by David Blasby and The Open Planning Project
018: * http://openplans.org
019: *
020: * Released under the LGPL
021: * contact: dblasby@openplans.org
022: */
023: package org.geotools.filter.function;
024:
025: import java.util.Collection;
026:
027: import com.vividsolutions.jts.operation.polygonize.Polygonizer;
028:
029: import com.vividsolutions.jts.geom.Geometry;
030: import com.vividsolutions.jts.geom.GeometryCollection;
031:
032: import com.vividsolutions.jts.geom.LineString;
033: import com.vividsolutions.jts.geom.LinearRing;
034: import com.vividsolutions.jts.geom.MultiLineString;
035: import com.vividsolutions.jts.geom.MultiPoint;
036: import com.vividsolutions.jts.geom.MultiPolygon;
037: import com.vividsolutions.jts.geom.Point;
038: import com.vividsolutions.jts.geom.Polygon;
039: import com.vividsolutions.jts.io.WKTReader;
040:
041: public class StaticGeometry {
042:
043: //--------------------------------------------------------------------------
044: //JTS SF SQL functions
045:
046: static public Geometry geomFromWKT(String wkt) {
047: WKTReader wktreader = new WKTReader();
048:
049: try {
050: return wktreader.read(wkt);
051: } catch (Exception e) {
052: throw new IllegalArgumentException("bad wkt");
053: }
054: }
055:
056: static public String toWKT(Geometry arg0) {
057: Geometry _this = arg0;
058:
059: return _this .toString();
060: }
061:
062: static public boolean contains(Geometry arg0, Geometry arg1) {
063: Geometry _this = arg0;
064:
065: return _this .contains(arg1);
066: }
067:
068: static public boolean isEmpty(Geometry arg0) {
069: Geometry _this = arg0;
070:
071: return _this .isEmpty();
072: }
073:
074: static public double geomLength(Geometry arg0) {
075: Geometry _this = arg0;
076:
077: return _this .getLength();
078: }
079:
080: static public boolean intersects(Geometry arg0, Geometry arg1) {
081: Geometry _this = arg0;
082:
083: return _this .intersects(arg1);
084: }
085:
086: static public boolean isValid(Geometry arg0) {
087: Geometry _this = arg0;
088:
089: return _this .isValid();
090: }
091:
092: static public String geometryType(Geometry arg0) {
093: Geometry _this = arg0;
094:
095: return _this .getGeometryType();
096: }
097:
098: static public int numPoints(Geometry arg0) {
099: Geometry _this = arg0;
100:
101: return _this .getNumPoints();
102: }
103:
104: static public boolean isSimple(Geometry arg0) {
105: Geometry _this = arg0;
106:
107: return _this .isSimple();
108: }
109:
110: static public double distance(Geometry arg0, Geometry arg1) {
111: Geometry _this = arg0;
112:
113: return _this .distance(arg1);
114: }
115:
116: static public boolean isWithinDistance(Geometry arg0,
117: Geometry arg1, double arg2) {
118: Geometry _this = arg0;
119:
120: return _this .isWithinDistance(arg1, arg2);
121: }
122:
123: static public double area(Geometry arg0) {
124: Geometry _this = arg0;
125:
126: return _this .getArea();
127: }
128:
129: static public Geometry centroid(Geometry arg0) {
130: Geometry _this = arg0;
131:
132: return _this .getCentroid();
133: }
134:
135: static public Geometry interiorPoint(Geometry arg0) {
136: Geometry _this = arg0;
137:
138: return _this .getInteriorPoint();
139: }
140:
141: static public int dimension(Geometry arg0) {
142: Geometry _this = arg0;
143:
144: return _this .getDimension();
145: }
146:
147: static public Geometry boundary(Geometry arg0) {
148: Geometry _this = arg0;
149:
150: return _this .getBoundary();
151: }
152:
153: static public int boundaryDimension(Geometry arg0) {
154: Geometry _this = arg0;
155:
156: return _this .getBoundaryDimension();
157: }
158:
159: static public Geometry envelope(Geometry arg0) {
160: Geometry _this = arg0;
161:
162: return _this .getEnvelope();
163: }
164:
165: static public boolean disjoint(Geometry arg0, Geometry arg1) {
166: Geometry _this = arg0;
167:
168: return _this .disjoint(arg1);
169: }
170:
171: static public boolean touches(Geometry arg0, Geometry arg1) {
172: Geometry _this = arg0;
173:
174: return _this .touches(arg1);
175: }
176:
177: static public boolean crosses(Geometry arg0, Geometry arg1) {
178: Geometry _this = arg0;
179:
180: return _this .crosses(arg1);
181: }
182:
183: static public boolean within(Geometry arg0, Geometry arg1) {
184: Geometry _this = arg0;
185:
186: return _this .within(arg1);
187: }
188:
189: static public boolean overlaps(Geometry arg0, Geometry arg1) {
190: Geometry _this = arg0;
191:
192: return _this .overlaps(arg1);
193: }
194:
195: static public boolean relatePattern(Geometry arg0, Geometry arg1,
196: String arg2) {
197: Geometry _this = arg0;
198:
199: return _this .relate(arg1, arg2);
200: }
201:
202: static public String relate(Geometry arg0, Geometry arg1) {
203: Geometry _this = arg0;
204:
205: return _this .relate(arg1).toString();
206: }
207:
208: static public Geometry bufferWithSegments(Geometry arg0,
209: double arg1, int arg2) {
210: Geometry _this = arg0;
211:
212: return _this .buffer(arg1, arg2);
213: }
214:
215: static public Geometry buffer(Geometry arg0, double arg1) {
216: Geometry _this = arg0;
217:
218: return _this .buffer(arg1);
219: }
220:
221: static public Geometry convexHull(Geometry arg0) {
222: Geometry _this = arg0;
223:
224: return _this .convexHull();
225: }
226:
227: static public Geometry intersection(Geometry arg0, Geometry arg1) {
228: Geometry _this = arg0;
229:
230: return _this .intersection(arg1);
231: }
232:
233: static public Geometry union(Geometry arg0, Geometry arg1) {
234: Geometry _this = arg0;
235:
236: return _this .union(arg1);
237: }
238:
239: static public Geometry difference(Geometry arg0, Geometry arg1) {
240: Geometry _this = arg0;
241:
242: return _this .difference(arg1);
243: }
244:
245: static public Geometry symDifference(Geometry arg0, Geometry arg1) {
246: Geometry _this = arg0;
247:
248: return _this .symDifference(arg1);
249: }
250:
251: static public boolean equalsExactTolerance(Geometry arg0,
252: Geometry arg1, double arg2) {
253: Geometry _this = arg0;
254:
255: return _this .equalsExact(arg1, arg2);
256: }
257:
258: static public boolean equalsExact(Geometry arg0, Geometry arg1) {
259: Geometry _this = arg0;
260:
261: return _this .equalsExact(arg1);
262: }
263:
264: static public int numGeometries(Geometry arg0) {
265: GeometryCollection _this = (GeometryCollection) arg0;
266:
267: return _this .getNumGeometries();
268: }
269:
270: static public Geometry getGeometryN(Geometry arg0, int arg1) {
271: GeometryCollection _this = (GeometryCollection) arg0;
272:
273: return _this .getGeometryN(arg1);
274: }
275:
276: static public double getX(Geometry arg0) {
277: Point _this = (Point) arg0;
278:
279: return _this .getX();
280: }
281:
282: static public double getY(Geometry arg0) {
283: Point _this = (Point) arg0;
284:
285: return _this .getY();
286: }
287:
288: static public boolean isClosed(Geometry arg0) {
289: LineString _this = (LineString) arg0;
290:
291: return _this .isClosed();
292: }
293:
294: static public Geometry pointN(Geometry arg0, int arg1) {
295: LineString _this = (LineString) arg0;
296:
297: return _this .getPointN(arg1);
298: }
299:
300: static public Geometry startPoint(Geometry arg0) {
301: LineString _this = (LineString) arg0;
302:
303: return _this .getStartPoint();
304: }
305:
306: static public Geometry endPoint(Geometry arg0) {
307: LineString _this = (LineString) arg0;
308:
309: return _this .getEndPoint();
310: }
311:
312: static public boolean isRing(Geometry arg0) {
313: LineString _this = (LineString) arg0;
314:
315: return _this .isRing();
316: }
317:
318: static public Geometry exteriorRing(Geometry arg0) {
319: Polygon _this = (Polygon) arg0;
320:
321: return _this .getExteriorRing();
322: }
323:
324: static public int numInteriorRing(Geometry arg0) {
325: Polygon _this = (Polygon) arg0;
326:
327: return _this .getNumInteriorRing();
328: }
329:
330: static public Geometry interiorRingN(Geometry arg0, int arg1) {
331: Polygon _this = (Polygon) arg0;
332:
333: return _this .getInteriorRingN(arg1);
334: }
335:
336: //--------------------------------------------------------------------------
337: //JAVA String functions
338:
339: static public String strConcat(String s1, String s2) {
340: return s1 + s2;
341: }
342:
343: static public boolean strEndsWith(String s1, String s2) {
344: return s1.endsWith(s2);
345: }
346:
347: static public boolean strStartsWith(String s1, String s2) {
348: return s1.startsWith(s2);
349: }
350:
351: static public boolean strEqualsIgnoreCase(String s1, String s2) {
352: return s1.equalsIgnoreCase(s2);
353: }
354:
355: static public int strIndexOf(String s1, String s2) {
356: return s1.indexOf(s2);
357: }
358:
359: static public int strLastIndexOf(String s1, String s2) {
360: return s1.lastIndexOf(s2);
361: }
362:
363: static public int strLength(String s1) {
364: return s1.length();
365: }
366:
367: static public boolean strMatches(String s1, String s2) {
368: return s1.matches(s2);
369: }
370:
371: static public String strReplace(String s1, String s2, String s3,
372: Boolean bAll) {
373: if (bAll.booleanValue()) {
374: return s1.replaceAll(s2, s3);
375: } else {
376: return s1.replaceFirst(s2, s3);
377: }
378: }
379:
380: static public String strSubstring(String s1, int beg, int end) {
381: return s1.substring(beg, end);
382: }
383:
384: static public String strSubstringStart(String s1, int beg) {
385: return s1.substring(beg);
386: }
387:
388: static public String strTrim(String s1) {
389: return s1.trim();
390: }
391:
392: //--------------------------------------------------------------------------
393: //data type xform
394:
395: static public double parseDouble(String s) {
396: return Double.parseDouble(s);
397: }
398:
399: static public int parseInt(String s) {
400: try {
401: return Integer.parseInt(s);
402: } catch (NumberFormatException e) // be nice for silly people!
403: {
404: return (int) Math.round(Double.parseDouble(s));
405: }
406: }
407:
408: static public boolean parseBoolean(String s) {
409: if (s.equalsIgnoreCase("") || s.equalsIgnoreCase("f")
410: || s.equalsIgnoreCase("false")
411: || s.equalsIgnoreCase("0") || s.equalsIgnoreCase("0.0"))
412: return false;
413: return true;
414: }
415:
416: static public int roundDouble(double d) {
417: return (int) Math.round(d);
418: }
419:
420: static public double int2ddouble(int i) {
421: return (double) i;
422: }
423:
424: static public boolean int2bbool(int i) {
425: return i == 0;
426: }
427:
428: static public boolean double2bool(double d) {
429: return d == 0;
430: }
431:
432: static public Object if_then_else(boolean p, Object a, Object b) {
433: if (p)
434: return a;
435: else
436: return b;
437: }
438:
439: // --------------------------------------------------------------------------
440: //OGC Filter comparisionOP functions
441:
442: static public boolean equalTo(Object o1, Object o2) {
443: if (o1.getClass() == o2.getClass())
444: return o1.equals(o2);
445: if ((o1 instanceof Number) && (o2 instanceof Number)) {
446: return ((Number) o1).doubleValue() == ((Number) o2)
447: .doubleValue();
448: }
449: return (o1).toString().equals((o2).toString());
450: }
451:
452: static public boolean notEqualTo(Object o1, Object o2) {
453: return !(equalTo(o1, o2));
454: }
455:
456: static public boolean lessThan(Object o1, Object o2) {
457: if ((o1 instanceof Integer) && (o2 instanceof Integer)) {
458: return ((Integer) o1).intValue() < ((Integer) o2)
459: .intValue();
460: }
461: if ((o1 instanceof Number) && (o2 instanceof Number)) {
462: return ((Number) o1).doubleValue() < ((Number) o2)
463: .doubleValue();
464: }
465: return (o1).toString().compareTo((o2).toString()) == 0;
466: }
467:
468: static public boolean greaterThan(Object o1, Object o2) {
469: if ((o1 instanceof Integer) && (o2 instanceof Integer)) {
470: return ((Integer) o1).intValue() > ((Integer) o2)
471: .intValue();
472: }
473: if ((o1 instanceof Number) && (o2 instanceof Number)) {
474: return ((Number) o1).doubleValue() > ((Number) o2)
475: .doubleValue();
476: }
477: return (o1).toString().compareTo((o2).toString()) == 2;
478: }
479:
480: static public boolean greaterEqualThan(Object o1, Object o2) {
481: if ((o1 instanceof Integer) && (o2 instanceof Integer)) {
482: return ((Integer) o1).intValue() >= ((Integer) o2)
483: .intValue();
484: }
485: if ((o1 instanceof Number) && (o2 instanceof Number)) {
486: return ((Number) o1).doubleValue() >= ((Number) o2)
487: .doubleValue();
488: }
489: return (((o1).toString().compareTo((o2).toString()) == 2) || ((o1)
490: .toString().compareTo((o2).toString()) == 1));
491: }
492:
493: static public boolean lessEqualThan(Object o1, Object o2) {
494: if ((o1 instanceof Integer) && (o2 instanceof Integer)) {
495: return ((Integer) o1).intValue() <= ((Integer) o2)
496: .intValue();
497: }
498: if ((o1 instanceof Number) && (o2 instanceof Number)) {
499: return ((Number) o1).doubleValue() <= ((Number) o2)
500: .doubleValue();
501: }
502: return (((o1).toString().compareTo((o2).toString()) == 0) || ((o1)
503: .toString().compareTo((o2).toString()) == 1));
504: }
505:
506: static public boolean isLike(String s1, String s2) {
507: return s1.matches(s2); // this sucks, but hay...
508: }
509:
510: static public boolean isNull(Object o) {
511: return o == null;
512: }
513:
514: static public boolean between(Object o, Object o_low, Object o_high) {
515:
516: return StaticGeometry.greaterEqualThan(o, o_low)
517: && StaticGeometry.lessEqualThan(o, o_high);
518: }
519:
520: static public boolean not(boolean b) {
521: return !b;
522: }
523:
524: // --------------------------------------------------------------------------
525: // SQL "var in (list)"
526:
527: static public boolean in2(Object s, Object s1, Object s2) {
528: return (equalTo(s, s1) || equalTo(s, s2));
529: }
530:
531: static public boolean in3(Object s, Object s1, Object s2, Object s3) {
532: return (equalTo(s, s1) || equalTo(s, s2) || equalTo(s, s3));
533: }
534:
535: static public boolean in4(Object s, Object s1, Object s2,
536: Object s3, Object s4) {
537: return (equalTo(s, s1) || equalTo(s, s2) || equalTo(s, s3) || equalTo(
538: s, s4));
539: }
540:
541: static public boolean in5(Object s, Object s1, Object s2,
542: Object s3, Object s4, Object s5) {
543: return (equalTo(s, s1) || equalTo(s, s2) || equalTo(s, s3)
544: || equalTo(s, s4) || equalTo(s, s5));
545: }
546:
547: static public boolean in6(Object s, Object s1, Object s2,
548: Object s3, Object s4, Object s5, Object s6) {
549: return (equalTo(s, s1) || equalTo(s, s2) || equalTo(s, s3)
550: || equalTo(s, s4) || equalTo(s, s5) || equalTo(s, s6));
551: }
552:
553: static public boolean in7(Object s, Object s1, Object s2,
554: Object s3, Object s4, Object s5, Object s6, Object s7) {
555: return (equalTo(s, s1) || equalTo(s, s2) || equalTo(s, s3)
556: || equalTo(s, s4) || equalTo(s, s5) || equalTo(s, s6) || equalTo(
557: s, s7));
558: }
559:
560: static public boolean in8(Object s, Object s1, Object s2,
561: Object s3, Object s4, Object s5, Object s6, Object s7,
562: Object s8) {
563: return (equalTo(s, s1) || equalTo(s, s2) || equalTo(s, s3)
564: || equalTo(s, s4) || equalTo(s, s5) || equalTo(s, s6)
565: || equalTo(s, s7) || equalTo(s, s8));
566: }
567:
568: static public boolean in9(Object s, Object s1, Object s2,
569: Object s3, Object s4, Object s5, Object s6, Object s7,
570: Object s8, Object s9) {
571: return (equalTo(s, s1) || equalTo(s, s2) || equalTo(s, s3)
572: || equalTo(s, s4) || equalTo(s, s5) || equalTo(s, s6)
573: || equalTo(s, s7) || equalTo(s, s8) || equalTo(s, s9));
574: }
575:
576: static public boolean in10(Object s, Object s1, Object s2,
577: Object s3, Object s4, Object s5, Object s6, Object s7,
578: Object s8, Object s9, Object s10) {
579: return (equalTo(s, s1) || equalTo(s, s2) || equalTo(s, s3)
580: || equalTo(s, s4) || equalTo(s, s5) || equalTo(s, s6)
581: || equalTo(s, s7) || equalTo(s, s8) || equalTo(s, s9) || equalTo(
582: s, s10));
583: }
584:
585: }
|