Search in sources :

Example 1 with GeographyPoint

use of org.umlg.sqlg.gis.GeographyPoint in project sqlg by pietermartin.

the class PostgresDialect method handleOther.

@Override
public void handleOther(Map<String, Object> properties, String columnName, Object o, PropertyType propertyType) {
    switch(propertyType) {
        case POINT:
            properties.put(columnName, ((PGgeometry) o).getGeometry());
            break;
        case LINESTRING:
            properties.put(columnName, ((PGgeometry) o).getGeometry());
            break;
        case GEOGRAPHY_POINT:
            try {
                Geometry geometry = PGgeometry.geomFromString(((PGobject) o).getValue());
                properties.put(columnName, new GeographyPoint((Point) geometry));
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            break;
        case GEOGRAPHY_POLYGON:
            try {
                Geometry geometry = PGgeometry.geomFromString(((PGobject) o).getValue());
                properties.put(columnName, new GeographyPolygon((Polygon) geometry));
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            break;
        case POLYGON:
            properties.put(columnName, ((PGgeometry) o).getGeometry());
            break;
        case JSON:
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                JsonNode jsonNode = objectMapper.readTree(((PGobject) o).getValue());
                properties.put(columnName, jsonNode);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            break;
        case BYTE_ARRAY:
            java.sql.Array array = (java.sql.Array) o;
            String arrayAsString = array.toString();
            // remove the wrapping curly brackets
            arrayAsString = arrayAsString.substring(1);
            arrayAsString = arrayAsString.substring(0, arrayAsString.length() - 1);
            String[] byteAsString = arrayAsString.split(",");
            // PGbytea.toBytes();
            Byte[] result = new Byte[byteAsString.length];
            int count = 0;
            for (String s : byteAsString) {
                Integer byteAsInteger = Integer.parseUnsignedInt(s.replace("\"", ""));
                result[count++] = new Byte("");
            }
            properties.put(columnName, result);
            break;
        default:
            throw new IllegalStateException("sqlgDialect.handleOther does not handle " + propertyType.name());
    }
// if (o instanceof PGgeometry) {
// properties.put(columnName, ((PGgeometry) o).getGeometry());
// } else if ((o instanceof PGobject) && ((PGobject) o).getType().equals("geography")) {
// try {
// Geometry geometry = PGgeometry.geomFromString(((PGobject) o).getValue());
// if (geometry instanceof Point) {
// properties.put(columnName, new GeographyPoint((Point) geometry));
// } else if (geometry instanceof Polygon) {
// properties.put(columnName, new GeographyPolygon((Polygon) geometry));
// } else {
// throw new IllegalStateException("Gis type " + geometry.getClass().getName() + " is not supported.");
// }
// } catch (SQLException e) {
// throw new RuntimeException(e);
// }
// } else {
// //Assume json for now
// if (o instanceof java.sql.Array) {
// java.sql.Array array = (java.sql.Array) o;
// String arrayAsString = array.toString();
// //remove the wrapping curly brackets
// arrayAsString = arrayAsString.substring(1);
// arrayAsString = arrayAsString.substring(0, arrayAsString.length() - 1);
// arrayAsString = StringEscapeUtils.unescapeJava(arrayAsString);
// //remove the wrapping qoutes
// arrayAsString = arrayAsString.substring(1);
// arrayAsString = arrayAsString.substring(0, arrayAsString.length() - 1);
// String[] jsons = arrayAsString.split("\",\"");
// JsonNode[] jsonNodes = new JsonNode[jsons.length];
// ObjectMapper objectMapper = new ObjectMapper();
// int count = 0;
// for (String json : jsons) {
// try {
// JsonNode jsonNode = objectMapper.readTree(json);
// jsonNodes[count++] = jsonNode;
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
// }
// properties.put(columnName, jsonNodes);
// } else {
// ObjectMapper objectMapper = new ObjectMapper();
// try {
// JsonNode jsonNode = objectMapper.readTree(((PGobject) o).getValue());
// properties.put(columnName, jsonNode);
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
// }
// }
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) GeographyPoint(org.umlg.sqlg.gis.GeographyPoint) GeographyPoint(org.umlg.sqlg.gis.GeographyPoint) java.sql(java.sql) GeographyPolygon(org.umlg.sqlg.gis.GeographyPolygon) GeographyPoint(org.umlg.sqlg.gis.GeographyPoint) GeographyPolygon(org.umlg.sqlg.gis.GeographyPolygon) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with GeographyPoint

use of org.umlg.sqlg.gis.GeographyPoint in project sqlg by pietermartin.

the class TestGis method testUpgradePostGisTypes.

@Test
public void testUpgradePostGisTypes() throws Exception {
    Point johannesburgPoint = new Point(26.2044, 28.0456);
    Vertex johannesburg = this.sqlgGraph.addVertex(T.label, "Gis", "point", johannesburgPoint);
    Point pretoriaPoint = new Point(25.7461, 28.1881);
    LineString lineString = new LineString(new Point[] { johannesburgPoint, pretoriaPoint });
    Vertex pretoria = this.sqlgGraph.addVertex(T.label, "Gis", "lineString", lineString);
    GeographyPoint geographyPointJohannesburg = new GeographyPoint(26.2044, 28.0456);
    Vertex johannesburgGeographyPoint = this.sqlgGraph.addVertex(T.label, "Gis", "geographyPoint", geographyPointJohannesburg);
    LinearRing linearRing = new LinearRing("0 0, 1 1, 1 2, 1 1, 0 0");
    Polygon polygon1 = new Polygon(new LinearRing[] { linearRing });
    Vertex johannesburgPolygon = this.sqlgGraph.addVertex(T.label, "Gis", "polygon", polygon1);
    linearRing = new LinearRing("0 0, 1 1, 1 2, 1 1, 0 0");
    GeographyPolygon geographyPolygon = new GeographyPolygon(new LinearRing[] { linearRing });
    Vertex johannesburgGeographyPolygon = this.sqlgGraph.addVertex(T.label, "Gis", "geographyPolygon", geographyPolygon);
    this.sqlgGraph.tx().commit();
    // Delete the topology
    Connection conn = this.sqlgGraph.tx().getConnection();
    try (Statement statement = conn.createStatement()) {
        statement.execute("DROP SCHEMA " + this.sqlgGraph.getSqlDialect().maybeWrapInQoutes("sqlg_schema") + " CASCADE");
    }
    this.sqlgGraph.tx().commit();
    this.sqlgGraph.close();
    try (SqlgGraph sqlgGraph1 = SqlgGraph.open(configuration)) {
        List<Vertex> schemaVertices = sqlgGraph1.topology().V().hasLabel("sqlg_schema.schema").has("name", sqlgGraph1.getSqlDialect().getPublicSchema()).toList();
        Assert.assertEquals(1, schemaVertices.size());
        List<Vertex> propertyVertices = sqlgGraph1.topology().V().hasLabel("sqlg_schema.schema").has("name", sqlgGraph1.getSqlDialect().getPublicSchema()).out("schema_vertex").has("name", "Gis").out("vertex_property").has("name", "point").toList();
        Assert.assertEquals(1, propertyVertices.size());
        Assert.assertEquals("POINT", propertyVertices.get(0).value("type"));
        Assert.assertEquals(johannesburgPoint, sqlgGraph1.traversal().V(johannesburg.id()).next().value("point"));
        Assert.assertEquals(lineString, sqlgGraph1.traversal().V(pretoria.id()).next().value("lineString"));
        Assert.assertEquals(geographyPointJohannesburg, sqlgGraph1.traversal().V(johannesburgGeographyPoint.id()).next().value("geographyPoint"));
        Assert.assertEquals(polygon1, sqlgGraph1.traversal().V(johannesburgPolygon.id()).next().value("polygon"));
        Assert.assertEquals(geographyPolygon, sqlgGraph1.traversal().V(johannesburgGeographyPolygon.id()).next().value("geographyPolygon"));
    }
}
Also used : GeographyPolygon(org.umlg.sqlg.gis.GeographyPolygon) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) LineString(org.postgis.LineString) Statement(java.sql.Statement) GeographyPoint(org.umlg.sqlg.gis.GeographyPoint) Connection(java.sql.Connection) Point(org.postgis.Point) GeographyPoint(org.umlg.sqlg.gis.GeographyPoint) LinearRing(org.postgis.LinearRing) GeographyPolygon(org.umlg.sqlg.gis.GeographyPolygon) Polygon(org.postgis.Polygon) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 3 with GeographyPoint

use of org.umlg.sqlg.gis.GeographyPoint in project sqlg by pietermartin.

the class TestGis method testGeographyPoint.

@Test
public void testGeographyPoint() {
    GeographyPoint geographyPointJohannesburg = new GeographyPoint(26.2044, 28.0456);
    Vertex johannesburg = this.sqlgGraph.addVertex(T.label, "Gis", "geographyPoint", geographyPointJohannesburg);
    GeographyPoint geographyPointPretoria = new GeographyPoint(25.7461, 28.1881);
    Vertex pretoria = this.sqlgGraph.addVertex(T.label, "Gis", "geographyPoint", geographyPointPretoria);
    this.sqlgGraph.tx().commit();
    Object geographyPoint = this.sqlgGraph.traversal().V(johannesburg.id()).next().value("geographyPoint");
    Assert.assertEquals(geographyPointJohannesburg, geographyPoint);
    geographyPoint = this.sqlgGraph.traversal().V(pretoria.id()).next().value("geographyPoint");
    Assert.assertEquals(geographyPointPretoria, geographyPoint);
    Gis gis = this.sqlgGraph.gis();
    System.out.println(gis.distanceBetween(geographyPointJohannesburg, geographyPointPretoria));
    geographyPointPretoria = new GeographyPoint(25.7461, 28.1881);
    pretoria.property("geographyPoint", geographyPointPretoria);
    this.sqlgGraph.tx().commit();
    geographyPoint = this.sqlgGraph.traversal().V(pretoria.id()).next().value("geographyPoint");
    Assert.assertEquals(geographyPointPretoria, geographyPoint);
}
Also used : Gis(org.umlg.sqlg.gis.Gis) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) GeographyPoint(org.umlg.sqlg.gis.GeographyPoint) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 4 with GeographyPoint

use of org.umlg.sqlg.gis.GeographyPoint in project sqlg by pietermartin.

the class TestGisBulkWithin method testBulkWithinGeographyPoint.

@Test
public void testBulkWithinGeographyPoint() {
    GeographyPoint point1 = new GeographyPoint(26.2044, 28.0456);
    GeographyPoint point2 = new GeographyPoint(26.2045, 28.0457);
    GeographyPoint point3 = new GeographyPoint(26.2046, 28.0458);
    GeographyPoint point4 = new GeographyPoint(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));
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) GeographyPoint(org.umlg.sqlg.gis.GeographyPoint) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Aggregations

GeographyPoint (org.umlg.sqlg.gis.GeographyPoint)4 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)3 Test (org.junit.Test)3 BaseTest (org.umlg.sqlg.test.BaseTest)3 GeographyPolygon (org.umlg.sqlg.gis.GeographyPolygon)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 java.sql (java.sql)1 Connection (java.sql.Connection)1 Statement (java.sql.Statement)1 LineString (org.postgis.LineString)1 LinearRing (org.postgis.LinearRing)1 Point (org.postgis.Point)1 Polygon (org.postgis.Polygon)1 Gis (org.umlg.sqlg.gis.Gis)1 SqlgGraph (org.umlg.sqlg.structure.SqlgGraph)1