Skip to content

Instantly share code, notes, and snippets.

@schierlm
Created March 31, 2016 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schierlm/bcc2961056d735c392fcfecea11952cb to your computer and use it in GitHub Desktop.
Save schierlm/bcc2961056d735c392fcfecea11952cb to your computer and use it in GitHub Desktop.
Short sample using geotools for Java to determine timezone of Busingen (using http://efele.net/maps/tz/world/)
package tzshapeparser;
import java.io.*;
import java.util.*;
import org.geotools.data.*;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.feature.*;
import org.geotools.geometry.jts.*;
import org.opengis.feature.simple.*;
import com.vividsolutions.jts.geom.*;
public class Main {
// http://efele.net/maps/tz/world/
public static void main(String[] args) throws Exception {
File file = new File("world/tz_world.shp");
com.vividsolutions.jts.geom.GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
Coordinate coord = new Coordinate(8.689098, 47.703077); // longitude, latitude
Point busingen = geometryFactory.createPoint(coord);
Map<String, Object> params = new HashMap<>();
params.put("url", file.toURI().toURL());
DataStore dataStore = DataStoreFinder.getDataStore(params);
SimpleFeatureSource featureSource = dataStore.getFeatureSource(dataStore.getTypeNames()[0]);
FeatureIterator<SimpleFeature> iterator = featureSource.getFeatures().features();
try {
while (iterator.hasNext()) {
SimpleFeature feature = iterator.next();
Geometry sourceGeometry = (Geometry) feature.getDefaultGeometryProperty().getValue();
if (sourceGeometry.contains(busingen)) {
System.out.println(feature.getProperty("TZID").getValue().toString());
}
}
} finally {
iterator.close();
}
}
}
<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>tzshapeparser</groupId>
<artifactId>TZShapeParser</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>TZShapeParser</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<geotools.version>14.3</geotools.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>${geotools.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<buildOutputDirectory>bin</buildOutputDirectory>
<downloadSources>true</downloadSources>
<testSourcesLast>true</testSourcesLast>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net repository</name>
<url>http://download.java.net/maven/2</url>
</repository>
<repository>
<id>osgeo</id>
<name>Open Source Geospatial Foundation Repository</name>
<url>http://download.osgeo.org/webdav/geotools/</url>
</repository>
</repositories>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment