private void insertAfterEdge ( Geometry geom , GeometryIndex index , Coordinate coordinate ) throws GeometryIndexNotFoundException { // First we check the geometry type: if ( ! Geometry . LINE_STRING . equals ( geom . getGeometryType ( ) ) && ! Geometry . LINEAR_RING . equals ( geom . getGeometryType ( ) ) ) { throw new GeometryIndexNotFoundException ( "Could not match index with given geometry." ) ; } if ( index . getValue ( ) < 0 ) { throw new GeometryIndexNotFoundException ( "Cannot insert in a negative index." ) ; } // Then we check if the edge exists: if ( geom . getCoordinates ( ) != null && geom . getCoordinates ( ) . length > index . getValue ( ) + 1 ) { // Inserting on edges allows only to insert on existing edges. No adding at the end: Coordinate [ ] result = new Coordinate [ geom . getCoordinates ( ) . length + 1 ] ; int count = 0 ; for ( int i = 0 ; i < geom . getCoordinates ( ) . length ; i ++ ) { if ( i == ( index . getValue ( ) + 1 ) ) { result [ i ] = coordinate ; count ++ ; } result [ i + count ] = geom . getCoordinates ( ) [ i ] ; } geom . setCoordinates ( result ) ; } else { throw new GeometryIndexNotFoundException ( "Cannot insert a vertex into an edge that does not exist." ) ; } }
Insert a point into a given edge . There can be only edges if there are at least 2 points in a LineString geometry .