Search in sources :

Example 66 with Point

use of com.vividsolutions.jts.geom.Point in project tests by datanucleus.

the class JtsGeometryMappingTest method testGeometryMapping.

public void testGeometryMapping() throws SQLException, ParseException {
    if (!runTestsForDatastore()) {
        return;
    }
    Point point = (Point) wktReader.read("POINT(10 10)");
    point.setSRID(-1);
    SampleGeometry sampleGeometry;
    SampleGeometry sampleGeometry_read;
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        sampleGeometry = new SampleGeometry(1001, "Geometry (Point)", point);
        em.persist(sampleGeometry);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
    em = emf.createEntityManager();
    tx = em.getTransaction();
    try {
        tx.begin();
        sampleGeometry_read = (SampleGeometry) em.find(SampleGeometry.class, new Long(1001));
        assertEquals(sampleGeometry, sampleGeometry_read);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}
Also used : SampleGeometry(org.datanucleus.samples.jtsgeometry.SampleGeometry) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) SamplePoint(org.datanucleus.samples.jtsgeometry.SamplePoint) Point(com.vividsolutions.jts.geom.Point)

Example 67 with Point

use of com.vividsolutions.jts.geom.Point in project tests by datanucleus.

the class JtsGeometryMappingTest method testPointMapping.

public void testPointMapping() throws SQLException, ParseException {
    if (!runTestsForDatastore()) {
        return;
    }
    Point point = (Point) wktReader.read("POINT(10 10)");
    point.setSRID(-1);
    SamplePoint samplePoint;
    SamplePoint samplePoint_read;
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        samplePoint = new SamplePoint(1001, "Point 1", point);
        em.persist(samplePoint);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
    em = emf.createEntityManager();
    tx = em.getTransaction();
    try {
        tx.begin();
        samplePoint_read = (SamplePoint) em.find(SamplePoint.class, new Long(1001));
        assertEquals(samplePoint, samplePoint_read);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}
Also used : SamplePoint(org.datanucleus.samples.jtsgeometry.SamplePoint) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) SamplePoint(org.datanucleus.samples.jtsgeometry.SamplePoint) Point(com.vividsolutions.jts.geom.Point)

Example 68 with Point

use of com.vividsolutions.jts.geom.Point in project tests by datanucleus.

the class JtsGeometryMappingTest method testNoUserDataMapping.

