Commit 92c50767 authored by Matija Obreza's avatar Matija Obreza
Browse files

Initial commit

parents
/target/
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.genesys-pgr</groupId>
<artifactId>geo-tools</artifactId>
<version>0.1-SNAPSHOT</version>
<name>Genesys Geo Tools</name>
<licenses>
<license>
<name>The Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>
<organization>
<name>Global Crop Diversity Trust</name>
<url>https://www.croptrust.org</url>
</organization>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.target>1.8</jdk.target>
<jdk.source>1.8</jdk.source>
<!-- use the latest snapshot -->
<geotools.version>15-SNAPSHOT</geotools.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-cql</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-epsg-wkt</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-swing</artifactId>
<version>${geotools.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<inherited>true</inherited>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
<encoding>UTF-8</encoding>
<detail>true</detail>
</configuration>
</plugin>
<plugin>
<inherited>true</inherited>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${jdk.source}</source>
<target>${jdk.target}</target>
<optimize>true</optimize>
<showDeprecation>${show.deprecations}</showDeprecation>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<forkMode>once</forkMode>
<argLine>-Xms512m -Xmx1024m</argLine>
<testFailureIgnore>false</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>osgeo</id>
<name>Open Source Geospatial Foundation Repository</name>
<url>http://download.osgeo.org/webdav/geotools/</url>
</repository>
<repository>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>boundless</id>
<name>Boundless Maven Repository</name>
<url>http://repo.boundlessgeo.com/main</url>
</repository>
<repository>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>boundless-art</id>
<name>Boundless Artifactory Repository</name>
<url>https://boundless.artifactoryonline.com/boundless/main/</url>
</repository>
</repositories>
</project>
\ No newline at end of file
package org.genesys.geotools;
import java.io.File;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.map.FeatureLayer;
import org.geotools.map.Layer;
import org.geotools.map.MapContent;
import org.geotools.styling.SLD;
import org.geotools.styling.Style;
import org.geotools.swing.JMapFrame;
import org.geotools.swing.data.JFileDataStoreChooser;
/**
* Prompts the user for a shapefile and displays the contents on the screen in a map frame.
* <p>
* This is the GeoTools Quickstart application used in documentationa and tutorials. *
*/
public class Quickstart {
/**
* GeoTools Quickstart demo application. Prompts the user for a shapefile and displays its
* contents on the screen in a map frame
*/
public static void main(String[] args) throws Exception {
// display a data store file chooser dialog for shapefiles
File file = JFileDataStoreChooser.showOpenFile("shp", null);
if (file == null) {
return;
}
FileDataStore store = FileDataStoreFinder.getDataStore(file);
SimpleFeatureSource featureSource = store.getFeatureSource();
// Create a map content and add our shapefile to it
MapContent map = new MapContent();
map.setTitle("Quickstart");
Style style = SLD.createSimpleStyle(featureSource.getSchema());
Layer layer = new FeatureLayer(featureSource, style);
map.addLayer(layer);
// Now display the map
JMapFrame.showMap(map);
}
}
\ No newline at end of file
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.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.geotools.filter.text.ecql.ECQL;
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.geometry.MismatchedDimensionException;
import org.opengis.geometry.primitive.PrimitiveFactory;
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 {
@Test
public void test1() throws Exception {
//
DataStore dataStoreLand = openShapeFile("/Users/matijaobreza/Downloads/land-polygons-split-4326/land_polygons.shp");
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)")
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Pattern pattern = Pattern
.compile("^(\\-?\\d*\\.?\\d*)[ \\t,]+(\\-?\\d*\\.?\\d*)$");
System.out.println("Latitude\tLongitude\tResult");
String input = null;
do {
input = br.readLine();
Matcher matcher = pattern.matcher(input);
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)");
if (sourceWater.getFeatures(filterWater).size() > 0) {
if (sourceLand.getFeatures(filter).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(filterLand).size() > 0) {
System.out.println(latitude + ", " + longitude + ", Land");
// printFeatures(sourceLand.getFeatures(filter));
}
} else {
System.err.println("Invalid format: " + input);
}
} while (input == null || !"q".equals(input));
}
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);
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;
}
}
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