Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
<kotlin.version>1.5.20</kotlin.version>
<kotlin.compiler.jvmTarget>11</kotlin.compiler.jvmTarget>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<geotools.version>29-SNAPSHOT</geotools.version>
<maven.deploy.skip>true</maven.deploy.skip>
</properties>

<dependencies>
Expand Down Expand Up @@ -65,8 +68,54 @@
<artifactId>log4j-core</artifactId>
<version>2.14.1</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</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-swing</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-grid</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-epsg-hsql</artifactId>
<version>${geotools.version}</version>
</dependency>
</dependencies>

<repositories>
<repository>
<id>osgeo</id>
<name>OSGeo Release Repository</name>
<url>https://repo.osgeo.org/repository/release/</url>
<snapshots><enabled>false</enabled></snapshots>
<releases><enabled>true</enabled></releases>
</repository>
<repository>
<id>osgeo-snapshot</id>
<name>OSGeo Snapshot Repository</name>
<url>https://repo.osgeo.org/repository/snapshot/</url>
<snapshots><enabled>true</enabled></snapshots>
<releases><enabled>false</enabled></releases>
</repository>
</repositories>
<build>
<plugins>
<!-- Kotlin -->
Expand Down Expand Up @@ -171,6 +220,17 @@
</execution>
</executions>
</plugin>
<!-- Geo Tools-->
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
329 changes: 187 additions & 142 deletions src/main/java/Graph/SegmentImporter.java

Large diffs are not rendered by default.

456 changes: 421 additions & 35 deletions src/main/java/Graph/SegmentMapper.java

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/main/java/Leaflet/GeoJsonPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public static void writeGeoJSON(String content, File outputFile) {

}


