Search in sources :

Example 91 with Coordinate

use of com.vividsolutions.jts.geom.Coordinate in project Osmand by osmandapp.

the class JtsAdapter method ptsToGeomCmds.

/**
 * <p>Convert a {@link Point} or {@link MultiPoint} geometry to a list of MVT geometry drawing commands. See
 * <a href="https://github.com/mapbox/vector-tile-spec">vector-tile-spec</a>
 * for details.</p>
 *
 * <p>WARNING: The value of the {@code cursor} parameter is modified as a result of calling this method.</p>
 *
 * @param geom input of type {@link Point} or {@link MultiPoint}. Type is NOT checked and expected to be correct.
 * @param cursor modified during processing to contain next MVT cursor position
 * @return list of commands
 */
private static List<Integer> ptsToGeomCmds(final Geometry geom, final Vec2d cursor) {
    // Guard: empty geometry coordinates
    final Coordinate[] geomCoords = geom.getCoordinates();
    if (geomCoords.length <= 0) {
        Collections.emptyList();
    }
    /**
     * Tile commands and parameters
     */
    final List<Integer> geomCmds = new ArrayList<>(geomCmdBuffLenPts(geomCoords.length));
    /**
     * Holds next MVT coordinate
     */
    final Vec2d mvtPos = new Vec2d();
    /**
     * Length of 'MoveTo' draw command
     */
    int moveCmdLen = 0;
    // Insert placeholder for 'MoveTo' command header
    geomCmds.add(0);
    Coordinate nextCoord;
    for (int i = 0; i < geomCoords.length; ++i) {
        nextCoord = geomCoords[i];
        mvtPos.set(nextCoord.x, nextCoord.y);
        // Ignore duplicate MVT points
        if (i == 0 || !equalAsInts(cursor, mvtPos)) {
            ++moveCmdLen;
            moveCursor(cursor, geomCmds, mvtPos);
        }
    }
    if (moveCmdLen <= GeomCmdHdr.CMD_HDR_LEN_MAX) {
        // Write 'MoveTo' command header to first index
        geomCmds.set(0, GeomCmdHdr.cmdHdr(GeomCmd.MoveTo, moveCmdLen));
        return geomCmds;
    } else {
        // Invalid geometry, need at least 1 'MoveTo' value to make points
        return Collections.emptyList();
    }
}
Also used : Coordinate(com.vividsolutions.jts.geom.Coordinate) ArrayList(java.util.ArrayList) Vec2d(com.wdtinc.mapbox_vector_tile.util.Vec2d) Point(com.vividsolutions.jts.geom.Point) MultiPoint(com.vividsolutions.jts.geom.MultiPoint)

Example 92 with Coordinate

use of com.vividsolutions.jts.geom.Coordinate in project Osmand by osmandapp.

the class JtsAdapter method linesToGeomCmds.

/**
 * <p>Convert a {@link LineString} or {@link Polygon} to a list of MVT geometry drawing commands.
 * A {@link MultiLineString} or {@link MultiPolygon} can be encoded by calling this method multiple times.</p>
 *
 * <p>See <a href="https://github.com/mapbox/vector-tile-spec">vector-tile-spec</a> for details.</p>
 *
 * <p>WARNING: The value of the {@code cursor} parameter is modified as a result of calling this method.</p>
 *
 * @param geom input of type {@link LineString} or {@link Polygon}. Type is NOT checked and expected to be correct.
 * @param closeEnabled whether a 'ClosePath' command should terminate the command list
 * @param cursor modified during processing to contain next MVT cursor position
 * @param minLineToLen minimum allowed length for LineTo command.
 * @return list of commands
 */
