Search in sources :

Example 1 with SpatialDataItem

use of io.opentraffic.engine.data.SpatialDataItem in project traffic-engine by opentraffic.

the class OSMDataStore method createOffsetGeom.

private Geometry createOffsetGeom(long id) {
    SpatialDataItem segment = streetSegments.getById(id);
    Geometry geom = streetSegments.getById(id).getGeometry();
    if (!((StreetSegment) segment).oneway) {
        Coordinate[] offsetCoords = ocb.getOffsetCurve(geom.getCoordinates(), -0.000025);
        geom = geometryFactory.createLineString(offsetCoords);
    }
    return geom;
}
Also used : SpatialDataItem(io.opentraffic.engine.data.SpatialDataItem)

Example 2 with SpatialDataItem

use of io.opentraffic.engine.data.SpatialDataItem in project traffic-engine by opentraffic.

the class OSMUtils method toShapefile.

public static void toShapefile(List<SpatialDataItem> segs, String filename) throws SchemaException, IOException {
    final SimpleFeatureType TYPE = DataUtilities.createType("Location", "the_geom:LineString:srid=4326," + "name:String");
    System.out.println("TYPE:" + TYPE);
    List<SimpleFeature> features = new ArrayList<SimpleFeature>();
    /*
         * GeometryFactory will be used to create the geometry attribute of each feature,
         * using a Point object for the location.
         */
    GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
    SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
    for (SpatialDataItem seg : segs) {
        featureBuilder.add(seg.getGeometry());
        featureBuilder.add(seg.id);
        SimpleFeature feature = featureBuilder.buildFeature(null);
        features.add(feature);
    }
    File newFile = new File(filename);
    ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
    Map<String, Serializable> params = new HashMap<String, Serializable>();
    params.put("url", newFile.toURI().toURL());
    params.put("create spatial index", Boolean.TRUE);
    ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
    /*
         * TYPE is used as a template to describe the file contents
         */
    newDataStore.createSchema(TYPE);
    ContentFeatureSource cfs = newDataStore.getFeatureSource();
    if (cfs instanceof SimpleFeatureStore) {
        SimpleFeatureStore featureStore = (SimpleFeatureStore) cfs;
        SimpleFeatureCollection collection = new ListFeatureCollection(TYPE, features);
        try {
            featureStore.addFeatures(collection);
        } catch (Exception problem) {
            problem.printStackTrace();
        } finally {
        }
    }
}
Also used : Serializable(java.io.Serializable) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) ShapefileDataStore(org.geotools.data.shapefile.ShapefileDataStore) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SpatialDataItem(io.opentraffic.engine.data.SpatialDataItem) LineString(com.vividsolutions.jts.geom.LineString) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SchemaException(org.geotools.feature.SchemaException) IOException(java.io.IOException) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) ListFeatureCollection(org.geotools.data.collection.ListFeatureCollection) ShapefileDataStoreFactory(org.geotools.data.shapefile.ShapefileDataStoreFactory) File(java.io.File) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder) ContentFeatureSource(org.geotools.data.store.ContentFeatureSource)

Example 3 with SpatialDataItem

use of io.opentraffic.engine.data.SpatialDataItem in project traffic-engine by opentraffic.

the class StreetDataStore method delete.

@Override
public void delete(List<SpatialDataItem> objs) {
    List<SpatialDataItem> segments = new ArrayList<SpatialDataItem>();
    for (SpatialDataItem obj : objs) {
        Tuple3 segmentId = ((StreetSegment) obj).getSegmentId();
        if (!segmentIndex.containsKey(segmentId))
            continue;
        segments.add(obj);
        segmentIndex.remove(segmentId);
    }
    super.delete(segments);
}
Also used : StreetSegment(io.opentraffic.engine.geom.StreetSegment) Tuple3(org.mapdb.Fun.Tuple3) SpatialDataItem(io.opentraffic.engine.data.SpatialDataItem)

Example 4 with SpatialDataItem

use of io.opentraffic.engine.data.SpatialDataItem in project traffic-engine by opentraffic.

the class StreetDataStore method save.

@Override
public void save(List<SpatialDataItem> objs) {
    List<SpatialDataItem> segments = new ArrayList<SpatialDataItem>();
    for (SpatialDataItem obj : objs) {
        Tuple3 segmentId = ((StreetSegment) obj).getSegmentId();
        if (segmentIndex.containsKey(segmentId))
            continue;
        segments.add(obj);
        segmentIndex.put(segmentId, obj.id);
    }
    super.save(segments);
}
Also used : StreetSegment(io.opentraffic.engine.geom.StreetSegment) Tuple3(org.mapdb.Fun.Tuple3) SpatialDataItem(io.opentraffic.engine.data.SpatialDataItem)

