Commit d57bdd5d authored by Martin Raifer's avatar Martin Raifer
Browse files

upgrade to oshdb 0.4.0-RC

and remove some obsolete examples
parent 1bf5e15b
<?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>examples</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<parent>
<groupId>org.heigit.bigspatialdata</groupId>
<artifactId>oshdb-examples</artifactId>
<version>0.4.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.heigit.bigspatialdata</groupId>
<artifactId>oshdb</artifactId>
<version>${oshdb.version}</version>
</dependency>
<dependency>
<groupId>com.vividsolutions</groupId>
<artifactId>jts-core</artifactId>
<version>1.14.0</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-main</artifactId>
<version>${geotools.version}</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package org.heigit.bigspatialdata.oshdb.examples.activity;
import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.Polygon;
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;
public class ActivityIndicator {
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);
BoundingBox dimensions = xy.getCellDimensions(cell.getId());
Envelope e = new Envelope(
dimensions.minLon,
dimensions.maxLon,
dimensions.minLat,
dimensions.maxLat);
// 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();
}
}
Map<Long, Map<Long, Long>> cellTimestampActcivity = new TreeMap<>(); // cellId,
// Timestamp,
// Indicator
}
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());
// 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 timestampActivity;
}).reduce(Collections.emptyMap(),(partial, b) ->
{
Map<Long, Long> sum = new TreeMap<>();
sum.putAll(partial);
for(Map.Entry<Long, Long> entry : b.entrySet()){
Long activity = partial.get(entry.getKey());
if(activity==null){
activity = entry.getValue();
}
else{
activity= Long.valueOf((activity.longValue()+entry.getValue().longValue()));
}
sum.put(entry.getKey(), activity);
}
//
return sum;
}
);
// System.out.println(superresult);
// superresult.entrySet().forEach(entry -> {System.out.println(formatter.format(new Date(entry.getKey()*1000))+ ";" + entry.getValue());});
return superresult;
// } catch (
//
// SQLException e) { // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
}
package org.heigit.bigspatialdata.oshdb.examples.activity;
import java.io.FileWriter;
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.Optional;
import java.util.TimeZone;
import java.util.TreeMap;
import java.util.stream.Collectors;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.geotools.geometry.jts.JTS;
import org.heigit.bigspatialdata.oshdb.grid.GridOSHEntity;
import org.heigit.bigspatialdata.oshdb.index.XYGridTree;
import org.heigit.bigspatialdata.oshdb.osh.*;
import org.heigit.bigspatialdata.oshdb.osm.*;
import org.heigit.bigspatialdata.oshdb.util.BoundingBox;
import org.heigit.bigspatialdata.oshdb.util.CellId;
import org.heigit.bigspatialdata.oshdb.util.tagInterpreter.TagInterpreter;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.MultiPolygon;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.io.ParseException;
public class ActivityIndicatorFromPolygonBuildings {
/**
* Computes an Activity Indicator for buildings inside a polygons. This
* activity indicator counts all changes of building objects,which are
* geometry changes + tag changes (way versions) + tag and relation_member
* changes (relation versions)
*
* @param conn
* connection object to OSHDB e.g h2
* @param polygons
* to define analyis area
* @param ti
* TagInterpreter to decide whether to build polygons or linestrings
* @return Map<Long,Long> with timestamps and counts
* @throws ClassNotFoundException
* @throws ParseException
* @throws IOException
*/
public Map<Pair<Integer, Long>, Long> execute(Connection conn, List<MultiPolygon> polygons, TagInterpreter ti)
throws ClassNotFoundException, ParseException, IOException {
Class.forName("org.h2.Driver");
// FileWriter fw = new FileWriter("allbuildingversions.csv");
// create list of timestamps to analyze
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() / 1000);
} catch (java.text.ParseException e) {
System.err.println("basdoawrd");
}
;
}
}
Collections.sort(timestamps, Collections.reverseOrder());
// create BBOX to query gridcells as CellId-Objects, which contain
// (zoomlevel,id)
Double minLon = Double.MAX_VALUE;
Double maxLon = Double.MIN_VALUE;
Double minLat = Double.MAX_VALUE;
Double maxLat = Double.MIN_VALUE;
for (MultiPolygon inputPolygon: polygons) {
minLon = Double.min(JTS.toEnvelope(inputPolygon).getMinX(), minLon);
maxLon = Double.max(JTS.toEnvelope(inputPolygon).getMaxX(), maxLon);
minLat = Double.min(JTS.toEnvelope(inputPolygon).getMinY(), minLat);
maxLat = Double.max(JTS.toEnvelope(inputPolygon).getMaxY(), maxLat);
}
BoundingBox inputBbox = new BoundingBox(minLon, maxLon, minLat, maxLat);
XYGridTree grid = new XYGridTree(12);
final List<CellId> cellIds = new ArrayList<>();
grid.bbox2CellIds(inputBbox, true).forEach(cellIds::add);
// start processing in parallel all grid cells that relate to the input
// polygons BBOX
Map<Pair<Integer, Long>, Long> superresult = cellIds.parallelStream().flatMap(cellId -> {
//<<<<<<< HEAD
// try (
// final PreparedStatement pstmt = conn.prepareStatement(
// // union (select data from grid_relation where level = ?1 and id = ?2)
//=======
try (final PreparedStatement pstmt = conn.prepareStatement(
//>>>>>>> refs/remotes/origin/all-tiles-at-once
"(select data from grid_way where level = ?1 and id = ?2) union (select data from grid_relation where level = ?1 and id = ?2)")) {
pstmt.setInt(1, cellId.getZoomLevel());
pstmt.setLong(2, cellId.getId());
// each time 2 gridCells (1 grid_way and 1 grid_relation) are queried
// and put into a stream
try (final ResultSet rst2 = pstmt.executeQuery()) {
List<GridOSHEntity> cells = new LinkedList<>();
while (rst2.next()) {
final ObjectInputStream ois = new ObjectInputStream(rst2.getBinaryStream(1));
cells.add((GridOSHEntity) ois.readObject());
// final GridOSHEntity gridCell = (GridOSHEntity) ois.readObject();
// return gridCell;
}