Determining if a geographic point is within X meters of the state border (using shapefile for border data)

So, I am writing a Java application, and I have an ESRI Shapefile that contains the borders of all US states. I need to determine if any given lat / lon point is at a given distance from ANY line of the state border, i.e. I will not indicate a specific border line, just need to see if the point is close to any of them.

The solution does not have to be very accurate; for example, I don’t have to deal with a dimension perpendicular to the border, or anything else. Just checking if moving X meters north, south, east or west will lead to crossing the border will be more than enough. The solution must be efficient from the point of view of calculations, since I will perform a huge amount of these calculations.

I plan to use the GeoTools library (although if there is a simpler option, I'm all for it) with the Shapefile plugin. What I really don’t understand is: Once I have loaded the shapefile into memory, how can I check if I am close to the border?

Thanks! Dan

+4
source share
3 answers

Assuming JTS for the geometry that is included with GeoTools:

public boolean pointIsClose( File file, Point targetPoint,double distance) { boolean ret = false; Map connect = new HashMap(); connect.put("url", file.toURL()); DataStore dataStore = DataStoreFinder.getDataStore(connect); FeatureSource featureSource = dataStore.getFeatureSource(typeName); FeatureCollection collection = featureSource.getFeatures(); FeatureIterator iterator = collection.features(); try { while (iterator.hasNext()) { Feature feature = iterator.next(); Geometry sourceGeometry = feature.getDefaultGeometry(); ret= sourceGeometry.isWithinDistance(targetPoint, distance ); } } finally { iterator.close(); } return ret; } 

The double number should be obtained from CRS , which will determine the units of measurement in which the calculation will be performed.

This is the import of geotexts:

 import org.geotools.data.DataStore; import org.geotools.data.DataStoreFinder; import org.geotools.data.FeatureSource; import org.geotools.feature.Feature; import org.geotools.feature.FeatureCollection; import org.geotools.feature.FeatureIterator; import org.geotools.geometry.jts.JTS; import org.geotools.referencing.CRS; import org.opengis.referencing.crs.CoordinateReferenceSystem; 
+7
source

If you just want to know if point A is within X meters of the state border, and X is constant, and it doesn’t matter what border it is, you can pre-copy the negative space as a series of blocks. Then all you have to do is check for the presence of each of these fields against a point. If none of them matches, you are not in negative space.

+2
source

If you can somehow extract the shape for each state from the shapefile, create an x ​​meter envelope on the side (with your dot in the exact center) and see if these two shapes intersect, you can answer the question.

If I were to use the ESRI ArcGIS Engine, I would use an ISpatialFilter with a point defined in the geometry (possibly with a buffer) and a query related to the States form file. Any returned result indicates that the point was close to state. I am not familiar with GeoTools and, looking through their documentation, I did not come across what looked like this type of functionality, but they should have it. You can see examples of using GeoTools to perform spatial queries in shapefiles.

+1
source

All Articles