I have a table that contains buildings in a line geometry where a line starts and ends at the same point. Is there away to convert lines to polygons in Oracle Spatial?

  • Would you mind accepting the answer or is my proposed solution not sufficient for your needs?
    – Lars
    Commented Apr 3, 2017 at 5:38
  • @Lars I am so sorry for not replying to you earlier because I did not have the time to test your solution. I have now tested the "UPDATE - Better solution" version and it worked like a charm! Thank you very much for your help. :)
    – GIS_DBA
    Commented Apr 19, 2017 at 15:07

1 Answer 1


This solution works for linestrings with not too much vertices (which should be the case for most buildings):

CREATE OR REPLACE FUNCTION line_to_polygon (closedLineGeom SDO_GEOMETRY) 
    wktStringLine VARCHAR2(4000);
    wktStringPolygon VARCHAR2(4000);
    wktStringLine := SDO_UTIL.TO_WKTGEOMETRY(closedLineGeom);
    wktStringPolygon := replace(wktStringLine, 'LINESTRING (', 'POLYGON ((');
    wktStringPolygon := wktStringPolygon || ')';

The function just replaces the linestring WKT (SDO_UTIL.FROM_WKTGEOMETRY) with polygon WKT and returns it as a geometry (SDO_UTIL.TO_WKTGEOMETRY). Note that this function will break for linestrings with a lot of vertices because the length of a VARCHAR2 is limited to 4000. Also you have to make sure that the line is really closed!

UPDATE - Better solution

The following is a better solution that does not have the previous limitation:

UPDATE your_table_name x
x.geometry_column_name.SDO_GTYPE = 2003,
x.geometry_column_name.SDO_ELEM_INFO = SDO_ELEM_INFO_ARRAY(1,1003,1);

Of course you still have to make sure that your linestrings are really closed!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.