public void testNoUserDataMapping() throws ParseException {
    if (!runTestsForDatastore()) {
        return;
    }
    Point point = (Point) wktReader.read("POINT(10 10)");
    point.setSRID(-1);
    point.setUserData(new Object());
    SampleGeometry sampleGeometry = null;
    SampleGeometry sampleGeometry_read = null;
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        sampleGeometry = new SampleGeometry(10001, "NoUserData", point);
        em.persist(sampleGeometry);
        tx.commit();
    } catch (PersistenceException pe) {
        LOG.error("Exception on persist", pe);
        fail("Persist failed : " + pe.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
    em = emf.createEntityManager();
    // Make sure not cached
    emf.getCache().evictAll();
    tx = em.getTransaction();
    try {
        tx.begin();
        sampleGeometry_read = (SampleGeometry) em.find(SampleGeometry.class, new Long(10001));
        assertEquals(sampleGeometry, sampleGeometry_read);
        assertNull(sampleGeometry_read.getGeom().getUserData());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}
Also used : SampleGeometry(org.datanucleus.samples.jtsgeometry.SampleGeometry) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) PersistenceException(javax.persistence.PersistenceException) SamplePoint(org.datanucleus.samples.jtsgeometry.SamplePoint) Point(com.vividsolutions.jts.geom.Point)

Example 69 with Point

use of com.vividsolutions.jts.geom.Point 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 70 with Point

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

the class JtsAdapter method toFeature.

/**
 * Create and return a feature from a geometry. Returns null on failure.
 *
 * @param geom flat geometry via {@link #flatFeatureList(Geometry)} that can be translated to a feature
 * @param cursor vector tile cursor position
 * @param layerProps layer properties for tagging features
 * @return new tile feature instance, or null on failure
 */
private static VectorTile.Tile.Feature toFeature(Geometry geom, Vec2d cursor, MvtLayerProps layerProps, IUserDataConverter userDataConverter) {
    // Guard: UNKNOWN Geometry
    final VectorTile.Tile.GeomType mvtGeomType = JtsAdapter.toGeomType(geom);
    if (mvtGeomType == VectorTile.Tile.GeomType.UNKNOWN) {
        return null;
    }
    final VectorTile.Tile.Feature.Builder featureBuilder = VectorTile.Tile.Feature.newBuilder();
    final boolean mvtClosePath = MvtUtil.shouldClosePath(mvtGeomType);
    final List<Integer> mvtGeom = new ArrayList<>();
    featureBuilder.setType(mvtGeomType);
    if (geom instanceof Point || geom instanceof MultiPoint) {
        // Encode as MVT point or multipoint
        mvtGeom.addAll(ptsToGeomCmds(geom, cursor));
    } else if (geom instanceof LineString || geom instanceof MultiLineString) {
        // Encode as MVT linestring or multi-linestring
        for (int i = 0; i < geom.getNumGeometries(); ++i) {
            mvtGeom.addAll(linesToGeomCmds(geom.getGeometryN(i), mvtClosePath, cursor, 1));
        }
    } else if (geom instanceof MultiPolygon || geom instanceof Polygon) {
        // Encode as MVT polygon or multi-polygon
        for (int i = 0; i < geom.getNumGeometries(); ++i) {
            final Polygon nextPoly = (Polygon) geom.getGeometryN(i);
            boolean valid = true;
            // Add exterior ring
            final LineString exteriorRing = nextPoly.getExteriorRing();
            // Area must be non-zero
            final double exteriorArea = CGAlgorithms.signedArea(exteriorRing.getCoordinates());
            if (((int) Math.round(exteriorArea)) == 0) {
                continue;
            }
            // Check CCW Winding (must be positive area)
            if (exteriorArea < 0d) {
                CoordinateArrays.reverse(exteriorRing.getCoordinates());
            }
            final List<Integer> nextPolyGeom = new ArrayList<>(linesToGeomCmds(exteriorRing, mvtClosePath, cursor, 2));
            // Add interior rings
            for (int ringIndex = 0; ringIndex < nextPoly.getNumInteriorRing(); ++ringIndex) {
                final LineString nextInteriorRing = nextPoly.getInteriorRingN(ringIndex);
                // Area must be non-zero
                final double interiorArea = CGAlgorithms.signedArea(nextInteriorRing.getCoordinates());
                if (((int) Math.round(interiorArea)) == 0) {
                    continue;
                }
                // Check CW Winding (must be negative area)
                if (interiorArea > 0d) {
                    CoordinateArrays.reverse(nextInteriorRing.getCoordinates());
                }
                // Interior ring area must be < exterior ring area, or entire geometry is invalid
                if (Math.abs(exteriorArea) <= Math.abs(interiorArea)) {
                    valid = false;
                    break;
                }
                nextPolyGeom.addAll(linesToGeomCmds(nextInteriorRing, mvtClosePath, cursor, 2));
            }
            if (valid) {
                mvtGeom.addAll(nextPolyGeom);
            }
        }
    }
    if (mvtGeom.size() < 1) {
        return null;
    }
    featureBuilder.addAllGeometry(mvtGeom);
    // Feature Properties
    userDataConverter.addTags(geom.getUserData(), layerProps, featureBuilder);
    return featureBuilder.build();
}
Also used : MultiPoint(com.vividsolutions.jts.geom.MultiPoint) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) ArrayList(java.util.ArrayList) VectorTile(net.osmand.binary.VectorTile) Point(com.vividsolutions.jts.geom.Point) MultiPoint(com.vividsolutions.jts.geom.MultiPoint) Point(com.vividsolutions.jts.geom.Point) MultiPoint(com.vividsolutions.jts.geom.MultiPoint) LineString(com.vividsolutions.jts.geom.LineString) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) VectorTile(net.osmand.binary.VectorTile) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) Polygon(com.vividsolutions.jts.geom.Polygon)

Aggregations

Point (com.vividsolutions.jts.geom.Point)157 Geometry (com.vividsolutions.jts.geom.Geometry)53 MultiPoint (com.vividsolutions.jts.geom.MultiPoint)49 Coordinate (com.vividsolutions.jts.geom.Coordinate)46 ArrayList (java.util.ArrayList)27 Polygon (com.vividsolutions.jts.geom.Polygon)26 LineString (com.vividsolutions.jts.geom.LineString)25 SamplePoint (org.datanucleus.samples.jtsgeometry.SamplePoint)22 List (java.util.List)20 PersistenceManager (javax.jdo.PersistenceManager)19 Transaction (javax.jdo.Transaction)19 MultiLineString (com.vividsolutions.jts.geom.MultiLineString)18 Query (javax.jdo.Query)17 Test (org.junit.Test)17 MultiPolygon (com.vividsolutions.jts.geom.MultiPolygon)15 HashMap (java.util.HashMap)15 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)12 QuadPointDouble (net.osmand.data.QuadPointDouble)12 GeometryCollection (com.vividsolutions.jts.geom.GeometryCollection)10 LinearRing (com.vividsolutions.jts.geom.LinearRing)10