private static List<Integer> linesToGeomCmds(final Geometry geom, final boolean closeEnabled, final Vec2d cursor, final int minLineToLen) {
    final Coordinate[] geomCoords = geom.getCoordinates();
    // Check geometry for repeated end points
    final int repeatEndCoordCount = countCoordRepeatReverse(geomCoords);
    final int minExpGeomCoords = geomCoords.length - repeatEndCoordCount;
    // Guard/Optimization: Not enough geometry coordinates for a line
    if (minExpGeomCoords < 2) {
        Collections.emptyList();
    }
    /**
     * Tile commands and parameters
     */
    final List<Integer> geomCmds = new ArrayList<>(geomCmdBuffLenLines(minExpGeomCoords, closeEnabled));
    /**
     * Holds next MVT coordinate
     */
    final Vec2d mvtPos = new Vec2d();
    // Initial coordinate
    Coordinate nextCoord = geomCoords[0];
    mvtPos.set(nextCoord.x, nextCoord.y);
    // Encode initial 'MoveTo' command
    geomCmds.add(GeomCmdHdr.cmdHdr(GeomCmd.MoveTo, 1));
    moveCursor(cursor, geomCmds, mvtPos);
    /**
     * Index of 'LineTo' 'command header'
     */
    final int lineToCmdHdrIndex = geomCmds.size();
    // Insert placeholder for 'LineTo' command header
    geomCmds.add(0);
    /**
     * Length of 'LineTo' draw command
     */
    int lineToLength = 0;
    for (int i = 1; i < minExpGeomCoords; ++i) {
        nextCoord = geomCoords[i];
        mvtPos.set(nextCoord.x, nextCoord.y);
        // Ignore duplicate MVT points in sequence
        if (!equalAsInts(cursor, mvtPos)) {
            ++lineToLength;
            moveCursor(cursor, geomCmds, mvtPos);
        }
    }
    if (lineToLength >= minLineToLen && lineToLength <= GeomCmdHdr.CMD_HDR_LEN_MAX) {
        // Write 'LineTo' 'command header'
        geomCmds.set(lineToCmdHdrIndex, GeomCmdHdr.cmdHdr(GeomCmd.LineTo, lineToLength));
        if (closeEnabled) {
            geomCmds.add(GeomCmdHdr.closePathCmdHdr());
        }
        return geomCmds;
    } else {
        // Invalid geometry, need at least 1 'LineTo' value to make a Multiline or Polygon
        return Collections.emptyList();
    }
}
Also used : Coordinate(com.vividsolutions.jts.geom.Coordinate) ArrayList(java.util.ArrayList) Vec2d(com.wdtinc.mapbox_vector_tile.util.Vec2d) Point(com.vividsolutions.jts.geom.Point) MultiPoint(com.vividsolutions.jts.geom.MultiPoint)

Example 93 with Coordinate

use of com.vividsolutions.jts.geom.Coordinate in project Osmand by osmandapp.

the class MvtReader method readPoints.

/**
 * Create {@link Point} or {@link MultiPoint} from MVT geometry drawing commands.
 *
 * @param geomFactory creates JTS geometry
 * @param geomCmds    contains MVT geometry commands
 * @param cursor      contains current MVT extent position
 * @return JTS geometry or null on failure
 */
private static Geometry readPoints(GeometryFactory geomFactory, List<Integer> geomCmds, Vec2d cursor) {
    // Guard: must have header
    if (geomCmds.isEmpty()) {
        return null;
    }
    /**
     * Geometry command index
     */
    int i = 0;
    // Read command header
    final int cmdHdr = geomCmds.get(i++);
    final int cmdLength = GeomCmdHdr.getCmdLength(cmdHdr);
    final GeomCmd cmd = GeomCmdHdr.getCmd(cmdHdr);
    // Guard: command type
    if (cmd != GeomCmd.MoveTo) {
        return null;
    }
    // Guard: minimum command length
    if (cmdLength < 1) {
        return null;
    }
    // (require header and at least 1 value * 2 params)
    if (cmdLength * GeomCmd.MoveTo.getParamCount() + 1 > geomCmds.size()) {
        return null;
    }
    final CoordinateSequence coordSeq = geomFactory.getCoordinateSequenceFactory().create(cmdLength, 2);
    int coordIndex = 0;
    Coordinate nextCoord;
    while (i < geomCmds.size() - 1) {
        cursor.add(ZigZag.decode(geomCmds.get(i++)), ZigZag.decode(geomCmds.get(i++)));
        nextCoord = coordSeq.getCoordinate(coordIndex++);
        nextCoord.setOrdinate(0, cursor.x);
        nextCoord.setOrdinate(1, cursor.y);
    }
    return coordSeq.size() == 1 ? geomFactory.createPoint(coordSeq) : geomFactory.createMultiPoint(coordSeq);
}
Also used : CoordinateSequence(com.vividsolutions.jts.geom.CoordinateSequence) Coordinate(com.vividsolutions.jts.geom.Coordinate) GeomCmd(com.wdtinc.mapbox_vector_tile.encoding.GeomCmd) Point(com.vividsolutions.jts.geom.Point) MultiPoint(com.vividsolutions.jts.geom.MultiPoint)

Example 94 with Coordinate

