use of org.umlg.sqlg.gis.GeographyPolygon in project sqlg by pietermartin.
the class TestGis method testGeographyPolygon.
@Test
public void testGeographyPolygon() throws SQLException {
LinearRing linearRing = new LinearRing("0 0, 1 1, 1 2, 1 1, 0 0");
GeographyPolygon polygon1 = new GeographyPolygon(new LinearRing[] { linearRing });
Vertex johannesburg = this.sqlgGraph.addVertex(T.label, "Gis", "polygon", polygon1);
this.sqlgGraph.tx().commit();
GeographyPolygon geographyPolygon = this.sqlgGraph.traversal().V(johannesburg.id()).next().value("polygon");
Assert.assertEquals(polygon1, geographyPolygon);
linearRing = new LinearRing("0 1, 2 3, 1 3, 1 3, 0 1");
polygon1 = new GeographyPolygon(new LinearRing[] { linearRing });
johannesburg.property("polygon", polygon1);
this.sqlgGraph.tx().commit();
geographyPolygon = this.sqlgGraph.traversal().V(johannesburg.id()).next().value("polygon");
Assert.assertEquals(polygon1, geographyPolygon);
}
use of org.umlg.sqlg.gis.GeographyPolygon in project sqlg by pietermartin.
the class TestGisBulkWithin method testBulkWithinGeographyPolygon.
@Test
public void testBulkWithinGeographyPolygon() throws SQLException {
LinearRing linearRing1 = new LinearRing("0 0, 1 1, 1 2, 1 1, 0 0");
GeographyPolygon polygon1 = new GeographyPolygon(new LinearRing[] { linearRing1 });
LinearRing linearRing2 = new LinearRing("1 1, 1 1, 1 2, 1 1, 1 1");
GeographyPolygon polygon2 = new GeographyPolygon(new LinearRing[] { linearRing2 });
LinearRing linearRing3 = new LinearRing("2 2, 1 1, 1 2, 1 1, 2 2");
GeographyPolygon polygon3 = new GeographyPolygon(new LinearRing[] { linearRing3 });
LinearRing linearRing4 = new LinearRing("1 3, 1 2, 2 2, 1 1, 1 3");
GeographyPolygon polygon4 = new GeographyPolygon(new LinearRing[] { linearRing4 });
Vertex v1 = this.sqlgGraph.addVertex(T.label, "Gis", "polygon", polygon1);
Vertex v2 = this.sqlgGraph.addVertex(T.label, "Gis", "polygon", polygon2);
Vertex v3 = this.sqlgGraph.addVertex(T.label, "Gis", "polygon", polygon3);
Vertex v4 = this.sqlgGraph.addVertex(T.label, "Gis", "polygon", polygon4);
this.sqlgGraph.tx().commit();
List<Vertex> vertices = this.sqlgGraph.traversal().V().hasLabel("Gis").has("polygon", P.within(polygon1, polygon3, polygon4)).toList();
Assert.assertEquals(3, vertices.size());
Assert.assertTrue(Arrays.asList(v1, v3, v4).containsAll(vertices));
}
use of org.umlg.sqlg.gis.GeographyPolygon 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);
// }
// }
// }
}
use of org.umlg.sqlg.gis.GeographyPolygon 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"));
}
}
Aggregations