Skip to Main Content
  • Questions
  • How to convert a list of latitudes and longitudes into a sdo_geometry polygon

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Vatsa.

Asked: September 23, 2024 - 6:58 pm UTC

Last updated: September 30, 2024 - 7:06 am UTC

Version: 24.1.1

Viewed 1000+ times

You Asked

Greetings,

I have a table that stores multiple locations (lat and lon) for a project. I want to convert this list of lat and lons into a sdo_geometry so that I can plot a polygon on the map. I created this procedure to insert the sdo_geomety column in a temporary table. But I am getting PL/SQL: numeric or value error: character to number conversion error. Not sure what I am doing wrong here. Any help will be greatly appreciated.
Thanks
Vatsa.
Here is the procedure:
create or replace procedure "GEN_POLYGON_FOR_PROJ" (
p_project_number in number )
as
l_poly sdo_geometry;
l_loc_list varchar2(2000) := NULL;

Cursor L (p_proj number) is
Select to_char(lon)||','||to_char(lat) as loc
FROM PM_PROJECT_LOCATION
where proj_id = p_proj ;
begin
FOR I in L(p_project_number) Loop

l_loc_list := l_loc_list ||','|| I.loc ;
dbms_output.put_line(l_loc_list) ;

END LOOP;
dbms_output.put_line(substr(l_loc_list,2)) ;

l_poly := mdsys.sdo_geometry(2003, 1041002, null, sdo_elem_info_array(1, 1003, 1), MDSYS.sdo_ordinate_array(substr(l_loc_list,2))) ;

delete from VR_PROJECT_POLYGONS where project_number = p_project_number;
insert into VR_PROJECT_POLYGONS(PROJECT_NUMBER,PROJ_POLYGON) values (p_project_number,l_poly);
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line(SQLERRM) ;
end "GEN_POLYGON_FOR_PROJ";

and Connor said...

select
sdo_geometry(
  2001,
  4326,
  sdo_point_type(p.lon, p.lat, null),
  null,
  null))
from PM_PROJECT_LOCATION p;


should get you going.

Rating

  (4 ratings)

Comments

Thanks Connor

Vatsa Ramanathan, September 25, 2024 - 12:16 pm UTC

Thanks for your answer Connor. But that will give the geometry for one location of a project. I have 4 sets of lat and long for that project and I want to draw a polygon using those four locations. Could you please let me know how to do that? Thanks again for your help!
Chris Saxon
September 25, 2024 - 2:15 pm UTC

Where are you getting these lat & long values? What's the end result you're expecting? What have you tried?

Thanks for your response.

Vatsa Ramanathan, September 25, 2024 - 2:44 pm UTC

Hi Connor,
I am getting the lat and long values for a project from a table called PM_PROJECT_LOCATION which has the project number and multiple rows with lat and long values for a project. I want to use those lat and long values to generate a polygon to be shown on a map.

Thanks for your response.

Vatsa Ramanathan, September 25, 2024 - 2:55 pm UTC

I get these four points when I query the locations and plot it on the map. I want to make a polygon with these locations as it's corners.

Connor McDonald
September 26, 2024 - 7:00 am UTC

You need to build an array from the points, eg

declare
t sdo_ordinate_array := sdo_ordinate_array();
begin
t.extend();
t(t.count) := first_lon_value;
t.extend();
t(t.count) := first_lat_value;

t.extend();
t(t.count) := second_lon_value;
t.extend();
t(t.count) := second_lat_value;

end;
/

and so forth

my_polygon := SDO_GEOMETRY(2003,4326,null,SDO_ELEM_INFO_ARRAY(1,1003,1),t);

should then give you a polygon


Thanks Connor

Vatsa Ramanathan, September 26, 2024 - 1:50 pm UTC

That worked like a charm. THanks a lot for your guidance and help!
-Vatsa
Connor McDonald
September 30, 2024 - 7:06 am UTC

Glad we could help

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library