com.vividsolutions.jts.io.WKBWriter Java Examples

The following examples show how to use com.vividsolutions.jts.io.WKBWriter. 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: GeometrySerializer.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static void serialize(SerializationStreamWriter streamWriter,
		Geometry instance) throws SerializationException {
	WKBWriter writer = sWriter;
	
	byte[] wkb = writer.write(instance);
	String hex = WKBWriter.toHex(wkb);
	streamWriter.writeString(hex);
}
 
Example #2
Source File: SimpleDemo.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void demonstrateWkb() {
	GeometryFactory gf = new GeometryFactory();

	WKBReader wkbReader = new WKBReader(gf);
	WKBWriter wkbWriter = new WKBWriter();

	// geometry collection from above
	String hexEncodedWkb = "00000000070000000600000000014132D53A3BC2DADC414"
			+ "10BBD1DFB613500000000040000000200000000014132D53A3BC2DADC414"
			+ "10BBD1DFB61350000000001415026FE8EF0B6B74153F78BCEFDB09A00000"
			+ "00002000000024132D53A3BC2DADC41410BBD1DFB6135415026FE8EF0B6B"
			+ "74153F78BCEFDB09A0000000005000000020000000002000000024132D53"
			+ "A3BC2DADC41410BBD1DFB6135415026FE8EF0B6B74153F78BCEFDB09A000"
			+ "0000002000000024132D53A3BC2DADC41410BBD1DFB6135415026FE8EF0B"
			+ "6B74153F78BCEFDB09A000000000300000001000000044132D53A3BC2DAD"
			+ "C41410BBD1DFB6135415026FE8EF0B6B74153F78BCEFDB09A4157C81E8EF"
			+ "0B6B7415F693E8EFDB09A4132D53A3BC2DADC41410BBD1DFB61350000000"
			+ "00600000002000000000300000001000000044132D53A3BC2DADC41410BB"
			+ "D1DFB6135415026FE8EF0B6B74153F78BCEFDB09A4157C81E8EF0B6B7415"
			+ "F693E8EFDB09A4132D53A3BC2DADC41410BBD1DFB6135000000000300000"
			+ "001000000044132D53A3BC2DADC41410BBD1DFB6135415026FE8EF0B6B74"
			+ "153F78BCEFDB09A4157C81E8EF0B6B7415F693E8EFDB09A4132D53A3BC2D"
			+ "ADC41410BBD1DFB6135";

	try {
		Geometry g = wkbReader.read(WKBReader.hexToBytes(hexEncodedWkb));

		sLogger.info("Geom from WKB: " + g);

		byte[] freshWkb = wkbWriter.write(g);
		String freshWkbHex = WKBWriter.toHex(freshWkb);

		sLogger.warning("Hexes are equal?   "
				+ hexEncodedWkb.equals(freshWkbHex));
	} catch (ParseException e) {
		sLogger.log(Level.WARNING, "Unable to parse hex wkb", e);
	}
}
 
Example #3
Source File: DatabaseTransactionalWriter.java    From xyz-hub with Apache License 2.0 4 votes vote down vote up
public static FeatureCollection insertFeatures(String schema, String table, String streamId, FeatureCollection collection,
                                               List<Feature> inserts, Connection connection)
        throws SQLException, JsonProcessingException {

    boolean batchInsert = false;
    boolean batchInsertWithoutGeometry = false;

    final PreparedStatement insertStmt = createInsertStatement(connection,schema,table);
    final PreparedStatement insertWithoutGeometryStmt = createInsertWithoutGeometryStatement(connection,schema,table);

    insertStmt.setQueryTimeout(TIMEOUT);
    insertWithoutGeometryStmt.setQueryTimeout(TIMEOUT);

    for (int i = 0; i < inserts.size(); i++) {
        final Feature feature = inserts.get(i);

        final PGobject jsonbObject= featureToPGobject(feature);

        if (feature.getGeometry() == null) {
            insertWithoutGeometryStmt.setObject(1, jsonbObject);
            insertWithoutGeometryStmt.addBatch();
            batchInsertWithoutGeometry = true;
        } else {
            insertStmt.setObject(1, jsonbObject);

            final WKBWriter wkbWriter = new WKBWriter(3);
            Geometry jtsGeometry = feature.getGeometry().getJTSGeometry();
            //Avoid NAN values
            assure3d(jtsGeometry.getCoordinates());
            insertStmt.setBytes(2, wkbWriter.write(jtsGeometry));

            insertStmt.addBatch();
            batchInsert = true;
        }
        collection.getFeatures().add(feature);
    }

    if (batchInsert) {
        insertStmt.executeBatch();
    }
    if (batchInsertWithoutGeometry) {
        insertWithoutGeometryStmt.executeBatch();
    }

    return collection;
}
 
Example #4
Source File: DatabaseTransactionalWriter.java    From xyz-hub with Apache License 2.0 4 votes vote down vote up
public static FeatureCollection updateFeatures(String schema, String table, String streamId, FeatureCollection collection,
                                               List<FeatureCollection.ModificationFailure> fails, List<Feature> updates,
                                               Connection connection, boolean handleUUID)
        throws SQLException, JsonProcessingException {

    final PreparedStatement updateStmt = createUpdateStatement(connection, schema, table, handleUUID);
    final PreparedStatement updateWithoutGeometryStmt = createUpdateWithoutGeometryStatement(connection,schema,table,handleUUID);

    updateStmt.setQueryTimeout(TIMEOUT);
    updateWithoutGeometryStmt.setQueryTimeout(TIMEOUT);

    List<String> updateIdList = new ArrayList<>();
    List<String> updateWithoutGeometryIdList = new ArrayList<>();

    int[] batchUpdateResult = null;
    int[] batchUpdateWithoutGeometryResult = null;

    for (int i = 0; i < updates.size(); i++) {
        final Feature feature = updates.get(i);
        final String puuid = feature.getProperties().getXyzNamespace().getPuuid();

        if (feature.getId() == null) {
            throw new NullPointerException("id");
        }

        final PGobject jsonbObject= featureToPGobject(feature);

        if (feature.getGeometry() == null) {
            updateWithoutGeometryStmt.setObject(1, jsonbObject);
            updateWithoutGeometryStmt.setString(2, feature.getId());
            if(handleUUID)
                updateWithoutGeometryStmt.setString(3, puuid);
            updateWithoutGeometryStmt.addBatch();

            updateWithoutGeometryIdList.add(feature.getId());
        } else {
            updateStmt.setObject(1, jsonbObject);

            final WKBWriter wkbWriter = new WKBWriter(3);
            Geometry jtsGeometry = feature.getGeometry().getJTSGeometry();
            //Avoid NAN values
            assure3d(jtsGeometry.getCoordinates());
            updateStmt.setBytes(2, wkbWriter.write(jtsGeometry));
            updateStmt.setString(3, feature.getId());

            if(handleUUID) {
                updateStmt.setString(4, puuid);
            }
            updateStmt.addBatch();

            updateIdList.add(feature.getId());
        }
        collection.getFeatures().add(feature);
    }

    if (updateIdList.size() > 0) {
        batchUpdateResult = updateStmt.executeBatch();
        fillFailList(batchUpdateResult, fails, updateIdList, handleUUID);
    }
    if (updateWithoutGeometryIdList.size() > 0) {
        batchUpdateWithoutGeometryResult = updateWithoutGeometryStmt.executeBatch();
        fillFailList(batchUpdateWithoutGeometryResult, fails, updateWithoutGeometryIdList, handleUUID);
    }

    if(fails.size() > 0)
        throw new SQLException(UPDATE_ERROR_GENERAL);

    return collection;
}
 
Example #5
Source File: DatabaseStreamWriter.java    From xyz-hub with Apache License 2.0 4 votes vote down vote up
protected static FeatureCollection insertFeatures( String schema, String table, String streamId, FeatureCollection collection,
                                                List<FeatureCollection.ModificationFailure> fails,
                                                List<Feature> inserts, Connection connection)
        throws SQLException {

    final PreparedStatement insertStmt = createInsertStatement(connection,schema,table);
    final PreparedStatement insertWithoutGeometryStmt = createInsertWithoutGeometryStatement(connection,schema,table);

    insertStmt.setQueryTimeout(TIMEOUT);
    insertWithoutGeometryStmt.setQueryTimeout(TIMEOUT);

    for (int i = 0; i < inserts.size(); i++) {

        String fId = "";
        try {
            int rows = 0;
            final Feature feature = inserts.get(i);
            fId = feature.getId();

            final PGobject jsonbObject= featureToPGobject(feature);

            if (feature.getGeometry() == null) {
                insertWithoutGeometryStmt.setObject(1, jsonbObject);
                rows = insertWithoutGeometryStmt.executeUpdate();
            } else {
                insertStmt.setObject(1, jsonbObject);
                final WKBWriter wkbWriter = new WKBWriter(3);
                Geometry jtsGeometry = feature.getGeometry().getJTSGeometry();
                //Avoid NAN values
                assure3d(jtsGeometry.getCoordinates());
                insertStmt.setBytes(2, wkbWriter.write(jtsGeometry));

                rows = insertStmt.executeUpdate();
            }

            if(rows == 0) {
                fails.add(new FeatureCollection.ModificationFailure().withId(fId).withMessage(INSERT_ERROR_GENERAL));
            }else
                collection.getFeatures().add(feature);

        } catch (Exception e) {
            if((e instanceof SQLException && ((SQLException)e).getSQLState() != null
                    && ((SQLException)e).getSQLState().equalsIgnoreCase("42P01"))){
                insertStmt.close();
                insertWithoutGeometryStmt.close();
                connection.close();
                throw new SQLException(e);
            }

            fails.add(new FeatureCollection.ModificationFailure().withId(fId).withMessage(INSERT_ERROR_GENERAL));
            logException(e,streamId,i, LOG_EXCEPTION_INSERT);
        }
    }

    return collection;
}
 
Example #6
Source File: DatabaseStreamWriter.java    From xyz-hub with Apache License 2.0 4 votes vote down vote up
protected static FeatureCollection updateFeatures( String schema, String table, String streamId, FeatureCollection collection,
                                                List<FeatureCollection.ModificationFailure> fails,
                                                List<Feature> updates, Connection connection,
                                                boolean handleUUID)
        throws SQLException {

    final PreparedStatement updateStmt = createUpdateStatement(connection, schema, table, handleUUID);
    final PreparedStatement updateWithoutGeometryStmt = createUpdateWithoutGeometryStatement(connection,schema,table,handleUUID);

    updateStmt.setQueryTimeout(TIMEOUT);
    updateWithoutGeometryStmt.setQueryTimeout(TIMEOUT);

    for (int i = 0; i < updates.size(); i++) {
        String fId = "";
        try {
            final Feature feature = updates.get(i);
            final String puuid = feature.getProperties().getXyzNamespace().getPuuid();
            int rows = 0;

            if (feature.getId() == null) {
                fails.add(new FeatureCollection.ModificationFailure().withId(fId).withMessage(UPDATE_ERROR_ID_MISSING));
                continue;
            }

            fId = feature.getId();

            if (handleUUID && puuid == null){
                fails.add(new FeatureCollection.ModificationFailure().withId(fId).withMessage(UPDATE_ERROR_PUUID_MISSING));
                continue;
            }

            final PGobject jsonbObject= featureToPGobject(feature);

            if (feature.getGeometry() == null) {
                updateWithoutGeometryStmt.setObject(1, jsonbObject);
                updateWithoutGeometryStmt.setString(2, fId);

                if(handleUUID)
                    updateWithoutGeometryStmt.setString(3, puuid);

                rows = updateWithoutGeometryStmt.executeUpdate();
            } else {
                updateStmt.setObject(1, jsonbObject);
                final WKBWriter wkbWriter = new WKBWriter(3);
                Geometry jtsGeometry = feature.getGeometry().getJTSGeometry();
                //Avoid NAN values
                assure3d(jtsGeometry.getCoordinates());
                updateStmt.setBytes(2, wkbWriter.write(jtsGeometry));
                updateStmt.setString(3, fId);

                if(handleUUID) {
                    updateStmt.setString(4, puuid);
                }
                rows = updateStmt.executeUpdate();
            }

            if(rows == 0) {
                fails.add(new FeatureCollection.ModificationFailure().withId(fId).withMessage((handleUUID ? UPDATE_ERROR_UUID : UPDATE_ERROR_NOT_EXISTS)));
            }else
                collection.getFeatures().add(feature);

        } catch (Exception e) {
            fails.add(new FeatureCollection.ModificationFailure().withId(fId).withMessage(UPDATE_ERROR_GENERAL));
            logException(e,streamId,i, LOG_EXCEPTION_UPDATE);
        }
    }

    updateStmt.close();
    updateWithoutGeometryStmt.close();
    connection.close();

    return collection;
}
 
Example #7
Source File: GeometrySerializer.java    From jts with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static WKBWriter getWriter() {
	return sWriter;
}
 
Example #8
Source File: XMLTestWriter.java    From jts with GNU Lesser General Public License v2.1 4 votes vote down vote up
private String getWKTorWKB(Geometry g, boolean useWKT)
{
  if (useWKT)
    return wktWriter.writeFormatted(g);
  return WKBWriter.toHex(wkbWriter.write(g));
}
 
Example #9
Source File: WriterFunctions.java    From jts with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static String writeWKB(Geometry g)
{
  if (g == null) return "";
  return WKBWriter.toHex((new WKBWriter().write(g)));
}