Converting GeoJSON to SDO_GEOMETRY
April 21, 2012 GeoJSON GIS javascript Oracle Oracle Spatial SDO_GEOMETRY Spatial
For a customer I had to store geospatial data in an Oracle Spatial database. The source data was in the GeoJSON format and thus had to converted to the SDO_GEOMETRY format.
The following JavaScript code snippet gives some code to convert GeoJSON Points and LineStrings to the SDO_GEOMETRY format. Polygons weren’t involved in this project, but should not be too hard to implement.
// Serialization (and scaling) of coordinates
function strCoordinates(coordinates) {
var strs = [];
for (var i = 0; i < coordinates.length; ++i) {
var coordinate = coordinates[i];
var str = strCoordinate(coordinate);
strs.push(str);
}
return strs.join(',');
}
function strCoordinate(coordinate) {
return coordinate[0] + ',' + coordinate[1];
}
// Converting GeoJSON to SDO_GEOMETRY
function pointToSdoGeom(geoJson) {
return 'SDO_GEOMETRY(2001, 28992, SDO_POINT_TYPE(' + strCoordinate(geoJson.coordinates) + ', NULL), NULL, NULL)';
}
function lineStringToSdoGeom(geoJson) {
return 'SDO_GEOMETRY(2002, 28992, NULL, SDO_ELEM_INFO_ARRAY(1, 2, 1), SDO_ORDINATE_ARRAY(' + strCoordinates(geoJson.coordinates) + '))';
}
function arcToSdoGeom(geoJson) {
return 'SDO_GEOMETRY(2002, 28992, NULL, SDO_ELEM_INFO_ARRAY(1, 2, 2), SDO_ORDINATE_ARRAY(' + strCoordinates(geoJson.coordinates) + '))';
}
// Usage example
var geoJson = { "type": "Point", "coordinates": [ 1, 1 ] };
var sdoGeom = pointToSdoGeom(geoJson);
console.log(sdoGeom); // SDO_GEOMETRY(2001, 28992, SDO_POINT_TYPE(1,1, NULL), NULL, NULL)
geoJson = { "type": "LineString", "coordinates": [ [ 1, 1 ], [ 2, 2 ] ] };
sdoGeom = lineStringToSdoGeom(geoJson);
console.log(sdoGeom); // SDO_GEOMETRY(2002, 28992, NULL, SDO_ELEM_INFO_ARRAY(1, 2, 1), SDO_ORDINATE_ARRAY(1,1,2,2))
geoJson = { "type": "LineString", "coordinates": [ [ 1, 1 ], [ 2, 2 ], [ 3, 1 ] ] };
sdoGeom = arcToSdoGeom(geoJson); // HACK: GeoJSON does not support arcs, so we use a LineString to store the arc
console.log(sdoGeom); // SDO_GEOMETRY(2002, 28992, NULL, SDO_ELEM_INFO_ARRAY(1, 2, 2), SDO_ORDINATE_ARRAY(1,1,2,2,3,1))
Needless to say, the used SRID is 28992, Amersfoort / RD New.