use of com.vividsolutions.jts.geom.Coordinate in project eol-globi-data by jhpoelen.

the class StudyImporterForRaymond method calculateCentroidOfBBox.

protected static LatLng calculateCentroidOfBBox(double left, double top, double right, double bottom) {
    LatLng latLng;
    if (left == right && top == bottom) {
        latLng = new LatLng(top, left);
    } else {
        Coordinate[] points = { GeoUtil.getCoordinate(top, left), GeoUtil.getCoordinate(top, right), GeoUtil.getCoordinate(bottom, right), GeoUtil.getCoordinate(bottom, left), GeoUtil.getCoordinate(top, left) };
        GeometryFactory geometryFactory = new GeometryFactory();
        Polygon polygon = geometryFactory.createPolygon(points);
        Point centroid = polygon.getCentroid();
        latLng = new LatLng(centroid.getCoordinate().y, centroid.getCoordinate().x);
    }
    return latLng;
}
Also used : GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) Coordinate(com.vividsolutions.jts.geom.Coordinate) LatLng(org.eol.globi.geo.LatLng) Point(com.vividsolutions.jts.geom.Point) Polygon(com.vividsolutions.jts.geom.Polygon)

Example 95 with Coordinate

use of com.vividsolutions.jts.geom.Coordinate in project eol-globi-data by jhpoelen.

the class StudyImporterForSzoboszlai method importShapes.

protected Map<Integer, LatLng> importShapes() throws StudyImporterException {
    Map<Integer, LatLng> localityMap = new TreeMap<>();
    FileDataStore dataStore = null;
    try {
        InputStream shapeZipArchive = getDataset().getResource("shapes");
        File tmpFolder = new File(FileUtils.getTempDirectory(), UUID.randomUUID().toString());
        tmpFolder.deleteOnExit();
        unpackZip(shapeZipArchive, tmpFolder);
        dataStore = FileDataStoreFinder.getDataStore(new File(tmpFolder, "LocatPolygonsPoints.shp"));
        if (dataStore == null) {
            throw new StudyImporterException("failed to parse shapefiles");
        }
        FeatureReader<SimpleFeatureType, SimpleFeature> featureReader = dataStore.getFeatureReader();
        while (featureReader.hasNext()) {
            SimpleFeature feature = featureReader.next();
            Object geom = feature.getAttribute("the_geom");
            if (geom instanceof Point) {
                Coordinate coordinate = ((Point) geom).getCoordinate();
                Object localNum = feature.getAttribute("LocatNum");
                if (localNum instanceof Integer) {
                    localityMap.put((Integer) localNum, new LatLng(coordinate.y, coordinate.x));
                }
            }
        }
        featureReader.close();
    } catch (IOException e) {
        throw new StudyImporterException(e);
    } finally {
        if (dataStore != null) {
            dataStore.dispose();
        }
    }
    return localityMap;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) Point(com.vividsolutions.jts.geom.Point) IOException(java.io.IOException) TreeMap(java.util.TreeMap) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Coordinate(com.vividsolutions.jts.geom.Coordinate) LatLng(org.eol.globi.geo.LatLng) FileDataStore(org.geotools.data.FileDataStore) File(java.io.File)

Aggregations

Coordinate (com.vividsolutions.jts.geom.Coordinate)336 LineString (com.vividsolutions.jts.geom.LineString)70 Geometry (com.vividsolutions.jts.geom.Geometry)67 ArrayList (java.util.ArrayList)65 Test (org.junit.Test)60 Point (com.vividsolutions.jts.geom.Point)52 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)48 Polygon (com.vividsolutions.jts.geom.Polygon)30 StreetEdge (org.opentripplanner.routing.edgetype.StreetEdge)27 SimpleFeature (org.opengis.feature.simple.SimpleFeature)23 LinearRing (com.vividsolutions.jts.geom.LinearRing)22 Vertex (org.opentripplanner.routing.graph.Vertex)22 Envelope (com.vividsolutions.jts.geom.Envelope)21 MultiLineString (com.vividsolutions.jts.geom.MultiLineString)20 Edge (org.opentripplanner.routing.graph.Edge)19 IntersectionVertex (org.opentripplanner.routing.vertextype.IntersectionVertex)19 CoordinateSequence (com.vividsolutions.jts.geom.CoordinateSequence)14 TransitStop (org.opentripplanner.routing.vertextype.TransitStop)13 File (java.io.File)11 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)11