Commit 6eb73aed authored by Moritz Schott's avatar Moritz Schott
Browse files

initial commit to move examples from core to here

parents
# Handle line endings automatically for files detected as text
# and leave all files detected as binary untouched.
* text=auto
#
# The above will handle all files NOT found below
#
# These files are text and should be normalized (Convert crlf => lf)
*.css text
*.df text
*.htm text
*.html text
*.java text
*.js text
*.json text
*.jsp text
*.jspf text
*.jspx text
*.properties text
*.sh text
*.tld text
*.txt text
*.tag text
*.tagx text
*.xml text
*.yml text
# These files are binary and should be left untouched
# (binary is a macro for -text -diff)
*.class binary
*.dll binary
*.ear binary
*.gif binary
*.ico binary
*.jar binary
*.jpg binary
*.jpeg binary
*.png binary
*.so binary
*.war binary
# Compiled class file
*.class
# Package Files #
*.jar
*.war
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
# maven generated target folder
**/target/
# eclipse stuff
.project
.classpath
.settings
# intellij stuff
*.iml
.idea
# netbeans stuff
nb-configuration.xml
nbactions*.xml
**/nbproject/
# gedit stuff
*~
# DB stuff
temp_*
*.mv.db
*.trace.db
{
"osmatrix-db": {"connection":"jdbc:postgresql://lemberg.geog.uni-heidelberg.de:5432/osmatrixhd", "user":"osmatrix", "password":"osmatrix2016"},
"osh-db": {"connection":"jdbc:h2:./heidelberg--2017-05-29", "user":"sa", "password":""},
"temp-db": {"connection": "jdbc:postgresql://lemberg.geog.uni-heidelberg.de:5432/osmatrixhd", "user":"osmatrix", "password":"osmatrix2016"},
"timestamps": {"start": "2012-01","end":"2012-12", "steps":"PM1"},
"bbox":[8.56982,8.79541,49.3504,49.4616],
"attributes": ["TotalNumberOfDrinkingFountains"]
}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<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.heigit.bigspatialdata</groupId>
<artifactId>oshdb-examples</artifactId>
<version>0.3.0-SNAPSHOT</version>
<name>HeiGIT OSHDb-Examples</name>
<url>http://www.geog.uni-heidelberg.de/gis/heigit_bigspatialdata.html</url>
<description/>
<licenses><!-- <license> <name>The Apache Software License, Version 2.0</name> <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license> -->
</licenses>
<developers><!-- https://maven.apache.org/pom.html#Developers -->
<developer>
<id>rtroilo</id>
<name>Rafael Troilo</name>
<email>rafael.troilo@uni-heidelberg.de</email>
</developer>
<developer>
<id>mraifer</id>
<name>Martin Raifer</name>
<email>martin.raifer@uni-heidelberg.de</email>
</developer>
<developer>
<id>mr.moehritz</id>
<name>Moriz Schott</name>
<email>m.schott@stud.uni-heidelberg.de</email>
</developer>
</developers>
<properties>
<geotools-version>18-RC1</geotools-version>
<oshdb-version>0.3.0-SNAPSHOT</oshdb-version>
</properties>
<dependencies>
<dependency>
<groupId>org.heigit.bigspatialdata</groupId>
<artifactId>oshdb-h2</artifactId>
<version>${oshdb-version}</version>
</dependency>
<dependency>
<groupId>org.heigit.bigspatialdata</groupId>
<artifactId>oshdb-api</artifactId>
<version>${oshdb-version}</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-grid</artifactId>
<version>${geotools-version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>${geotools-version}</version>
</dependency>
<dependency>
<groupId>org.geotools.jdbc</groupId>
<artifactId>gt-jdbc-postgis</artifactId>
<version>${geotools-version}</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.wololo</groupId>
<artifactId>jts2geojson</artifactId>
<version>0.10.0</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>libs-release</name>
<url>http://129.206.7.121:8081/artifactory/libs-release</url>
</repository>
<repository>
<snapshots />
<id>snapshots</id>
<name>libs-snapshot</name>
<url>http://129.206.7.121:8081/artifactory/libs-snapshot</url>
</repository>
</repositories>
</project>
package org.heigit.bigspatialdata.oshdb.examples.activity;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.TreeMap;
import org.geotools.geometry.jts.JTS;
import org.heigit.bigspatialdata.oshdb.grid.GridOSHNodes;
import org.heigit.bigspatialdata.oshdb.index.XYGrid;
import org.heigit.bigspatialdata.oshdb.index.XYGridTree;
import org.heigit.bigspatialdata.oshdb.osh.OSHNode;
import org.heigit.bigspatialdata.oshdb.osm.OSMNode;
import org.heigit.bigspatialdata.oshdb.util.BoundingBox;
import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.Polygon;
import mil.nga.giat.geowave.core.index.sfc.data.MultiDimensionalNumericData;
public class ActivityIndicator {
Map<Long, Map<Long, Long>> cellTimestampActcivity = new TreeMap<>(); // cellId,
// Timestamp,
// Indicator
public static void main(String[] args) throws ClassNotFoundException {
Class.forName("org.h2.Driver");
List<Long> timestamps = new ArrayList<>();
final SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
for (int year = 2016; year <= 2016; year++) {
for (int month = 1; month <= 12; month++) {
try {
timestamps.add(formatter.parse(String.format("%d%02d01", year, month)).getTime());
} catch (java.text.ParseException e) {
System.err.println("basdoawrd");
}
;
}
}
Collections.sort(timestamps, Collections.reverseOrder());
BoundingBox bbox = new BoundingBox(8.61, 8.76, 49.40, 49.41);
XYGridTree grid = new XYGridTree(12);
final List<Long> cellIds = new ArrayList<>();
grid.bbox2CellIds(bbox, false).forEach(cell -> {
if (cell.getZoomLevel() == 12) {
cellIds.add(cell.getId());
}
});
// connect to the "Big"DB
try (Connection conn = DriverManager.getConnection("jdbc:h2:tcp://localhost:9092/d:/eclipseNeon2Workspace/OSH-BigDB/core/hosmdb/resources/oshdb/heidelberg-ccbysa",
"sa", "")) {
cellIds.parallelStream().flatMap(cellId -> {
try (final PreparedStatement pstmt = conn
.prepareStatement("(select data from grid_node where level = ? and id = ?)")) {
pstmt.setInt(1, 12);
pstmt.setLong(2, cellId);
try (final ResultSet rst2 = pstmt.executeQuery()) {
List<GridOSHNodes> cells = new LinkedList<>();
while (rst2.next()) {
final ObjectInputStream ois = new ObjectInputStream(rst2.getBinaryStream(1));
cells.add((GridOSHNodes) ois.readObject());
}
return cells.stream();
}
} catch (IOException | SQLException | ClassNotFoundException e) {
e.printStackTrace();
return null;
}
}).map(oshCell -> {
GridOSHNodes cell = (GridOSHNodes) oshCell;
Map<Long, Map<Long, Long>> result = new TreeMap<>();
Map<Long, Long> timestampActivity = new TreeMap<>();
result.put(cell.getId(), timestampActivity);
Iterator<OSHNode> itr = cell.iterator();
while (itr.hasNext()) {
OSHNode osh = itr.next();
List<OSMNode> versions = new ArrayList<>();
osh.forEach(osm -> versions.add(osm));
int v = 0;
for (int i = 0; i < timestamps.size(); i++) {
long ts = timestamps.get(i);
long count = 0;
while (v < versions.size() && versions.get(v).getTimestamp() > ts) {
count++;
v++;
}
if (timestampActivity.containsKey(ts)) {
timestampActivity.put(ts, timestampActivity.get(ts) + count);
} else {
timestampActivity.put(ts, count);
}
if (v >= versions.size())
break;
}
}
XYGrid xy = new XYGrid(12);
MultiDimensionalNumericData dimensions = xy.getCellDimensions(cell.getId());
dimensions.getMinValuesPerDimension();
dimensions.getMaxValuesPerDimension();
Envelope e = new Envelope(
dimensions.getMinValuesPerDimension()[0],
dimensions.getMaxValuesPerDimension()[0],
dimensions.getMinValuesPerDimension()[1],
dimensions.getMaxValuesPerDimension()[1]);
// System.out.println(e);
Polygon p = JTS.toGeometry(e);
// System.out.println(p.toText());
StringBuilder sb = new StringBuilder();
for(Map.Entry<Long,Long> entry : timestampActivity.entrySet()){
sb.append(entry.getValue()).append(";");
}
System.out.printf("%s;%s\n",p.toText(),sb.toString());
//System.out.println(result);
return result;
}).count();
} catch (
SQLException e) { // TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package org.heigit.bigspatialdata.oshdb.examples.activity;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.TreeMap;
import org.geotools.geometry.jts.JTS;
import org.heigit.bigspatialdata.oshdb.grid.GridOSHNodes;
import org.heigit.bigspatialdata.oshdb.index.XYGridTree;
import org.heigit.bigspatialdata.oshdb.osh.OSHNode;
import org.heigit.bigspatialdata.oshdb.osm.OSMNode;
import org.heigit.bigspatialdata.oshdb.util.BoundingBox;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.MultiPolygon;
import com.vividsolutions.jts.io.ParseException;
public class ActivityIndicatorFromPolygon {
Map<Long, Map<Long, Long>> cellTimestampActcivity = new TreeMap<>(); // cellId,
// Timestamp,
// Indicator
public Map<Long,Long> execute(Connection conn, Geometry polygon) throws ClassNotFoundException, ParseException {
Class.forName("org.h2.Driver");
// GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( null );
// WKTReader wktReader = new WKTReader(geometryFactory);
// String polygonString = "POLYGON ((8.69282133333848961 49.41176730111411075, 8.69436717935256631 49.40753302724946394, 8.70713721164276855 49.40934771604860032, 8.71271569943269952 49.41089356206267524, 8.71842860861515767 49.41459015035720626, 8.71069937854477239 49.41351477921698176, 8.70081940619393279 49.41284267225433524, 8.69282133333848961 49.41176730111411075))";
// Polygon inputPolygon = (Polygon) wktReader.read(polygonString);
//Polygon inputEnvelope = (Polygon) inputPolygon.getEnvelope();
MultiPolygon inputPolygon = (MultiPolygon) polygon;
Double minLon = JTS.toEnvelope(inputPolygon).getMinX();
Double maxLon = JTS.toEnvelope(inputPolygon).getMaxX();
Double minLat = JTS.toEnvelope(inputPolygon).getMinY();
Double maxLat = JTS.toEnvelope(inputPolygon).getMaxY();
BoundingBox inputBbox = new BoundingBox(minLon, maxLon, minLat, maxLat);
//System.out.println(JTS.toEnvelope(inputPolygon));
List<Long> timestamps = new ArrayList<>();
final SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
for (int year = 2016; year <= 2016; year++) {
for (int month = 1; month <= 2; month++) {
try {
timestamps.add(formatter.parse(String.format("%d%02d01", year, month)).getTime()/1000);
} catch (java.text.ParseException e) {
System.err.println("basdoawrd");
}
;
}
}
Collections.sort(timestamps, Collections.reverseOrder());
//BoundingBox bbox = new BoundingBox(8.61, 8.76, 49.40, 49.41);
XYGridTree grid = new XYGridTree(12);
final List<Long> cellIds = new ArrayList<>();
grid.bbox2CellIds(inputBbox, false).forEach(cell -> {
if (cell.getZoomLevel() == 12) {
cellIds.add(cell.getId());
}
});
// connect to the "Big"DB
// try (Connection conn = DriverManager.getConnection("jdbc:h2:tcp://localhost/~/git/OSH-BigDB/core/oshdb-examples/src/main/resources/heidelberg-ccbysa",
// try (Connection conn = DriverManager.getConnection("jdbc:h2:tcp://localhost/d:/eclipseNeon2Workspace/OSH-BigDB/core/hosmdb/resources/oshdb/heidelberg-ccbysa",
// "sa", "")) {
Map <Long,Long> superresult = cellIds.parallelStream().flatMap(cellId -> {
try (final PreparedStatement pstmt = conn
.prepareStatement("(select data from grid_node where level = ? and id = ?)")) {
pstmt.setInt(1, 12);
pstmt.setLong(2, cellId);
try (final ResultSet rst2 = pstmt.executeQuery()) {
List<GridOSHNodes> cells = new LinkedList<>();
while (rst2.next()) {
final ObjectInputStream ois = new ObjectInputStream(rst2.getBinaryStream(1));
cells.add((GridOSHNodes) ois.readObject());
}
return cells.stream();
}
} catch (IOException | SQLException | ClassNotFoundException e) {
e.printStackTrace();
return null;
}
}).map(oshCell -> {
GridOSHNodes cell = (GridOSHNodes) oshCell;
Map<Long, Map<Long, Long>> result = new TreeMap<>();
Map<Long, Long> timestampActivity = new TreeMap<>();
result.put(cell.getId(), timestampActivity);
Iterator<OSHNode> itr = cell.iterator();
int counter = 0;
while (itr.hasNext()) {
OSHNode osh = itr.next();
//System.out.println(osh.getBoundingBox().getGeometry());
if (! osh.getBoundingBox().getGeometry().intersects(inputPolygon)) {
continue;
}
List<OSMNode> versions = new ArrayList<>();
for (OSMNode osm : osh) {
// System.out.println(osm);
if (osm.isVisible() && osm.getGeometry().intersects(inputPolygon))
{
versions.add(osm);
}
}
//osh.forEach(osm -> versions.add(osm));
List <OSMNode> numberOfAllVersions = osh.getVersions();
if(numberOfAllVersions.size()!=versions.size() && versions.size()> 0){
// System.out.println("Number of all Versions in OSH object vs number of all versions in Polygon: " + numberOfAllVersions.size() + " " + versions.size() + " VersionNummer: http://www.openstreetmap.org/node/" + osh.getId() + " isVisible: " + osh.getVersions().get(0).isVisible());
}
//
int v = 0;
for (int i = 0; i < timestamps.size(); i++) {
long ts = timestamps.get(i);
long count = 0;
while (v < versions.size() && versions.get(v).getTimestamp() > ts) {
count++;
v++;
}
if (i==0){
continue;
}
if (timestampActivity.containsKey(ts)) {
timestampActivity.put(ts, timestampActivity.get(ts) + count);
} else {
timestampActivity.put(ts, count);
}
if (v >= versions.size())
break;
}
++counter;
}
//System.out.println(counter);
// XYGrid xy = new XYGrid(12);
// MultiDimensionalNumericData dimensions = xy.getCellDimensions(cell.getId());