org.postgis.Geometry Java Examples

The following examples show how to use org.postgis.Geometry. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GeometryConverterAdapter.java    From importer-exporter with Apache License 2.0 6 votes vote down vote up
private GeometryObject getEnvelope(Geometry geometry) {
	double[] coordinates = new double[]{Double.MAX_VALUE, Double.MAX_VALUE, Double.MAX_VALUE, -Double.MAX_VALUE, -Double.MAX_VALUE, -Double.MAX_VALUE};

	for (int i = 0; i < geometry.numPoints(); ++i) {
		Point point = geometry.getPoint(i);
		if (point.x < coordinates[0])
			coordinates[0] = point.x;
		if (point.y < coordinates[1])
			coordinates[1] = point.y;
		if (point.z < coordinates[2])
			coordinates[2] = point.z;
		if (point.x > coordinates[3])
			coordinates[3] = point.x;
		if (point.y > coordinates[4])
			coordinates[4] = point.y;
		if (point.z > coordinates[5])
			coordinates[5] = point.z;
	}

	return GeometryObject.createEnvelope(coordinates, 3, geometry.getSrid());
}
 
Example #2
Source File: GeometryConverterAdapter.java    From importer-exporter with Apache License 2.0 6 votes vote down vote up
@Override
public GeometryObject getMultiPoint(Object geomObj) throws SQLException {
	GeometryObject multiPoint = null;

	if (geomObj instanceof PGgeometry) {
		Geometry geometry = ((PGgeometry)geomObj).getGeometry();
		if (geometry.getType() == Geometry.MULTIPOINT) {
			multiPoint = getMultiPoint((MultiPoint)geometry);
		}

		else if (geometry.getType() == Geometry.POINT) {
			Point pointObj = (Point)geometry;
			double[][] coordiantes = new double[1][];
			coordiantes[0] = getPointCoordinates(pointObj);

			multiPoint = GeometryObject.createMultiPoint(coordiantes, pointObj.getDimension(), pointObj.getSrid());
		}
	}

	return multiPoint;
}
 
Example #3
Source File: GeometryConverterAdapter.java    From importer-exporter with Apache License 2.0 6 votes vote down vote up
@Override
public GeometryObject getMultiCurve(Object geomObj) throws SQLException {
	GeometryObject multiCurve = null;

	if (geomObj instanceof PGgeometry) {
		Geometry geometry = ((PGgeometry)geomObj).getGeometry();
		if (geometry.getType() == Geometry.MULTILINESTRING) {
			multiCurve = getMultiCurve((MultiLineString)geometry);
		}

		else if (geometry.getType() == Geometry.LINESTRING) {
			LineString lineStringObj = (LineString)geometry;
			double[][] coordiantes = new double[1][];
			coordiantes[0] = getCurveCoordinates(lineStringObj);

			multiCurve = GeometryObject.createMultiPoint(coordiantes, lineStringObj.getDimension(), lineStringObj.getSrid());
		}
	}

	return multiCurve;
}
 
Example #4
Source File: GeometryConverterAdapter.java    From importer-exporter with Apache License 2.0 6 votes vote down vote up
@Override
public GeometryObject getMultiPolygon(Object geomObj) throws SQLException {
	GeometryObject multiPolygon = null;

	if (geomObj instanceof PGgeometry) {
		Geometry geometry = ((PGgeometry)geomObj).getGeometry();
		if (geometry.getType() == Geometry.MULTIPOLYGON) {
			multiPolygon = getMultiPolygon((MultiPolygon)geometry);
		}

		else if (geometry.getType() == Geometry.POLYGON) {
			Polygon polygonObj = (Polygon)geometry;
			double[][] coordinates = getPolygonCoordinates(polygonObj);
			int[] exteriorRings = new int[]{ 0 };

			multiPolygon = GeometryObject.createMultiPolygon(coordinates, exteriorRings, polygonObj.getDimension(), polygonObj.getSrid());
		}
	}

	return multiPolygon;
}
 
Example #5
Source File: GeometryConverterAdapter.java    From importer-exporter with Apache License 2.0 6 votes vote down vote up
@Override
public GeometryObject getGeometry(Object geomObj) throws SQLException {
	if (geomObj instanceof PGgeometry) {
		Geometry geometry = ((PGgeometry)geomObj).getGeometry();
		switch (geometry.getType()) {
		case Geometry.POINT:
			return getPoint((Point)geometry);
		case Geometry.MULTIPOINT:
			return getMultiPoint((MultiPoint)geometry);
		case Geometry.LINESTRING:
			return getCurve((LineString)geometry);
		case Geometry.MULTILINESTRING:
			return getMultiCurve((MultiLineString)geometry);
		case Geometry.POLYGON:
			return getPolygon((Polygon)geometry);
		case Geometry.MULTIPOLYGON:
			return getMultiPolygon((MultiPolygon)geometry);
		default:
			throw new SQLException("Cannot convert PostGIS geometry type '" + geometry.getType() + "' to internal representation: Unsupported type.");
		}
	}

	return null;
}
 
Example #6
Source File: GeographyPolygon.java    From sqlg with MIT License 5 votes vote down vote up
public GeographyPolygon(Polygon polygon) {
    this.srid = polygon.srid;
    this.haveMeasure = polygon.haveMeasure;
    this.dimension = polygon.dimension;
    this.subgeoms = new Geometry[polygon.numGeoms()];
    for (int i = 0 ; i < polygon.numGeoms(); i++) {
        subgeoms[i] = polygon.getSubGeometry(i);
    }
}
 
Example #7
Source File: UtilAdapter.java    From importer-exporter with Apache License 2.0 5 votes vote down vote up
@Override
protected BoundingBox transformBoundingBox(BoundingBox bbox, DatabaseSrs sourceSrs, DatabaseSrs targetSrs, Connection connection) throws SQLException {
    BoundingBox result = new BoundingBox(bbox);
    int sourceSrid = sourceSrs.getSrid();
    int targetSrid = targetSrs.getSrid();

    StringBuilder boxGeom = new StringBuilder()
            .append("SRID=").append(sourceSrid).append(";POLYGON((")
            .append(bbox.getLowerCorner().getX()).append(" ").append(bbox.getLowerCorner().getY()).append(",")
            .append(bbox.getLowerCorner().getX()).append(" ").append(bbox.getUpperCorner().getY()).append(",")
            .append(bbox.getUpperCorner().getX()).append(" ").append(bbox.getUpperCorner().getY()).append(",")
            .append(bbox.getUpperCorner().getX()).append(" ").append(bbox.getLowerCorner().getY()).append(",")
            .append(bbox.getLowerCorner().getX()).append(" ").append(bbox.getLowerCorner().getY()).append("))");

    try (PreparedStatement psQuery = connection.prepareStatement("select ST_Transform(ST_GeomFromEWKT(?), " + targetSrid + ')')) {
        psQuery.setString(1, boxGeom.toString());

        try (ResultSet rs = psQuery.executeQuery()) {
            if (rs.next()) {
                PGgeometry pgGeom = (PGgeometry) rs.getObject(1);
                if (!rs.wasNull() && pgGeom != null) {
                    Geometry geom = pgGeom.getGeometry();
                    result.getLowerCorner().setX(geom.getPoint(0).x);
                    result.getLowerCorner().setY(geom.getPoint(0).y);
                    result.getUpperCorner().setX(geom.getPoint(2).x);
                    result.getUpperCorner().setY(geom.getPoint(2).y);
                    result.setSrs(targetSrs);
                }
            }
        }

        return result;
    }
}
 
Example #8
Source File: GeometryConverterAdapter.java    From importer-exporter with Apache License 2.0 5 votes vote down vote up
@Override
public GeometryObject getPoint(Object geomObj) throws SQLException {
	GeometryObject point = null;

	if (geomObj instanceof PGgeometry) {
		Geometry geometry = ((PGgeometry)geomObj).getGeometry();
		if (geometry.getType() != Geometry.POINT)
			return null;

		point = getPoint((Point)geometry);
	}

	return point;
}
 
Example #9
Source File: GeometryConverterAdapter.java    From importer-exporter with Apache License 2.0 5 votes vote down vote up
@Override
public GeometryObject getCurve(Object geomObj) throws SQLException {
	GeometryObject curve = null;

	if (geomObj instanceof PGgeometry) {
		Geometry geometry = ((PGgeometry)geomObj).getGeometry();
		if (geometry.getType() != Geometry.LINESTRING)
			return null;

		curve = getCurve((LineString)geometry);
	}

	return curve;
}
 
Example #10
Source File: GeometryConverterAdapter.java    From importer-exporter with Apache License 2.0 5 votes vote down vote up
@Override
public GeometryObject getPolygon(Object geomObj) throws SQLException {
	GeometryObject polygon = null;

	if (geomObj instanceof PGgeometry) {
		Geometry geometry = ((PGgeometry)geomObj).getGeometry();
		if (geometry.getType() != Geometry.POLYGON)
			return null;

		polygon = getPolygon((Polygon)geometry);
	}

	return polygon;
}
 
Example #11
Source File: UtilAdapter.java    From importer-exporter with Apache License 2.0 4 votes vote down vote up
@Override
protected BoundingBox createBoundingBoxes(List<Integer> objectClassIds, boolean onlyIfNull, Connection connection) throws SQLException {
    BoundingBox bbox = null;

    try {
        for (Integer classId : objectClassIds) {
            String call = "{? = call " + databaseAdapter.getSQLAdapter().resolveDatabaseOperationName("citydb_envelope.get_envelope_cityobjects") + "(?,1,?)}";
            interruptableCallableStatement = connection.prepareCall(call);
            interruptableCallableStatement.registerOutParameter(1, databaseAdapter.getGeometryConverter().getNullGeometryType());
            interruptableCallableStatement.setInt(2, classId);
            interruptableCallableStatement.setInt(3, onlyIfNull ? 1 : 0);
            interruptableCallableStatement.executeUpdate();

            Position lowerCorner = new Position(Double.MAX_VALUE, Double.MAX_VALUE);
            Position upperCorner = new Position(-Double.MAX_VALUE, -Double.MAX_VALUE);

            Object geomObject = interruptableCallableStatement.getObject(1);
            if (geomObject instanceof PGgeometry) {
                Geometry geom = ((PGgeometry) geomObject).getGeometry();
                double xmin, ymin, xmax, ymax;

                xmin = geom.getPoint(0).x;
                ymin = geom.getPoint(0).y;
                xmax = geom.getPoint(2).x;
                ymax = geom.getPoint(2).y;

                lowerCorner.setX(xmin);
                lowerCorner.setY(ymin);
                upperCorner.setX(xmax);
                upperCorner.setY(ymax);
            }

            if (!isInterrupted) {
                if (bbox == null)
                    bbox = new BoundingBox(lowerCorner, upperCorner);
                else
                    bbox.update(lowerCorner, upperCorner);
            }
        }

    } catch (SQLException e) {
        if (!isInterrupted)
            throw e;
    } finally {
        if (interruptableCallableStatement != null) {
            interruptableCallableStatement.close();
            interruptableCallableStatement = null;
        }

        isInterrupted = false;
    }

    return bbox;
}
 
Example #12
Source File: ShapeTiles.java    From open-rmbt with Apache License 2.0 4 votes vote down vote up
public GeometryColor(final Geometry geometry, final Color color)
{
    this.geometry = geometry;
    this.color = color;
}