Question

I'm trying to create geom table based on another table having lat lon. I'm trying to create two column one with Spherical Mercator( SRID 4326, geographic coordinate system) and other column with projected coordinate system (SRID 3857) in the table. Below is my query.

create table KP_SPTL(
                            select mdsys.sdo_geometry(
                                                        2003, 
                                                        4326, 
                                                        NULL, 
                                                        SDO_ELEM_INFO_ARRAY(1,3,3), 
                                                        SDO_ORDINATE_ARRAY(B.LL_LAT, B.LL_LON, B.UR_LAT, B.UR_LON)
                                                      ) as KP_GCS,
                              select mdsys.sdo_geometry(
                                                        2003, 
                                                        3857, 
                                                        NULL, 
                                                        SDO_ELEM_INFO_ARRAY(1,3,3), 
                                                        SDO_ORDINATE_ARRAY(B.LL_LAT, B.LL_LON, B.UR_LAT, B.UR_LON)
                                                      ) as KP_PCS,
                                                      B.COMPANY, B.ADDRS,B_CDE
                              FROM KP_STAGE B);

I get following error

Error report:
SQL Error: ORA-00936: missing expression
00936. 00000 -  "missing expression"

However if I remove the second select statement it works fine, I mean if the sql query is as below

create table KP_SPTL(
                            select mdsys.sdo_geometry(
                                                        2003, 
                                                        4326, 
                                                        NULL, 
                                                        SDO_ELEM_INFO_ARRAY(1,3,3), 
                                                        SDO_ORDINATE_ARRAY(B.LL_LAT, B.LL_LON, B.UR_LAT, B.UR_LON)
                                                      ) as KP_GCS,
                                                      B.COMPANY, B.ADDRS,B_CDE
                              FROM KP_STAGE B);

How to use the second select statement, if its normal query we can use select from statement. but how to use in this case?

Was it helpful?

Solution 2

I don't know what is oracle Spatial At all. but just by Lookin SQL, I think, the below would work.!

mdsys.sdo_geometry() returns a TYPE sdo_geometry, and it can be used in A SELECT clause multiple times. We do not need to accompany a SELECT every time.

Its is like SELECT B.COMPANY, B.ADDRS,B_CDE .. you don't need to prefix a SELECT always!

General syntax of an SQL could be SELECT <types/columns> FROM <view/table> WHERE <conditions>

create table KP_SPTL(
                            select mdsys.sdo_geometry(
                                                        2003, 
                                                        4326, 
                                                        NULL, 
                                                        SDO_ELEM_INFO_ARRAY(1,3,3), 
                                                        SDO_ORDINATE_ARRAY(B.LL_LAT, B.LL_LON, B.UR_LAT, B.UR_LON)
                                                      ) as KP_GCS,
                              mdsys.sdo_geometry(
                                                        2003, 
                                                        3857, 
                                                        NULL, 
                                                        SDO_ELEM_INFO_ARRAY(1,3,3), 
                                                        SDO_ORDINATE_ARRAY(B.LL_LAT, B.LL_LON, B.UR_LAT, B.UR_LON)
                                                      ) as KP_PCS,
                                                      B.COMPANY, B.ADDRS,B_CDE
                              FROM KP_STAGE B);

OTHER TIPS

Your statement will not do what you expect for several reasons.

As I understand, your source table KP_STAGE B contains four columns: LL_LAT, LL_LON, UR_LAT, UR_LON which presumably are the latitude and longitude of the two corners (lower left and upper right) of some rectangle. From what you say, those coordinates are in long/lat WGS84 (i.e. SRID 4326)

And you want a new table that contains two geometry objects, one containing a rectangle in long/lat (SRID 4326), and its equivalent in SRID 3857 (the spherical mercator projection used by Google and others).

There are three problems with your syntax:

1) The order of the ordinates is always X then Y, which for geographic coordinates (latitude/longitude) means they must be specified in longitude first, then latitude. This is the way all GIS systems store geographic data. So use this:

sdo_geometry(
  2003, 4326, NULL, 
  SDO_ELEM_INFO_ARRAY(1,1003,3),                                                          
  SDO_ORDINATE_ARRAY(
    LL_LON, LL_LAT, 
    UR_LON, UR_LAT
  ) 
) as KP_GCS,

2) The element type for a polygon must be one of 1003 or 2003, not just 3. This is so that the database can differentiate between an "outer ring" (= a regular polygon) and a "inner ring" (= a hole).

3) Most important: you cannot construct a geometry in a different coordinate system (in your case 3857) by just setting a different SRID into the spatial object. You need to transform it (project it) to the proper coordinate system using the SDO_CS.TRANSFORM() call, like this:

sdo_cs.transform (
  sdo_geometry(
    2003, 4326, NULL, 
    SDO_ELEM_INFO_ARRAY(1,1003,3),                                                          
    SDO_ORDINATE_ARRAY(
      LL_LON, LL_LAT, 
      UR_LON, UR_LAT
    ) 
  ),
  3857
) as KP_PCS

Note that this will not return a rectangle in the simple two-corner notation: while the result will be graphically rendered as a rectangle, its internal notation is that of a full polygon.

So the full and correct answer will be:

create table KP_SPTL as
select  sdo_geometry (
          2003, 4326, NULL, 
          SDO_ELEM_INFO_ARRAY (1,1003,3),                                                          
          SDO_ORDINATE_ARRAY (
            LL_LON, LL_LAT, 
            UR_LON, UR_LAT
          ) 
        ) as KP_GCS,
        sdo_cs.transform (
          sdo_geometry(
            2003, 4326, NULL, 
            SDO_ELEM_INFO_ARRAY(1,1003,3),                                                          
            SDO_ORDINATE_ARRAY(
              LL_LON, LL_LAT, 
              UR_LON, UR_LAT
            ) 
          ),
          3857
        ) as KP_PCS,
        COMPANY, ADDRS, B_CDE
from KB_STAGE_B;

Or simpler:

create table KP_SPTL as
with t as (
  select  COMPANY, ADDRS, B_CDE,
          sdo_geometry (
            2003, 4326, NULL, 
            SDO_ELEM_INFO_ARRAY (1,1003,3),                                                          
            SDO_ORDINATE_ARRAY (
              LL_LON, LL_LAT, 
              UR_LON, UR_LAT
            ) 
          ) as KP_GCS,
  from KB_STAGE_B
)
select KP_GCS, sdo_cs.transform(KP_GCS, 3857) KP_PCS, COMPANY, ADDRS, B_CDE
from t;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top