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

Extracted utils

parent e2948939
......@@ -14,7 +14,7 @@ import com.opencsv.CSVWriter;
public class LandOrSeaCLI {
// 2000 meters
private static final int ALLOWED_DISTANCE_FROM_COAST = 2000;
private static final int ALLOWED_DISTANCE_MARGIN = 2000;
public static void main(String arg[]) throws Exception {
......@@ -42,7 +42,7 @@ public class LandOrSeaCLI {
float longitude = Float.parseFloat(matcher.group(2).replace(",", "."));
System.out.println(latitude + ", " + longitude + ", "
+ landOrSeaService.classifyLocation(longitude, latitude, ALLOWED_DISTANCE_FROM_COAST));
+ landOrSeaService.classifyLocation(longitude, latitude, ALLOWED_DISTANCE_MARGIN));
} else {
System.err.println("Invalid format: " + input);
......@@ -80,12 +80,12 @@ public class LandOrSeaCLI {
try {
writeLine[writeLine.length - 1] = landOrSeaService.classifyLocation(longitude, latitude,
ALLOWED_DISTANCE_FROM_COAST);
ALLOWED_DISTANCE_MARGIN);
} catch (Exception e) {
writeLine[writeLine.length - 1] = e.getMessage();
}
} catch (NumberFormatException e) {
writeLine[writeLine.length - 1] = e.getMessage();
} catch (Throwable e) {
writeLine[writeLine.length - 1] = "ERROR: " + e.getMessage();
}
writer.writeNext(writeLine);
......
......@@ -12,24 +12,16 @@ 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.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
......@@ -38,30 +30,15 @@ import com.vividsolutions.jts.geom.Point;
*/
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;
private LoadingCache<LonLatCacheKey, Boolean> waterCache;
private LoadingCache<LonLatCacheKey, String> classifierCache;
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"
......@@ -78,7 +55,7 @@ public class LandOrSeaServiceImpl implements LandOrSeaService {
.build(new CacheLoader<LonLatCacheKey, Boolean>() {
public Boolean load(LonLatCacheKey key) throws Exception {
// System.err.println("Loading");
return _isOnLand(key.getLongitude(), key.getLatitude(), key.getAllowedDistanceFromLand());
return _isOnLand(key.getLongitude(), key.getLatitude(), key.getAllowedDistanceMargin());
}
});
......@@ -87,7 +64,7 @@ public class LandOrSeaServiceImpl implements LandOrSeaService {
public String load(LonLatCacheKey key) throws Exception {
// System.err.println("Loading");
return _classifyLocation(key.getLongitude(), key.getLatitude(),
key.getAllowedDistanceFromLand());
key.getAllowedDistanceMargin());
}
});
}
......@@ -111,7 +88,7 @@ public class LandOrSeaServiceImpl implements LandOrSeaService {
// Create a buffer and check for Land
Filter filterBuffered = ff.intersects(ff.property("the_geom"),
ff.literal(getPointBuffer(point, allowedDistanceFromLand)));
ff.literal(ShapefileUtils.getPointBuffer(point, allowedDistanceFromLand)));
if (sourceLand.getFeatures(filterBuffered).size() > 0) {
return "Coastal";
......@@ -164,7 +141,7 @@ public class LandOrSeaServiceImpl implements LandOrSeaService {
// Create a buffer and check for Land
Filter filterBuffered = ff.intersects(ff.property("the_geom"),
ff.literal(getPointBuffer(point, allowedDistanceFromLand)));
ff.literal(ShapefileUtils.getPointBuffer(point, allowedDistanceFromLand)));
if (sourceLand.getFeatures(filterBuffered).size() > 0) {
return true;
......@@ -183,21 +160,6 @@ public class LandOrSeaServiceImpl implements LandOrSeaService {
}
}
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("data", shapeFilePath);
......
......@@ -7,21 +7,21 @@ public class LonLatCacheKey implements Serializable {
private static final long serialVersionUID = -3626533849742141104L;
private float longitude;
private float latitude;
private int allowedDistanceFromLand;
private int allowedDistanceMargin;
public LonLatCacheKey(float longitude, float latitude, int allowedDistanceFromLand) {
public LonLatCacheKey(float longitude, float latitude, int allowedDistanceMargin) {
this.longitude = longitude;
this.latitude = latitude;
this.allowedDistanceFromLand = allowedDistanceFromLand;
this.allowedDistanceMargin = allowedDistanceMargin;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + allowedDistanceMargin;
result = prime * result + Float.floatToIntBits(latitude);
result = prime * result + Float.floatToIntBits(longitude);
result = prime * result + allowedDistanceFromLand;
return result;
}
......@@ -34,11 +34,12 @@ public class LonLatCacheKey implements Serializable {
if (getClass() != obj.getClass())
return false;
LonLatCacheKey other = (LonLatCacheKey) obj;
if (allowedDistanceMargin != other.allowedDistanceMargin)
return false;
if (Float.floatToIntBits(latitude) != Float.floatToIntBits(other.latitude))
return false;
if (Float.floatToIntBits(longitude) != Float.floatToIntBits(other.longitude))
return false;
if (allowedDistanceFromLand != other.allowedDistanceFromLand)
return false;
return true;
}
......@@ -51,7 +52,7 @@ public class LonLatCacheKey implements Serializable {
return longitude;
}
public int getAllowedDistanceFromLand() {
return allowedDistanceFromLand;
public int getAllowedDistanceMargin() {
return allowedDistanceMargin;
}
}
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.geometry.jts.JTS;
import org.geotools.referencing.CRS;
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.Geometry;
import com.vividsolutions.jts.geom.Point;
public class ShapefileUtils {
private static final MathTransform transformToUtm;
private static final MathTransform transformToGeo;
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 static DataStore openShapeFile(String shapeFilePath) throws MalformedURLException, IOException {
File file = new File("data", shapeFilePath);
Map<String, Object> map = new HashMap<String, Object>();
map.put("url", file.toURI().toURL());
System.err.println(file.toURI().toURL());
DataStore dataStore = DataStoreFinder.getDataStore(map);
return dataStore;
}
public static 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;
}
}
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