Commit 88861e73 authored by Matija Obreza's avatar Matija Obreza
Browse files

Find "Land" accessions within with X meters buffer

parent 92c50767
......@@ -13,9 +13,7 @@ import java.util.regex.Pattern;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.FeatureSource;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.geotools.filter.text.ecql.ECQL;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.geometry.jts.JTS;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.referencing.CRS;
......@@ -23,8 +21,8 @@ import org.junit.Test;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.Filter;
import org.opengis.filter.FilterFactory2;
import org.opengis.geometry.MismatchedDimensionException;
import org.opengis.geometry.primitive.PrimitiveFactory;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.NoSuchAuthorityCodeException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
......@@ -38,11 +36,20 @@ import com.vividsolutions.jts.geom.Point;
public class Snippet {
/**
* Polygons used here are derived from OSM data, © OpenStreetMap contributors
*
* @throws Exception
*/
@Test
public void test1() throws Exception {
//
// http://openstreetmapdata.com/data/land-polygons
// Use "split large polygons"
DataStore dataStoreLand = openShapeFile("/Users/matijaobreza/Downloads/land-polygons-split-4326/land_polygons.shp");
// http://openstreetmapdata.com/data/water-polygons
// Use "split large polygons"
DataStore dataStoreWater = openShapeFile("/Users/matijaobreza/Downloads/water-polygons-split-4326/water_polygons.shp");
FeatureSource<SimpleFeatureType, SimpleFeature> sourceLand = dataStoreLand
......@@ -53,6 +60,9 @@ public class Snippet {
// Filter filter = Filter.INCLUDE; //
// ECQL.toFilter("BBOX(THE_GEOM, 10,20,30,40)")
FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Pattern pattern = Pattern
......@@ -71,15 +81,24 @@ public class Snippet {
float longitude = Float.parseFloat(matcher.group(2).replace(
",", "."));
Filter filterWater = ECQL.toFilter("CONTAINS (the_geom, POINT("
+ longitude + " " + latitude + "))");
Filter filterLand = ECQL.toFilter("CONTAINS (the_geom, POINT("
+ longitude + " " + latitude + "))");
Filter filter = ECQL.toFilter("DWITHIN(the_geom, POINT("
+ longitude + " " + latitude + "), 100, meters)");
if (sourceWater.getFeatures(filterWater).size() > 0) {
if (sourceLand.getFeatures(filter).size() > 0) {
// Filter filterWater =
// ECQL.toFilter("CONTAINS (the_geom, POINT("
// + longitude + " " + latitude + "))");
// Filter filterLand =
// ECQL.toFilter("CONTAINS (the_geom, POINT("
// + longitude + " " + latitude + "))");
// Filter filter = ECQL.toFilter("DWITHIN(the_geom, POINT("
// + longitude + " " + latitude + "), 100, meters)");
Point point = geometryFactory.createPoint(new Coordinate(
longitude, latitude));
Filter filterExact = ff.contains(ff.property("the_geom"),
ff.literal(point));
Filter filterBuffered = ff.intersects(ff.property("the_geom"),
ff.literal(getPointBuffer(point, 200)));
if (sourceWater.getFeatures(filterExact).size() > 0) {
if (sourceLand.getFeatures(filterBuffered).size() > 0) {
// Inland
System.out.println(latitude + ", " + longitude
+ ", Land");
......@@ -89,7 +108,7 @@ public class Snippet {
+ ", Water");
// printFeatures(sourceWater.getFeatures(filter));
}
} else if (sourceLand.getFeatures(filterLand).size() > 0) {
} else if (sourceLand.getFeatures(filterExact).size() > 0) {
System.out.println(latitude + ", " + longitude + ", Land");
// printFeatures(sourceLand.getFeatures(filter));
}
......@@ -100,22 +119,22 @@ public class Snippet {
}
private void printFeatures(
FeatureCollection<SimpleFeatureType, SimpleFeature> collection) {
try (FeatureIterator<SimpleFeature> features = collection.features()) {
System.err.println("1");
while (features.hasNext()) {
SimpleFeature feature = features.next();
System.out.print(feature.getID());
System.out.print(": ");
System.out.println(feature.getDefaultGeometryProperty()
.getValue());
for (Object x : feature.getAttributes()) {
System.out.println(x);
}
}
}
}
// private void printFeatures(
// FeatureCollection<SimpleFeatureType, SimpleFeature> collection) {
// try (FeatureIterator<SimpleFeature> features = collection.features()) {
// System.err.println("1");
// while (features.hasNext()) {
// SimpleFeature feature = features.next();
// System.out.print(feature.getID());
// System.out.print(": ");
// System.out.println(feature.getDefaultGeometryProperty()
// .getValue());
// for (Object x : feature.getAttributes()) {
// System.out.println(x);
// }
// }
// }
// }
@Test
public void foo() throws MismatchedDimensionException,
......@@ -147,7 +166,7 @@ public class Snippet {
Geometry bufferGeo = JTS.transform(buffer, transformToGeo);
bufferGeo.setSRID(4326);
System.err.println(point + " -> " + bufferGeo);
return bufferGeo;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment