4

I am tasked with trying to find whether a given latitude/longitude (in degrees, so for example -50.55555, 78.232222) exists within a given Shapefile.

The Shapefile I downloaded is from here: https://www.census.gov/geo/maps-data/data/cbf/cbf_nation.html (it contains shape files for the United States and all her territories).

I am looking for a Java based solution, that where given a randomly generated latitude/longitude - to determine whether that point resides within the United States. One tool I have found useful (but am new to) is GeoTools.

So a few questions:

  • Is it possible to use GeoTools to accomplish this requirement (determine if some lat/long resides in the US)?

  • If this is not possible with GeoTools (or GeoTools alone) is there another way to find a java solution for this (without resorting to say web services like Google Maps, etc).

Also to note: I downloaded QGIS and with the use of mmqgis plugin was able to export all the shape file contents (shape_id, lat, long) for the Shapefile I mentioned above. Maybe there is another Java library I could use to read these shapes in and test whether a random lat/long falls within these shapes?

I'm new to all of this, any/all questions please ask! Thank you so much.

ionqm
  • 41
  • 1
  • 2
  • GeoTools seems like an overkill for your task. Look carefully through the geometry / feature reference of GeoTools. Finding whether a point is inside / outside of a feature must definitely be there. – 9000 Dec 12 '14 at 21:54
  • Thank you. I can't seem to find any library for this requirement. I can calculate in bounding boxes easily but can't seem to find a library where I can pass in polygons with lat/long boundaries and test whether a particular lat/long coord is within the polygon. – ionqm Dec 12 '14 at 22:39

1 Answers1

2

You can do this in several ways but I'd do it this way:

   private SimpleFeatureCollection features;
   private ReferencedEnvelope env;
   private boolean isInShape(Point p) {
        if (!env.contains(p.getCoordinate())) {
            return false;
        }
        Expression propertyName = filterFactory.property(features.getSchema()
                .getGeometryDescriptor().getName());
        Filter filter = filterFactory.contains(propertyName,
                filterFactory.literal(p));
        SimpleFeatureCollection sub = features.subCollection(filter);
        if (sub.size() > 0) {
            return true;
        }
        return false;
    }

Then read in your shapefile and extract the features and throw points at it:

public static void main(String[] args) throws IOException {
    File file = null;
    if (args.length == 0) {
        // display a data store file chooser dialog for shapefiles
        file = JFileDataStoreChooser.showOpenFile("shp", null);
        if (file == null) {
            return;
        }
    } else {
        file = new File(args[0]);
        if (!file.exists()) {
            System.err.println(file + " doesn't exist");
            return;
        }
    }
    PointInPolygon tester = new PointInPolygon();
    FileDataStore store = FileDataStoreFinder.getDataStore(file);
    SimpleFeatureSource featureSource = store.getFeatureSource();
    tester.setFeatures(featureSource.getFeatures());
    GeometryFactory fac = new GeometryFactory();
    for (int i = 0; i < 1000; i++) {

        double lat = (Math.random() * 180.0) - 90.0;
        double lon = (Math.random() * 360.0) - 180.0;
        Point p = fac.createPoint(new Coordinate(lat, lon));
        boolean flag = tester.isInShape(p);
        if (flag) {
            System.out.println(p + " is in States ");
        }
    }
}

The full example is here.

Ian Turton
  • 10,018
  • 1
  • 28
  • 47