I think I figured it out... but if this code looks wrong, can someone let me
know?

I get a list of features by type from the repository that are within the
location's bounding box, then filter...

Jason

        List<Feature> initialList =
featureRepository.getFeaturesByBoxAndFeatureClassName(boundingBox,
featureClassName);

        int numPoints = location.getBorderPointList().size();
        Double[] xArray = new Double[numPoints];
        Double[] yArray = new Double[numPoints];

        xArray = location.getCoordinateLists().get("X").toArray(xArray);
        yArray = location.getCoordinateLists().get("Y").toArray(yArray);


        GeometryFactory geometryFactory =
JTSFactoryFinder.getGeometryFactory(null);
        Coordinate[] coordinates = new Coordinate[xArray.length + 1];

        for (int i=0; i < xArray.length; i++) {
            coordinates[i] = new Coordinate(xArray[i], yArray[i]);
        }

        coordinates[xArray.length - 1] = new Coordinate(xArray[0],
yArray[0]);

        LinearRing lr = geometryFactory.createLinearRing(coordinates);
        Polygon polygon = geometryFactory.createPolygon(lr, null);

        // check if point is in Polygon, if not throw it out of the feature
list
        List<Feature> filteredList = new ArrayList<Feature>();
        for (Feature f: initialList) {
            Point point = geometryFactory.createPoint(new
Coordinate(f.getLng(), f.getLat()));
            if (polygon.contains(point)) {
                filteredList.add(f);
            }
        }

        return filteredList;


---------- Forwarded message ----------
From: Jason Ferguson <[email protected]>
Date: Sat, Jun 4, 2011 at 10:56 AM
Subject: Is Point Inside A Polygon?
To: [email protected]


I managed to create my Polygon from arrays of x and y coordinates (doubles)
using the following code:

        xArray = location.getCoordinateLists().get("X").toArray(xArray);
        yArray = location.getCoordinateLists().get("Y").toArray(yArray);


        GeometryFactory geometryFactory =
JTSFactoryFinder.getGeometryFactory(null);
        Coordinate[] coordinates = new Coordinate[xArray.length + 1];

        for (int i=0; i < xArray.length; i++) {
            coordinates[i] = new Coordinate(xArray[i], yArray[i]);
        }

        coordinates[xArray.length - 1] = new Coordinate(xArray[0],
yArray[0]);
        LinearRing lr = geometryFactory.createLinearRing(coordinates);
        Polygon polygon = geometryFactory.createPolygon(lr, null);

However, I now have a list of Feature objects with a latitude and longitude
that I need to check if is inside the polygon. I had assumed, incorrectly,
that I could do it with the Polygon contains() method.

I don't know anything about filters or expressions, and am asking for any
assistance anyone can give me.

Jason
------------------------------------------------------------------------------
Simplify data backup and recovery for your virtual environment with vRanger.
Installation's a snap, and flexible recovery options mean your data is safe,
secure and there when you need it. Discover what all the cheering's about.
Get your free trial download today. 
http://p.sf.net/sfu/quest-dev2dev2 
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to