Search in sources :

Example 51 with Polygon

use of com.vividsolutions.jts.geom.Polygon in project ili2db by claeis.

the class Iox2fgdb method multisurface2wkb.

public byte[] multisurface2wkb(// SurfaceOrAreaType type)
IomObject multisurfaceObj, // SurfaceOrAreaType type)
boolean asCurvePolygon, // SurfaceOrAreaType type)
double strokeP, // SurfaceOrAreaType type)
int srsId) throws IoxException {
    if (multisurfaceObj == null) {
        return null;
    }
    byte[] ret = null;
    os.reset();
    if (asCurvePolygon) {
        int shapeType = EsriShpConstants.ShapeGeneralPolygon;
        shapeType |= EsriShpConstants.shapeHasCurves;
        shapeType |= (outputDimension == 3 ? EsriShpConstants.shapeHasZs : 0);
        os.writeInt(shapeType);
    } else {
        if (outputDimension == 3) {
            os.writeInt(EsriShpConstants.ShapePolygonZ);
        } else {
            os.writeInt(EsriShpConstants.ShapePolygon);
        }
    }
    java.util.ArrayList<Polygon> polygons = new java.util.ArrayList<Polygon>();
    // boundingBox
    Envelope env = new Envelope();
    int surfacec = multisurfaceObj.getattrvaluecount("surface");
    for (int surfacei = 0; surfacei < surfacec; surfacei++) {
        IomObject surface = multisurfaceObj.getattrobj("surface", surfacei);
        IomObject iomSurfaceClone = new ch.interlis.iom_j.Iom_jObject("MULTISURFACE", null);
        iomSurfaceClone.addattrobj("surface", surface);
        Polygon polygon = Iox2jtsext.surface2JTS(iomSurfaceClone, strokeP);
        polygons.add(polygon);
        env.expandToInclude(polygon.getEnvelopeInternal());
    }
    os.writeDouble(env.getMinX());
    os.writeDouble(env.getMinY());
    os.writeDouble(env.getMaxX());
    os.writeDouble(env.getMaxY());
    // cParts The number of rings in the multisurface.
    // cPoints The total number of points for all parts.
    int cPart = 0;
    int cPoints = 0;
    for (Polygon polygon : polygons) {
        cPart += 1;
        cPoints += getNumPoints(polygon.getExteriorRing());
        int holec = polygon.getNumInteriorRing();
        cPart += holec;
        for (int holei = 0; holei < holec; holei++) {
            cPoints += getNumPoints(polygon.getInteriorRingN(holei));
        }
    }
    os.writeInt(cPart);
    os.writeInt(cPoints);
    // parts[cParts] An array of length NumParts. Stores, for each Ring, the index of its
    // first point in the points array. Array indexes are with respect to 0.
    int partStart = 0;
    for (Polygon polygon : polygons) {
        os.writeInt(partStart);
        partStart += getNumPoints(polygon.getExteriorRing());
        int holec = polygon.getNumInteriorRing();
        for (int holei = 0; holei < holec; holei++) {
            os.writeInt(partStart);
            partStart += getNumPoints(polygon.getInteriorRingN(holei));
        }
    }
    java.util.ArrayList<Arc> arcs = null;
    if (asCurvePolygon) {
        arcs = new java.util.ArrayList<Arc>();
    }
    java.util.ArrayList<Double> zv = null;
    if (outputDimension == 3) {
        zv = new java.util.ArrayList<Double>();
    }
    double[] zMin = new double[1];
    double[] zMax = new double[1];
    {
        Coordinate coord = polygons.get(0).getExteriorRing().getStartPoint().getCoordinate();
        if (outputDimension == 3) {
            zMin[0] = coord.z;
            zMax[0] = coord.z;
        }
    }
    int startPtIdx = 0;
    for (Polygon polygon : polygons) {
        // shell is always in clockwise order
        // holes are in a counterclockwise direction
        LineString polyline = polygon.getExteriorRing();
        polyline = asOneLine(polyline);
        if (CGAlgorithms.isCCW(polyline.getCoordinates())) {
            polyline = (LineString) polyline.reverse();
        }
        writePoints(polyline, false, zv, zMin, zMax, startPtIdx, arcs);
        startPtIdx += getNumPoints(polyline);
        int holec = polygon.getNumInteriorRing();
        for (int holei = 0; holei < holec; holei++) {
            polyline = polygon.getInteriorRingN(holei);
            polyline = asOneLine(polyline);
            if (!CGAlgorithms.isCCW(polyline.getCoordinates())) {
                polyline = (LineString) polyline.reverse();
            }
            writePoints(polyline, true, zv, zMin, zMax, startPtIdx, arcs);
            startPtIdx += getNumPoints(polyline);
        }
    }
    if (outputDimension == 3) {
        // zMin
        os.writeDouble(zMin[0]);
        // zMax
        os.writeDouble(zMax[0]);
        // Zs[cPoints]
        for (Double z : zv) {
            os.writeDouble(z);
        }
    }
    if (asCurvePolygon) {
        writeArcs(arcs);
    }
    ret = os.toByteArray();
    return ret;
}
Also used : ArrayList(java.util.ArrayList) Envelope(com.vividsolutions.jts.geom.Envelope) IomObject(ch.interlis.iom.IomObject) Coordinate(com.vividsolutions.jts.geom.Coordinate) LineString(com.vividsolutions.jts.geom.LineString) Polygon(com.vividsolutions.jts.geom.Polygon)

Example 52 with Polygon

use of com.vividsolutions.jts.geom.Polygon in project ili2db by claeis.

the class Iox2gpkg method surface2wkb.

/**
 * Converts a SURFACE to a JTS Polygon.
 * @param obj INTERLIS SURFACE structure
 * @param strokeP maximum stroke to use when removing ARCs
 * @return JTS Polygon
 * @throws Iox2wkbException
 */
public byte[] surface2wkb(// SurfaceOrAreaType type)
IomObject obj, // SurfaceOrAreaType type)
boolean asCurvePolygon, // SurfaceOrAreaType type)
double strokeP, // SurfaceOrAreaType type)
int srsId) throws Iox2wkbException {
    if (obj == null) {
        return null;
    }
    try {
        os.reset();
        Polygon surface = Iox2jtsext.surface2JTS(obj, strokeP);
        writeGeoPackageBinaryHeader(srsId, surface.getEnvelopeInternal());
        // wkb
        Iox2wkb helper = new Iox2wkb(outputDimension, os.order());
        os.write(helper.surface2wkb(obj, asCurvePolygon, strokeP));
    } catch (IOException e) {
        throw new RuntimeException("Unexpected IO exception: " + e.getMessage());
    } catch (IoxException e) {
        throw new RuntimeException("Unexpected exception: " + e.getMessage());
    }
    return os.toByteArray();
}
Also used : Iox2wkb(ch.interlis.iox_j.wkb.Iox2wkb) IOException(java.io.IOException) Polygon(com.vividsolutions.jts.geom.Polygon) IoxException(ch.interlis.iox.IoxException)

Example 53 with Polygon

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

the class JtsGeometrySpatialTest method testDifference.

@Datastore(POSTGRESQL)
public void testDifference() throws SQLException, ParseException {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Polygon polygon = (Polygon) wktReader.read("POLYGON((75 75,100 75,100 80,80 80,75 75))");
        Polygon difference = (Polygon) wktReader.read("POLYGON((80 80,100 100,100 80,80 80))");
        Query query = pm.newQuery(SamplePolygon.class, "geom != null && geom.difference(:polygon).equals(:difference)");
        List list = (List) query.execute(polygon, difference);
        assertEquals("Wrong number of geometries whose difference from a given polygon is equal to a given polygon returned", 1, list.size());
        assertTrue("Polygon 2 should be in the list of geometries whose difference from a given polygon is equal to a given polygon", list.contains(getSamplePolygon(2)));
        query = pm.newQuery(SamplePolygon.class, "id == :id");
        query.setResult("geom.difference(Spatial.geomFromText('POLYGON((75 75,100 75,100 80,80 80,75 75))', 4326))");
        query.setUnique(true);
        Geometry difference_read = (Geometry) query.execute(new Long(getSamplePolygon(2).getId()));
        difference.normalize();
        difference_read.normalize();
        assertTrue("Returned difference should be equal to the given polygon", difference_read.equalsExact(difference));
    } finally {
        tx.commit();
    }
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) SamplePolygon(org.datanucleus.samples.jtsgeometry.SamplePolygon) List(java.util.List) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) SamplePolygon(org.datanucleus.samples.jtsgeometry.SamplePolygon) Polygon(com.vividsolutions.jts.geom.Polygon) Datastore(org.datanucleus.tests.annotations.Datastore)

Example 54 with Polygon

use of com.vividsolutions.jts.geom.Polygon in project openems by OpenEMS.

the class SVGWriter method appendGeometryTaggedText.

/**
 * Converts a <code>Geometry</code> to &lt;Geometry Tagged Text&gt; format,
 * then appends it to the writer.
 *
 * @param geometry
 *            the <code>Geometry</code> to process
 * @param writer
 *            the output writer to append to
 */
