Oracle Spatial Based Analysis

Spatial AI provides a basic Python API for geometry data in Oracle Spatial database.

This enables Spatial database-based analysis using the Python API provided by the oraclesai.data package which is implemented in the SpatialDataFrame class. It provides data access to Oracle Spatial and also data processing functionalities from Oracle Spatial.

These analyses include basic spatial operations, such as geometry computation, spatial query, aggregation, summary, join, and optimization. They are provided by Python calls to the Oracle Spatial database, which also enables in-database processing.

If the dataset is from the database, then the methods of the SpatialDataFrame class will push all the operations to Oracle Spatial and do in-database processing. Otherwise, all the operations are executed in memory.

The following table describes some of the techniques for spatial analysis.

Operation Description
aggregate Operation is executed over columns, and the supported functions are count, avg, sum, and mbr. The result is a SpatialDataFrame object.
area Returns a new SpatialDataFrame instance containing the areas of each geometry.
buffer Constructs a buffer around each geometry. The result contains the same rows and columns but with the geometry buffered.
combine Combines the geometries from the current SpatialDataFrame instance with the geometries from the parameter tgt. The result is a SpatialDataFrame object with the combined geometries.
crs Returns the coordinate reference system associated to the geometry.
groupby Involves splitting a SpatialDataFrame object by the given criteria, applying a function, and combining the results.
length Returns a new SpatialDataFrame object with the lengths of the geometries.
merge Joins two instances of SpatialDataFrame by comparing the joining keys. The result is another SpatialDataFrame with columns from both the instances. It allows to specify the joining keys and how the join is executed. It is also possible to define the geometry column for the resulting object.
nearest_neighbors Returns a SpatialDataFrame object with observations that are closer to a given location, which can be either a shapely geometry or another instance of SpatialDataFrame. For the latter, the result will contain observations containing information from both SpatialDataFrame objects.
relate Executes a primary spatial filter based on a specified spatial operator from {anyinteract, inside, contains, equal, coveredby, on, covers, overlapbyintersect, overlapbydisjoint}. It returns a new SpatialDataFrame instance.
spatial_join

Joins two instances of SpatialDataFrame by a specified interaction defined in the spatial_op parameter, which can contain any spatial interaction supported by the relate operation.

The resulting object contains data from both input objects and the geometry from the calling instance is used as the geometry in the result.

total_bounds The total_bounds property calculates the minimum bounding rectangle enclosing all the data. It returns the result as a tuple with the following elements (min_x, min_y, max_x, max_y).
to_crs Returns a new SpatialDataFrame object with the geometries in the specified CRS
within_distance Returns a SpatialDataFrame object containing only observations located within a certain distance from a query window specified as a shapely query window or another.
distance Returns a new SpatialDataFrame with the distance between the geometries of the current instance and the parameter qry_win.

See the SpatialDataFrame class in Python API Reference for Oracle Spatial AI for more information.

The following example creates an instance of SpatialDataFrame containing the name and location of schools in Los Angeles. It uses a DBSpatialDataset to get the data from the schools table.

import oml
from oraclesai import SpatialDataFrame, DBSpatialDataset

schools = SpatialDataFrame.create(DBSpatialDataset(table='schools', schema='oml_user'))

Then, using the block_groups SpatialDataFrame, the within_distance and the groupby operations, the example computes the number of schools within two kilometers of each block group, and stores the result in another SpatialDataFrame. The index comes from the column GEOID.

schools_counts = block_groups.within_distance(schools, distance=2000).groupby('GEOID').aggregate(count={'GEOID': 'SCHOOLS_COUNT'})
print(schools_counts["SCHOOLS_COUNT"])

By printing the resulting SpatialDataFrame, note the SCHOOLS_COUNT column, which indicates the number of schools within two kilometers from a block group.

      SCHOOLS_COUNT                                           geometry
0                 5  POLYGON ((-118.29131 34.26285, -118.29132 34.2...
1                 5  POLYGON ((-118.30075 34.25961, -118.30229 34.2...
2                 4  POLYGON ((-118.30076 34.26321, -118.30075 34.2...
3                 4  POLYGON ((-118.30320 34.27333, -118.30275 34.2...
4                 5  POLYGON ((-118.29069 34.27071, -118.29078 34.2...
...             ...                                                ...
3412             15  POLYGON ((-118.34312 33.99558, -118.34343 33.9...
3413             13  POLYGON ((-118.34930 33.99942, -118.34929 33.9...
3414             14  POLYGON ((-118.34432 33.99074, -118.34486 33.9...
3415             26  POLYGON ((-118.25165 34.08038, -118.25124 34.0...
3416             22  POLYGON ((-118.51849 34.18498, -118.51849 34.1...