Commit ee8f29f6 authored by Matija Obreza's avatar Matija Obreza
Browse files

Service

parent c599ccc4
LandOrSea
=========
= Running LandOrSea
jai creates an AWT window. Run with
Obtain the landorsea.jar file from the Downloads section or build from source code.
-Djava.awt.headless=true
Download the two "large split polygons" shapefiles in WGS84 projection from:
* http://openstreetmapdata.com/data/land-polygons
* http://openstreetmapdata.com/data/water-polygons
Shapefiles used in this application are derived from OSM data, © OpenStreetMap contributors.
Extract the two zip archives and move the `.shp` files into the same directory
as the jar file of this application. You should now have three files in this directory:
$ ls -1
land_polygons.shp
water_polygons.shp
landorsea.jar
jai creates an AWT window. Run in headless mode:
java -Djava.awt.headless=true -jar landorsea.jar
Test.
package org.genesys.geotools;
public interface LandOrSeaService {
public Boolean isOnLand(float longitude, float latitude, int allowedDistanceFromLand) throws Exception;
}
\ No newline at end of file
package org.genesys.geotools;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.FeatureSource;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.geometry.jts.JTS;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.referencing.CRS;
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.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
/**
* Polygons used here are derived from OSM data, © OpenStreetMap contributors
*/
public class LandOrSeaServiceImpl implements LandOrSeaService {
private static final MathTransform transformToUtm;
private static final MathTransform transformToGeo;
private static final FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
private static final GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
private FeatureSource<SimpleFeatureType, SimpleFeature> sourceLand;
private FeatureSource<SimpleFeatureType, SimpleFeature> sourceWater;
static {
try {
CoordinateReferenceSystem pointCRS = CRS.decode("EPSG:4326", true);
CoordinateReferenceSystem radiusCRS = CRS.decode("EPSG:32630", true);
transformToUtm = CRS.findMathTransform(pointCRS, radiusCRS);
transformToGeo = CRS.findMathTransform(radiusCRS, pointCRS);
} catch (Throwable e) {
// big problem
throw new RuntimeException(e);
}
}
public void afterPropertiesSet() throws MalformedURLException, IOException {
// 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");
sourceLand = dataStoreLand.getFeatureSource(dataStoreLand.getTypeNames()[0]);
sourceWater = dataStoreWater.getFeatureSource(dataStoreWater.getTypeNames()[0]);
}
/*
* (non-Javadoc)
*
* @see org.genesys.geotools.LandOrSeaService#isOnLand(float, float, int)
*/
@Override
public Boolean isOnLand(float longitude, float latitude, int allowedDistanceFromLand) throws Exception {
// Filter filter = Filter.INCLUDE; //
// ECQL.toFilter("BBOX(THE_GEOM, 10,20,30,40)")
Point point = geometryFactory.createPoint(new Coordinate(longitude, latitude));
Filter filterExact = ff.contains(ff.property("the_geom"), ff.literal(point));
try {
if (sourceWater.getFeatures(filterExact).size() > 0) {
// Create a buffer and check for Land
Filter filterBuffered = ff.intersects(ff.property("the_geom"),
ff.literal(getPointBuffer(point, allowedDistanceFromLand)));
if (sourceLand.getFeatures(filterBuffered).size() > 0) {
return true;
} else {
return false;
}
} else if (sourceLand.getFeatures(filterExact).size() > 0) {
return true;
} else {
throw new Exception("Can't figure out the result for " + longitude + "," + latitude + " with distance="
+ allowedDistanceFromLand);
}
} catch (Exception e) {
throw e;
}
}
public Geometry getPointBuffer(Point point, double distanceMeters) throws MismatchedDimensionException,
TransformException, FactoryException {
Geometry targetGeometry = JTS.transform(point, transformToUtm);
Geometry buffer = targetGeometry.buffer(distanceMeters);
buffer.setSRID(32630);
Geometry bufferGeo = JTS.transform(buffer, transformToGeo);
bufferGeo.setSRID(4326);
// System.err.println(point + " -> " + bufferGeo);
return bufferGeo;
}
private DataStore openShapeFile(String shapeFilePath) throws MalformedURLException, IOException {
File file = new File(shapeFilePath);
Map<String, Object> map = new HashMap<String, Object>();
map.put("url", file.toURI().toURL());
DataStore dataStore = DataStoreFinder.getDataStore(map);
return dataStore;
}
}
package org.genesys.geotools;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.FeatureSource;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.geometry.jts.JTS;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.referencing.CRS;
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.referencing.FactoryException;
import org.opengis.referencing.NoSuchAuthorityCodeException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
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
.getFeatureSource(dataStoreLand.getTypeNames()[0]);
FeatureSource<SimpleFeatureType, SimpleFeature> sourceWater = dataStoreWater
.getFeatureSource(dataStoreWater.getTypeNames()[0]);
// Filter filter = Filter.INCLUDE; //
// ECQL.toFilter("BBOX(THE_GEOM, 10,20,30,40)")
FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
LandOrSeaServiceImpl landOrSeaService = new LandOrSeaServiceImpl();
landOrSeaService.afterPropertiesSet();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Pattern pattern = Pattern
.compile("^(\\-?\\d*\\.?\\d*)[ \\t,]+(\\-?\\d*\\.?\\d*)$");
Pattern pattern = Pattern.compile("^(\\-?\\d*\\.?\\d*)[ \\t,]+(\\-?\\d*\\.?\\d*)$");
System.out.println("Latitude\tLongitude\tResult");
String input = null;
......@@ -76,42 +27,15 @@ public class Snippet {
if (matcher.find()) {
// System.out.println(matcher.group(1) + ", " +
// matcher.group(2));
float latitude = Float.parseFloat(matcher.group(1).replace(",",
"."));
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)");
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)));
float latitude = Float.parseFloat(matcher.group(1).replace(",", "."));
float longitude = Float.parseFloat(matcher.group(2).replace(",", "."));
if (sourceWater.getFeatures(filterExact).size() > 0) {
if (sourceLand.getFeatures(filterBuffered).size() > 0) {
// Inland
System.out.println(latitude + ", " + longitude
+ ", Land");
// printFeatures(sourceLand.getFeatures(filter));
} else {
System.out.println(latitude + ", " + longitude
+ ", Water");
// printFeatures(sourceWater.getFeatures(filter));
}
} else if (sourceLand.getFeatures(filterExact).size() > 0) {
if (landOrSeaService.isOnLand(longitude, latitude, 200)) {
System.out.println(latitude + ", " + longitude + ", Land");
// printFeatures(sourceLand.getFeatures(filter));
} else {
System.out.println(latitude + ", " + longitude + ", Water");
}
} else {
System.err.println("Invalid format: " + input);
}
......@@ -119,66 +43,31 @@ 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);
// }
// }
// }
// }
@Test
public void foo() throws MismatchedDimensionException,
NoSuchAuthorityCodeException, FactoryException, TransformException {
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
Point point = geometryFactory.createPoint(new Coordinate(0, 0));
System.out.println(point);
Geometry res = getPointBuffer(point, 100);
System.out.println(res);
}
public Geometry getPointBuffer(Point point, double distanceMeters)
throws NoSuchAuthorityCodeException, FactoryException,
MismatchedDimensionException, TransformException {
CoordinateReferenceSystem pointCRS = CRS.decode("EPSG:4326", true);
CoordinateReferenceSystem radiusCRS = CRS.decode("EPSG:32630", true);
MathTransform transformToUtm = CRS.findMathTransform(pointCRS,
radiusCRS);
Geometry targetGeometry = JTS.transform(point, transformToUtm);
Geometry buffer = targetGeometry.buffer(distanceMeters);
buffer.setSRID(32630);
MathTransform transformToGeo = CRS.findMathTransform(radiusCRS,
pointCRS);
Geometry bufferGeo = JTS.transform(buffer, transformToGeo);
bufferGeo.setSRID(4326);
System.err.println(point + " -> " + bufferGeo);
return bufferGeo;
}
private DataStore openShapeFile(String shapeFilePath)
throws MalformedURLException, IOException {
// 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,
// NoSuchAuthorityCodeException, FactoryException, TransformException {
// GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
// Point point = geometryFactory.createPoint(new Coordinate(0, 0));
// System.out.println(point);
// Geometry res = getPointBuffer(point, 100);
// System.out.println(res);
// }
File file = new File(shapeFilePath);
Map<String, Object> map = new HashMap<String, Object>();
map.put("url", file.toURI().toURL());
DataStore dataStore = DataStoreFinder.getDataStore(map);
return dataStore;
}
}
Markdown is supported
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