private void appendGeometryTaggedText(Geometry geometry, int level, Writer writer) throws IOException {
    indent(level, writer);
    if (geometry instanceof Point) {
        Point point = (Point) geometry;
        appendPointTaggedText(point.getCoordinate(), level, writer, point.getPrecisionModel());
    } else if (geometry instanceof LinearRing) {
        appendLinearRingTaggedText((LinearRing) geometry, level, writer);
    } else if (geometry instanceof LineString) {
        appendLineStringTaggedText((LineString) geometry, level, writer);
    } else if (geometry instanceof Polygon) {
        appendPolygon((Polygon) geometry, level, writer);
    } else if (geometry instanceof MultiPoint) {
        appendMultiPointTaggedText((MultiPoint) geometry, level, writer);
    } else if (geometry instanceof MultiLineString) {
        appendMultiLineStringTaggedText((MultiLineString) geometry, level, writer);
    } else if (geometry instanceof MultiPolygon) {
        appendMultiPolygonTaggedText((MultiPolygon) geometry, level, writer);
    } else if (geometry instanceof GeometryCollection) {
        appendGeometryCollectionTaggedText((GeometryCollection) geometry, level, writer);
    } else {
        Assert.shouldNeverReachHere("Unsupported Geometry implementation:" + geometry.getClass());
    }
}
Also used : MultiPoint(com.vividsolutions.jts.geom.MultiPoint) GeometryCollection(com.vividsolutions.jts.geom.GeometryCollection) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) LineString(com.vividsolutions.jts.geom.LineString) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) Point(com.vividsolutions.jts.geom.Point) MultiPoint(com.vividsolutions.jts.geom.MultiPoint) LinearRing(com.vividsolutions.jts.geom.LinearRing) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) Polygon(com.vividsolutions.jts.geom.Polygon)

Example 55 with Polygon

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

the class JtsGeometryMappingTest method testPolygonMapping.

public void testPolygonMapping() throws SQLException, ParseException {
    if (!runTestsForDatastore()) {
        return;
    }
    Polygon polygon = (Polygon) wktReader.read("POLYGON((25 25,75 25,75 75,25 75,25 25),(45 45,55 45,55 55,45 55,45 45))");
    SamplePolygon samplePolygon;
    SamplePolygon samplePolygon_read;
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    Object id = null;
    try {
        tx.begin();
        samplePolygon = new SamplePolygon(3001, "Polygon 1", polygon);
        pm.makePersistent(samplePolygon);
        id = JDOHelper.getObjectId(samplePolygon);
        samplePolygon = (SamplePolygon) pm.detachCopy(samplePolygon);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        samplePolygon_read = (SamplePolygon) pm.getObjectById(id, true);
        assertEquals(samplePolygon, samplePolygon_read);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) SamplePolygon(org.datanucleus.samples.jtsgeometry.SamplePolygon) SampleMultiPolygon(org.datanucleus.samples.jtsgeometry.SampleMultiPolygon) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) SamplePolygon(org.datanucleus.samples.jtsgeometry.SamplePolygon) Polygon(com.vividsolutions.jts.geom.Polygon)

Aggregations

Polygon (com.vividsolutions.jts.geom.Polygon)114 LinearRing (com.vividsolutions.jts.geom.LinearRing)50 MultiPolygon (com.vividsolutions.jts.geom.MultiPolygon)34 Test (org.junit.Test)32 Coordinate (com.vividsolutions.jts.geom.Coordinate)30 Point (com.vividsolutions.jts.geom.Point)29 PackedCoordinateSequence (com.vividsolutions.jts.geom.impl.PackedCoordinateSequence)27 ArrayList (java.util.ArrayList)26 RdfToRyaConversions.convertStatement (org.apache.rya.api.resolver.RdfToRyaConversions.convertStatement)24 Resource (org.openrdf.model.Resource)24 Statement (org.openrdf.model.Statement)24 URI (org.openrdf.model.URI)24 Value (org.openrdf.model.Value)24 ValueFactory (org.openrdf.model.ValueFactory)24 ContextStatementImpl (org.openrdf.model.impl.ContextStatementImpl)24 ValueFactoryImpl (org.openrdf.model.impl.ValueFactoryImpl)24 Geometry (com.vividsolutions.jts.geom.Geometry)22 LineString (com.vividsolutions.jts.geom.LineString)21 List (java.util.List)20 PersistenceManager (javax.jdo.PersistenceManager)15