Oracle spatial search within distance

后端 未结 4 1346
予麋鹿
予麋鹿 2021-02-03 15:59

I have the following table Cities:

ID(int),City(char),latitude(float),longitude(float).

Now based on a user`s longitude(ex:44.8) and latitude(e

4条回答
  •  灰色年华
    2021-02-03 16:19

    If you really want to use SDO_WITHIN_DISTANCE, you need to create a column of type SDO_GEOMETRY in your Cities table, fill spatial index metadata and create spatial index:

    1. SDO_GEOMETRY column:

      CREATE TABLE MYTABLE(
      ...,
      GEOLOC MDSYS.SDO_GEOMETRY,
      ...
      );
      
    2. Spatial Index Metadata:

      INSERT INTO USER_SDO_GEOM_METADATA (TABLE_NAME, COLUMN_NAME, DIMINFO, SRID)
      VALUES ('MYTABLE' /*your table name*/, 'GEOLOC', /*your spatial column name*/
          SDO_DIM_ARRAY(SDO_DIM_ELEMENT('X', -180, 180, 1),
                    SDO_DIM_ELEMENT('Y', -90, 90, 1)),
                    8307);
      
    3. Create Spatial Index:

      CREATE INDEX MY_SPATIAL_IDX ON MYTABLE (GEOLOC)
      tablespace SomeTablespace; -- optional
      
    4. Now substitute GEOLOC where you said [I don't know].

    This was to answer your question. Others gave you a hint that using Oracle spatial for such a simple task is on overkill. In this case, I tend to agree, because you can do a simple boxing in WHERE clause to cut out cities not in rectangular box with center of your starting point and size of your search distance; however sometimes you will need an intelligence of R-tree index. Anyway, their solutions have 2 major problems:

    a. They use Great Circle approach to calculate distance between the points. It is too rough, you need to use ellipsoid approach to get more accurate results. Googling provides answer immediately like this.

    b. If you would program the ellipsoid distance algorithm in PL/SQL, you will find it prohibitely slow. Solution is to move this logic to Java or C++ and make it callable from Oracle (there is standard way of doing that).

提交回复
热议问题