public static void writeGeoJSONLite(String content, File outputFile) {
try {
String json = geoJSONHead() + content + geoJSONTail();
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/Rides/Ride.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package Rides;

import Segments.Hexagon;
import Segments.Junction;
import Segments.Segment;
import Segments.Street;
Expand All @@ -11,6 +12,7 @@
import java.nio.file.Paths;
import java.util.*;


public class Ride {

public List<RideBucket> rideBuckets = new ArrayList<>();
Expand Down Expand Up @@ -72,6 +74,7 @@ public Ride(String pathToRide, HashMap<String, Segment> segmentMap, Raster raste
if (incidentPart) {
String[] lineArray = line.split(",", -1);
// skip incident if it is "nothing" or corrupted

if (line.endsWith(",,,,,") || line.length()<6 || lineArray[8].equals("0")||lineArray[8].equals("") || lineArray[8].equals("-5")) {
continue;
}
Expand Down Expand Up @@ -174,6 +177,25 @@ private void updateSegmentStatistics(RideBucket rideBucket, ArrayList<Incident>
System.exit(1);
}
}
} else if (rideBucket.segment instanceof Hexagon) {
((Hexagon)rideBucket.segment).numberOfRides++;
((Hexagon)rideBucket.segment).numberOfIncidents += incidentsOfOneSegment.size();
for (int i = 0; i < incidentsOfOneSegment.size(); i++) {
Incident thisIncident = incidentsOfOneSegment.get(i);
rideBucket.segment.incidents.add(thisIncident);
try {
if (thisIncident.scary) {
((Hexagon)rideBucket.segment).scaryIncidentTypes.merge(String.valueOf(thisIncident.incident),1,Integer::sum);
((Hexagon)rideBucket.segment).numberOfScaryIncidents++;
} else {
((Hexagon)rideBucket.segment).nonScaryIncidentTypes.merge(String.valueOf(thisIncident.incident),1,Integer::sum);
((Hexagon)rideBucket.segment).numberOfNonScaryIncidents++;
}
} catch (ArrayIndexOutOfBoundsException | NullPointerException aioobe) {
aioobe.printStackTrace();
System.exit(1);
}
}
} else {
if (!rideBucketsOfOneSegment.isEmpty()) {
if (towardsSouthWest > 0) {
Expand Down
24 changes: 19 additions & 5 deletions src/main/java/Rides/RideBucket.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package Rides;

import Segments.Hexagon;
import Segments.Junction;
import Segments.Segment;
import Segments.Street;
Expand Down Expand Up @@ -46,10 +47,14 @@ private Segment findSegment(HashMap<String, Segment> segmentMap, Raster raster,
double distanceToNearestJunction = Double.MAX_VALUE;
Street nearestStreet = null;
double distanceToNearestStreet = Double.MAX_VALUE;
Hexagon nearestHexagon = null;
double distanceToNearestHexagon = Double.MAX_VALUE;
Location location = new Location(lat, lon);
System.out.println(location);
System.out.println(raster.getNumberOfExistingRasterEntries());
// contains all segments that are near the RideBucket
List<ImmutablePair<String, String>> segmentCandidates = raster.getSubscriptionIdsInRasterEntryForPublisherLocation(location);

System.out.println(segmentCandidates);
// loop through all segment candidates and find the segment in which the RideBucket lies
// Junction beats Street if RideBucket lies in both
for (int i = 0; i < segmentCandidates.size(); i++) {
Expand All @@ -70,27 +75,36 @@ private Segment findSegment(HashMap<String, Segment> segmentMap, Raster raster,
distanceToNearestStreet = distance;

}
} else if (actualSegment instanceof Hexagon && distance <= distanceToNearestHexagon) {
if (distance <= MATCH_THRESHOLD) {
nearestHexagon = (Hexagon) actualSegment;
distanceToNearestHexagon = distance;
}
}
}

if (nearestJunction == null && nearestStreet == null && segmentCandidates.size() > 0) {
if (nearestJunction == null && nearestStreet == null && nearestHexagon == null && segmentCandidates.size() > 0) {
matchedToSegment = false;
}
// return nearest segment
if (distanceToNearestJunction <= distanceToNearestStreet) {
if (distanceToNearestJunction <= distanceToNearestStreet && distanceToNearestJunction <= distanceToNearestHexagon) {
return nearestJunction;
} else {
} else if (distanceToNearestHexagon <= distanceToNearestStreet && distanceToNearestHexagon <= distanceToNearestJunction) {
return nearestHexagon;
}
else {
return nearestStreet;
}
}

private double calculateDistanceFromPointToPolygon(Segment actualSegment, double lat, double lon) {
double[] polyLats = actualSegment.poly_vertices_latsArray;
double[] polyLons = actualSegment.poly_vertices_lonsArray;
Coordinate[] polygonCoordinates = new Coordinate[polyLats.length];
Coordinate[] polygonCoordinates = new Coordinate[polyLats.length+1]; //+1
for (int j = 0; j < polyLats.length; j++) {
polygonCoordinates[j] = new Coordinate(polyLats[j], polyLons[j]);
}
polygonCoordinates[polyLats.length] = polygonCoordinates[0];
Polygon polygon = new GeometryFactory().createPolygon(polygonCoordinates);
Point point = new GeometryFactory().createPoint(new Coordinate(lat, lon));
DistanceOp distanceOp = new DistanceOp(polygon, point);
Expand Down
157 changes: 157 additions & 0 deletions src/main/java/Segments/Hexagon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package Segments;

import geobroker.Geofence;
import geobroker.Location;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Hexagon extends Segment implements Comparable<Hexagon>{
public int numberOfRides, numberOfIncidents, numberOfScaryIncidents, numberOfNonScaryIncidents;
public int clopa, spiot, nlorh, ssho, tailgating, near_dooring, dao, other;
public HashMap<String, Integer> scaryIncidentTypes, nonScaryIncidentTypes = new HashMap<>();
public double dangerousnessScore;


public Hexagon(String id, double[] lats, double[] lons, double[] polyLats, double[] polyLons) {
this.id = id;
this.lats = lats;
this.lons = lons;
this.poly_vertices_latsArray = polyLats;
this.poly_vertices_lonsArray = polyLons;
List<Location> locations = new ArrayList<>();
for (int i = 0; i < polyLats.length-2; i++) {
locations.add(new Location(polyLats[i],polyLons[i]));
}
this.geofence = Geofence.Companion.polygon(locations);
scaryIncidentTypes = new HashMap<>();
scaryIncidentTypes.put("-2",0);
scaryIncidentTypes.put("1",0);
scaryIncidentTypes.put("2",0);
scaryIncidentTypes.put("3",0);
scaryIncidentTypes.put("4",0);
scaryIncidentTypes.put("5",0);
scaryIncidentTypes.put("6",0);
scaryIncidentTypes.put("7",0);
scaryIncidentTypes.put("8",0);
nonScaryIncidentTypes = new HashMap<>();
nonScaryIncidentTypes.put("-2",0);
nonScaryIncidentTypes.put("1",0);
nonScaryIncidentTypes.put("2",0);
nonScaryIncidentTypes.put("3",0);
nonScaryIncidentTypes.put("4",0);
nonScaryIncidentTypes.put("5",0);
nonScaryIncidentTypes.put("6",0);
nonScaryIncidentTypes.put("7",0);
nonScaryIncidentTypes.put("8",0);
}

public Double getScore() {
if (String.valueOf(this.dangerousnessScore).equals("NaN")) {
return -1.0;
} else {
return this.dangerousnessScore;
}
}

public String toString() {
StringBuilder result = new StringBuilder();

result.append("HEXAGON:");
result.append("|score:").append(dangerousnessScore);
result.append("|id:").append(id);
result.append("|coordinates:[");
for (int i = 0; i < lats.length; i++) {
result.append(lats[i]).append(",").append(lons[i]).append(" ");
}
result.append(lats[lats.length-1]).append(",").append((lons[lons.length-1])).append("]");
result.append(super.toString());
return result.toString();
}

public void appendFeatures(StringBuilder result){
result.append("{\"type\":\"Feature\",\"id\":\"").append(id)
.append("\",\"properties\":{\"type\":\"Hexagon\",")
.append("\n\"score\":").append(getScore())
.append(",\n\"incidents\":").append((numberOfNonScaryIncidents + numberOfScaryIncidents))
.append(",\n\"scary incidents\":").append((numberOfScaryIncidents))
.append(",\n\"non-scary incidents\":").append((numberOfNonScaryIncidents))
.append(",\n\"rides\":").append(numberOfRides)
.append(",\n\"clopa\":").append((nonScaryIncidentTypes.get("-2") + scaryIncidentTypes.get("-2") + nonScaryIncidentTypes.get("1") + scaryIncidentTypes.get("1") + clopa))
.append(",\n\"spiot\":").append((nonScaryIncidentTypes.get("2") + scaryIncidentTypes.get("2")+spiot))
.append(",\n\"nlorh\":").append((nonScaryIncidentTypes.get("3") + scaryIncidentTypes.get("3")+nlorh))
.append(",\n\"ssho\":").append((nonScaryIncidentTypes.get("4") + scaryIncidentTypes.get("4")+ssho))
.append(",\n\"tailgating\":").append((nonScaryIncidentTypes.get("5") + scaryIncidentTypes.get("5")+tailgating))
.append(",\n\"near-dooring\":").append((nonScaryIncidentTypes.get("6") + scaryIncidentTypes.get("6")+near_dooring))
.append(",\n\"dao\":").append((nonScaryIncidentTypes.get("7") + scaryIncidentTypes.get("7")+dao))
.append(",\n\"other\":").append((nonScaryIncidentTypes.get("8") + scaryIncidentTypes.get("8")+other));
}
public String detailJson() {
StringBuilder result = new StringBuilder();
appendFeatures(result);
//Lats
result.append(",\n\"lats\":[");
for (int i = 0; i < lats.length; i++){
result.append(lats[i]);
if (i != lats.length - 1){
result.append(",");
}
}
result.append("]");
//Lons
result.append(",\n\"lons\":[");
for (int i = 0; i < lons.length; i++){
result.append(lons[i]);
if (i != lons.length - 1){
result.append(",");
}
}
result.append("]");

//Poly_lats
result.append(",\n\"poly lats\":[");
for (int i = 0; i < poly_vertices_latsArray.length; i++){
result.append(poly_vertices_latsArray[i]);
if (i != poly_vertices_latsArray.length - 1){
result.append(",");
}
}
result.append("]");
//Poly lons
result.append(",\n\"poly lons\":[");
for (int i = 0; i < poly_vertices_lonsArray.length; i++){
result.append(poly_vertices_lonsArray[i]);
if (i != poly_vertices_lonsArray.length - 1){
result.append(",");
}
}
result.append("]");
result.append(super.toGeoJson());
result.append("},\n\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[");
for (int i = 0; i < poly_vertices_latsArray.length-1; i++) {
result.append("[").append(poly_vertices_lonsArray[i]).append(",").append(poly_vertices_latsArray[i]).append("],");
}
result.append("[").append(poly_vertices_lonsArray[poly_vertices_lonsArray.length-1]).append(",").append(poly_vertices_latsArray[poly_vertices_latsArray.length-1]).append("]]]}}");
return result.toString();
}
public String toGeoJson() {
StringBuilder result = new StringBuilder();
appendFeatures(result);
result.append(super.toGeoJson())
.append("},\n\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[");

for (int i = 0; i < poly_vertices_latsArray.length-1; i++) {
result.append("[").append(poly_vertices_lonsArray[i]).append(",").append(poly_vertices_latsArray[i]).append("],");
}
result.append("[").append(poly_vertices_lonsArray[poly_vertices_lonsArray.length-1]).append(",").append(poly_vertices_latsArray[poly_vertices_latsArray.length-1]).append("]]]}}");

return result.toString();
}

@Override
public int compareTo(@NotNull Hexagon o) {
return this.getScore().compareTo(o.getScore());
}
}
Loading