Example 5 with SpatialDataItem

use of io.opentraffic.engine.data.SpatialDataItem in project traffic-engine by opentraffic.

the class OSMDataStore method addOsm.

private OSMArea addOsm(Fun.Tuple2<Integer, Integer> tile, Envelope env, OSM osm, Boolean keepCompleteGeometries) {
    String placeName = null;
    Long placePop = null;
    for (Entry<Long, Node> entry : osm.nodes.entrySet()) {
        Long id = entry.getKey();
        Node node = entry.getValue();
        if (id == 259009337) {
            try {
                long pop = Long.parseLong(node.getTag("population"));
                if (placePop == null || placePop < pop) {
                    placePop = pop;
                    placeName = node.getTag("name");
                }
            } catch (Exception e) {
            }
        }
    }
    List<StreetSegment> segments = getStreetSegments(osm);
    List<SpatialDataItem> segmentItems = new ArrayList<>();
    List<SpatialDataItem> triplineItems = new ArrayList<>();
    for (StreetSegment segment : segments) {
        if (streetSegments.contains(segment.getSegmentId()))
            continue;
        if (segment.length > MIN_SEGMENT_LEN) {
            LengthIndexedLine lengthIndexedLine = new LengthIndexedLine(segment.getGeometry());
            double scale = (lengthIndexedLine.getEndIndex() - lengthIndexedLine.getStartIndex()) / segment.length;
            List<TripLine> tripLines = new ArrayList<TripLine>();
            tripLines.add(createTripLine(segment, 1, lengthIndexedLine, (OSMDataStore.INTERSECTION_MARGIN_METERS) * scale, OSMDataStore.INTERSECTION_MARGIN_METERS));
            tripLines.add(createTripLine(segment, 2, lengthIndexedLine, ((segment.length - OSMDataStore.INTERSECTION_MARGIN_METERS) * scale), segment.length - OSMDataStore.INTERSECTION_MARGIN_METERS));
            for (TripLine tripLine : tripLines) {
                triplineItems.add(tripLine);
            }
        } else {
            jumperDataStore.addJumper(new Jumper(segment));
        }
        if (!keepCompleteGeometries)
            segment.truncateGeometry();
        segmentItems.add(segment);
    }
    streetSegments.save(segmentItems);
    jumperDataStore.save();
    triplines.save(triplineItems);
    long zoneOffset = timeZoneConverter.getOffsetForCoord(env.centre());
    OSMArea osmArea = new OSMArea(osmAreaIds.getNextId(), tile.a, tile.b, Z_INDEX, placeName, placePop, zoneOffset, env);
    osmAreas.put(tile, osmArea);
    db.commit();
    System.out.println("Loaded OSM " + tile.a + ", " + tile.b);
    if (placeName != null)
        System.out.println("\t" + placeName + ", " + placePop);
    return osmArea;
}
Also used : StreetSegment(io.opentraffic.engine.geom.StreetSegment) Node(com.conveyal.osmlib.Node) SpatialDataItem(io.opentraffic.engine.data.SpatialDataItem) Jumper(io.opentraffic.engine.geom.Jumper) LengthIndexedLine(com.vividsolutions.jts.linearref.LengthIndexedLine) TripLine(io.opentraffic.engine.geom.TripLine)

Aggregations

SpatialDataItem (io.opentraffic.engine.data.SpatialDataItem)5 StreetSegment (io.opentraffic.engine.geom.StreetSegment)3 Tuple3 (org.mapdb.Fun.Tuple3)2 Node (com.conveyal.osmlib.Node)1 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)1 LineString (com.vividsolutions.jts.geom.LineString)1 LengthIndexedLine (com.vividsolutions.jts.linearref.LengthIndexedLine)1 Jumper (io.opentraffic.engine.geom.Jumper)1 TripLine (io.opentraffic.engine.geom.TripLine)1 File (java.io.File)1 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ListFeatureCollection (org.geotools.data.collection.ListFeatureCollection)1 ShapefileDataStore (org.geotools.data.shapefile.ShapefileDataStore)1 ShapefileDataStoreFactory (org.geotools.data.shapefile.ShapefileDataStoreFactory)1 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)1 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)1 ContentFeatureSource (org.geotools.data.store.ContentFeatureSource)1