org.postgis.Point Java Examples

The following examples show how to use org.postgis.Point. 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: Gis.java    From sqlg with MIT License 6 votes vote down vote up
public double distanceBetween(Point johannesburg, Point pretoria) {
    Connection conn = this.sqlgGraph.tx().getConnection();
    try (Statement statement = conn.createStatement()) {
        if (statement.execute("SELECT ST_Distance_Sphere('" + johannesburg.toString() + "', '" + pretoria.toString() + "')")) {
            ResultSet resultSet = statement.getResultSet();
            if (resultSet.next()) {
                return resultSet.getDouble("st_distance_sphere");
            } else {
                return -1;
            }
        } else {
            return -1;
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
 
Example #2
Source File: TestGisBulkWithin.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testBulkWithinLineString() {
    Point point1 = new Point(26.2044, 28.0456);
    Point point2 = new Point(26.2045, 28.0457);
    LineString lineString1 = new LineString(new Point[] {point1, point2});

    Point point3 = new Point(26.2046, 28.0458);
    LineString lineString2 = new LineString(new Point[] {point1, point3});
    LineString lineString3 = new LineString(new Point[] {point2, point3});

    Point point4 = new Point(26.2047, 28.0459);
    LineString lineString4 = new LineString(new Point[] {point1, point4});

    Vertex v1 = this.sqlgGraph.addVertex(T.label, "Gis", "line", lineString1);
    Vertex v2 = this.sqlgGraph.addVertex(T.label, "Gis", "line", lineString2);
    Vertex v3 = this.sqlgGraph.addVertex(T.label, "Gis", "line", lineString3);
    Vertex v4 = this.sqlgGraph.addVertex(T.label, "Gis", "line", lineString4);
    this.sqlgGraph.tx().commit();
    List<Vertex> vertices = this.sqlgGraph.traversal().V().hasLabel("Gis").has("line", P.within(lineString1, lineString3, lineString4)).toList();
    Assert.assertEquals(3, vertices.size());
    Assert.assertTrue(Arrays.asList(v1, v3, v4).containsAll(vertices));
}
 
Example #3
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 #4
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 #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: LineStringTypeHandlerTest.java    From mybatis-typehandlers-postgis with Do What The F*ck You Want To Public License 5 votes vote down vote up
@Before
public void before() {
    table = "test_linestring";

    Point[] points = new Point[4];
    points[0] = new Point(123.45d, 23.45d);
    points[1] = new Point(124.45d, 23.45d);
    points[2] = new Point(124.45d, 24.45d);
    points[3] = new Point(123.45d, 24.45d);

    t = new LineString(points);
    t.setSrid(SRID);
}
 
Example #7
Source File: PolygonTypeHandlerTest.java    From mybatis-typehandlers-postgis with Do What The F*ck You Want To Public License 5 votes vote down vote up
@Before
public void before() {
    table = "test_polygon";

    Point[] points = new Point[5];
    points[0] = new Point(123.45d, 23.45d);
    points[1] = new Point(124.45d, 23.45d);
    points[2] = new Point(124.45d, 24.45d);
    points[3] = new Point(123.45d, 24.45d);
    points[4] = new Point(123.45d, 23.45d);
    LinearRing linearRing = new LinearRing(points);
    t = new Polygon(new LinearRing[]{linearRing});
    t.setSrid(SRID);
}
 
Example #8
Source File: MultiPointTypeHandlerTest.java    From mybatis-typehandlers-postgis with Do What The F*ck You Want To Public License 5 votes vote down vote up
@Before
public void before() {
    table = "test_multipoint";

    Point[] points = new Point[4];
    points[0] = new Point(123.45d, 23.45d);
    points[1] = new Point(124.45d, 23.45d);
    points[2] = new Point(124.45d, 24.45d);
    points[3] = new Point(123.45d, 24.45d);

    t = new MultiPoint(points);
    t.setSrid(SRID);
}
 
Example #9
Source File: GeographyPoint.java    From sqlg with MIT License 5 votes vote down vote up
public GeographyPoint(Point point) {
    this.x = point.x;
    this.y = point.y;
    this.z = point.z;
    this.dimension = point.dimension;
    this.m = point.m;
    this.srid = point.srid;
    this.haveMeasure = point.haveMeasure;
}
 
Example #10
Source File: TestGisBulkWithin.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testBulkWithinPoint() {
    Point point1 = new Point(26.2044, 28.0456);
    Point point2 = new Point(26.2045, 28.0457);
    Point point3 = new Point(26.2046, 28.0458);
    Point point4 = new Point(26.2047, 28.0459);
    Vertex v1 = this.sqlgGraph.addVertex(T.label, "Gis", "point", point1);
    Vertex v2 = this.sqlgGraph.addVertex(T.label, "Gis", "point", point2);
    Vertex v3 = this.sqlgGraph.addVertex(T.label, "Gis", "point", point3);
    Vertex v4 = this.sqlgGraph.addVertex(T.label, "Gis", "point", point4);
    this.sqlgGraph.tx().commit();
    List<Vertex> vertices = this.sqlgGraph.traversal().V().hasLabel("Gis").has("point", P.within(point1, point3, point4)).toList();
    Assert.assertEquals(3, vertices.size());
    Assert.assertTrue(Arrays.asList(v1, v3, v4).containsAll(vertices));
}
 
Example #11
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 #12
Source File: GeometryConverterAdapter.java    From importer-exporter with Apache License 2.0 5 votes vote down vote up
private double[] getPointCoordinates(Point point) {
	int dimension = point.getDimension();
	double[] coordinates = new double[dimension];

	coordinates[0] = point.x;
	coordinates[1] = point.y;
	if (dimension == 3)
		coordinates[2] = point.z;

	return coordinates;
}
 
Example #13
Source File: PointTypeHandlerTest.java    From mybatis-typehandlers-postgis with Do What The F*ck You Want To Public License 4 votes vote down vote up
@Before
public void before() {
    table = "test_point";
    t = new Point(123.45d, 23.45d);
    t.setSrid(SRID);
}
 
Example #14
Source File: PointTypeHandlerTest.java    From mybatis-typehandlers-postgis with Do What The F*ck You Want To Public License 4 votes vote down vote up
@Override
protected TypeHandler<Point> getTypeHandler() {
    return TYPE_HANDLER;
}
 
Example #15
Source File: GeometryConverterAdapter.java    From importer-exporter with Apache License 2.0 4 votes vote down vote up
private GeometryObject getPoint(Point point) {
	return GeometryObject.createPoint(getPointCoordinates(point), point.getDimension(), point.getSrid());
}