Last active
August 29, 2015 14:10
-
-
Save thorwebdev/88469dcfd04259145064 to your computer and use it in GitHub Desktop.
When testing for a polygon I want to get back all fences that are either completely or partly contained in my polygon. Therefore I test if the returned fences are within my polygon or are not disjoint.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ApiMethod(name = "polygon", httpMethod = "post", path = "polygon") | |
public ArrayList < MyFence > queryPolygon(@Named("group") String group, MyPolyLine polyline) { | |
ArrayList < MyFence > fences = new ArrayList < MyFence > (); | |
//Get index from Memcache | |
MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService(); | |
STRtree index = (STRtree) syncCache.get(group); // read from cache | |
if (index != null) { | |
//Create coordinate array. | |
double[][] points = polyline.getCoordinates(); | |
Coordinate[] coordinates = new Coordinate[points.length]; | |
int i = 0; | |
for (double[] point: points) { | |
Coordinate coordinate = new Coordinate(point[0], point[1]); | |
coordinates[i++] = coordinate; | |
} | |
//Create polygon. | |
GeometryFactory fact = new GeometryFactory(); | |
LinearRing linear = new GeometryFactory().createLinearRing(coordinates); | |
Polygon polygon = new Polygon(linear, null, fact); | |
List < MyPolygon > items = index.query(polygon.getEnvelopeInternal()); | |
if (!items.isEmpty()) { | |
for (MyPolygon poly: items) { | |
if (polygon.contains(poly) || !polygon.disjoint(poly)) { | |
long id = poly.getID(); | |
MyFence newFence = new MyFence(); | |
newFence.setId(id); | |
fences.add(newFence); | |
} | |
} | |
} | |
} else { | |
try { | |
MyIndex.buildIndex(group); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
return fences; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment