Skip to content

Instantly share code, notes, and snippets.

@plamen9
Last active October 21, 2022 10:08
Show Gist options
  • Save plamen9/6db440412349fb808f9196e941bfbb03 to your computer and use it in GitHub Desktop.
Save plamen9/6db440412349fb808f9196e941bfbb03 to your computer and use it in GitHub Desktop.
Converting GeoJSON, SDO_GEOMETRY, Latitude and Longitude
-- *** Types conversion examples ***
 
-- Convert SDO_GEOMETRY to GeoJSON
select apex_json.stringify( 
 mdsys.sdo_geometry( 2001, 4326, sdo_point_type( 10, 50, null ), null, null ) 
 )
from dual;
select apex_json.stringify( 
 MDSYS.SDO_GEOMETRY(2002, NULL, NULL, 
 MDSYS.SDO_ELEM_INFO_ARRAY(1, 2, 1), 
 MDSYS.SDO_ORDINATE_ARRAY(-118.4899722, 34.2098056, -115.15225, 36.0800556, -116.1596528, 33.6266667)) 
 )
from dual;
select c.geom.Get_GeoJson() GeoJSON_converted
 from (select MDSYS.SDO_GEOMETRY(2002, NULL, NULL, 
 MDSYS.SDO_ELEM_INFO_ARRAY(1, 2, 1), 
 MDSYS.SDO_ORDINATE_ARRAY(-118.4899722, 34.2098056, -115.15225, 36.0800556, -116.1596528, 33.6266667)) geom 
 from dual) c;
select SDO_UTIL.TO_GEOJSON(
 MDSYS.SDO_GEOMETRY(2002, NULL, NULL, 
 MDSYS.SDO_ELEM_INFO_ARRAY(1, 2, 1), 
 MDSYS.SDO_ORDINATE_ARRAY(-118.4899722, 34.2098056, -115.15225, 36.0800556, -116.1596528, 33.6266667))) geom 
from dual;
-- All of the previous 3 methods to convert SDO_GEOMETRY to GeoJSON will return the same result
-- { "type": "LineString", "coordinates": [ [-118.4899722, 34.2098056], [-115.15225, 36.0800556], [-116.1596528, 33.6266667] ] }
-- Convert GeoJSON to SDO_GEOMETRY
select SDO_UTIL.FROM_JSON('{"type":"Point","coordinates":[10,50]}') from dual;
select SDO_UTIL.FROM_GEOJSON('{"type":"Point","coordinates":[10,50]}') from dual;
-- both functions provide exactly the same result
select SDO_UTIL.FROM_JSON('{"type":"LineString","coordinates":[[-118.4899722,34.2098056],[-115.15225,36.0800556],[-116.1596528,33.6266667]]}') from dual;
select SDO_UTIL.FROM_GEOJSON('{"type":"LineString","coordinates":[[-118.4899722,34.2098056],[-115.15225,36.0800556],[-116.1596528,33.6266667]]}') from dual;
-- both functions provide exactly the same result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment