Permalink
Browse files

gis: Added preliminary spatial backend for Oracle; added GEOS routine…

… `fromfile`.

git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@6524 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent 189335a commit ba9fa9844c7996bf1362b9769c2c2fecf760a039 @jbronn jbronn committed Oct 16, 2007
@@ -35,8 +35,16 @@
PostGISField as GeoBackendField, POSTGIS_TERMS as GIS_TERMS, \
create_spatial_db, get_geo_where_clause, gqn, \
ASGML, ASKML, GEOM_SELECT, TRANSFORM, UNION
+ SPATIAL_BACKEND = 'postgis'
+elif settings.DATABASE_ENGINE == 'oracle':
+ from django.contrib.gis.db.backend.oracle import \
+ OracleSpatialField as GeoBackendField, \
+ ORACLE_SPATIAL_TERMS as GIS_TERMS, \
+ create_spatial_db, get_geo_where_clause, gqn, \
+ ASGML, GEOM_SELECT, TRANSFORM, UNION
+ SPATIAL_BACKEND = 'oracle'
else:
- raise NotImplementedError('No Geographic Backend exists for %s' % settings.DATABASE_NAME)
+ raise NotImplementedError('No Geographic Backend exists for %s' % settings.DATABASE_ENGINE)
def geo_quotename(value):
"""
@@ -0,0 +1,12 @@
+"""
+ The Oracle spatial database backend module.
+
+ Please note that WKT support is broken on the XE version, and this will
+ not work.
+"""
+from django.contrib.gis.db.backend.oracle.creation import create_spatial_db
+from django.contrib.gis.db.backend.oracle.field import OracleSpatialField, gqn
+from django.contrib.gis.db.backend.oracle.query import \
+ get_geo_where_clause, ORACLE_SPATIAL_TERMS, \
+ ASGML, GEOM_SELECT, TRANSFORM, UNION
+
@@ -0,0 +1,21 @@
+"""
+ This object provides the database adaptor for Oracle geometries.
+"""
+from cx_Oracle import CLOB
+
+class OracleSpatialAdaptor(object):
+ def __init__(self, geom):
+ "Initializes only on the geometry object."
+ self.wkt = geom.wkt
+
+ def __str__(self):
+ "WKT is used for the substitution value of the geometry."
+ return self.wkt
+
+ def oracle_type(self):
+ """
+ The parameter type is a CLOB because no string (VARCHAR2) greater
+ than 4000 characters will be accepted through the Oracle database
+ API and/or SQL*Plus.
+ """
+ return CLOB
@@ -0,0 +1,8 @@
+from django.db.backends.oracle.creation import create_test_db
+
+def create_spatial_db(test=True, verbosity=1, autoclobber=False):
+ "A wrapper over the Oracle `create_test_db` routine."
+ if not test: raise NotImplementedError('This uses `create_test_db` from db/backends/oracle/creation.py')
+ from django.conf import settings
+ from django.db import connection
+ create_test_db(settings, connection, verbosity, autoclobber)
@@ -0,0 +1,159 @@
+import re
+from types import StringType, UnicodeType
+from django.db import connection
+from django.db.backends.util import truncate_name
+from django.db.models.fields import Field # Django base Field class
+from django.contrib.gis.geos import GEOSGeometry
+from django.contrib.gis.db.backend.util import GeoFieldSQL
+from django.contrib.gis.db.backend.oracle.adaptor import OracleSpatialAdaptor
+from django.contrib.gis.db.backend.oracle.query import ORACLE_SPATIAL_TERMS, TRANSFORM
+
+# Quotename & geographic quotename, respectively.
+qn = connection.ops.quote_name
+def gqn(value):
+ if isinstance(value, UnicodeType): value = value.encode('ascii')
+ return "'%s'" % value
+
+class OracleSpatialField(Field):
+ """
+ The backend-specific geographic field for Oracle Spatial.
+ """
+
+ empty_strings_allowed = False
+
+ def __init__(self, extent=(-180.0, -90.0, 180.0, 90.0), tolerance=0.00005, **kwargs):
+ """
+ Oracle Spatial backend needs to have the extent -- for projected coordinate
+ systems _you must define the extent manually_, since the coordinates are
+ for geodetic systems. The `tolerance` keyword specifies the tolerance
+ for error (in meters).
+ """
+ # Oracle Spatial specific keyword arguments.
+ self._extent = extent
+ self._tolerance = tolerance
+ # Calling the Django field initialization.
+ super(OracleSpatialField, self).__init__(**kwargs)
+
+ def _add_geom(self, style, db_table):
+ """
+ Adds this geometry column into the Oracle USER_SDO_GEOM_METADATA
+ table.
+ """
+
+ # Checking the dimensions.
+ # TODO: Add support for 3D geometries.
+ if self._dim != 2:
+ raise Exception('3D geometries not yet supported on Oracle Spatial backend.')
+
+ # Constructing the SQL that will be used to insert information about
+ # the geometry column into the USER_GSDO_GEOM_METADATA table.
+ meta_sql = style.SQL_KEYWORD('INSERT INTO ') + \
+ style.SQL_TABLE('USER_SDO_GEOM_METADATA') + \
+ ' (%s, %s, %s, %s)\n ' % tuple(map(qn, ['TABLE_NAME', 'COLUMN_NAME', 'DIMINFO', 'SRID'])) + \
+ style.SQL_KEYWORD(' VALUES ') + '(\n ' + \
+ style.SQL_TABLE(gqn(db_table)) + ',\n ' + \
+ style.SQL_FIELD(gqn(self.column)) + ',\n ' + \
+ style.SQL_KEYWORD("MDSYS.SDO_DIM_ARRAY") + '(\n ' + \
+ style.SQL_KEYWORD("MDSYS.SDO_DIM_ELEMENT") + \
+ ("('LONG', %s, %s, %s),\n " % (self._extent[0], self._extent[2], self._tolerance)) + \
+ style.SQL_KEYWORD("MDSYS.SDO_DIM_ELEMENT") + \
+ ("('LAT', %s, %s, %s)\n ),\n" % (self._extent[1], self._extent[3], self._tolerance)) + \
+ ' %s\n );' % self._srid
+ return meta_sql
+
+ def _geom_index(self, style, db_table):
+ "Creates an Oracle Geometry index (R-tree) for this geometry field."
+
+ # Getting the index name, Oracle doesn't allow object
+ # names > 30 characters.
+ idx_name = truncate_name('%s_%s_id' % (db_table, self.column), 30)
+
+ sql = style.SQL_KEYWORD('CREATE INDEX ') + \
+ style.SQL_TABLE(qn(idx_name)) + \
+ style.SQL_KEYWORD(' ON ') + \
+ style.SQL_TABLE(qn(db_table)) + '(' + \
+ style.SQL_FIELD(qn(self.column)) + ') ' + \
+ style.SQL_KEYWORD('INDEXTYPE IS ') + \
+ style.SQL_TABLE('MDSYS.SPATIAL_INDEX') + ';'
+ return sql
+
+ def _post_create_sql(self, style, db_table):
+ """
+ Returns SQL that will be executed after the model has been
+ created.
+ """
+ # Getting the meta geometry information.
+ post_sql = self._add_geom(style, db_table)
+
+ # Getting the geometric index for this Geometry column.
+ if self._index:
+ return (post_sql, self._geom_index(style, db_table))
+ else:
+ return (post_sql,)
+
+ def db_type(self):
+ "The Oracle geometric data type is MDSYS.SDO_GEOMETRY."
+ return 'MDSYS.SDO_GEOMETRY'
+
+ def get_db_prep_lookup(self, lookup_type, value):
+ """
+ Returns field's value prepared for database lookup, accepts WKT and
+ GEOS Geometries for the value.
+ """
+ if lookup_type in ORACLE_SPATIAL_TERMS:
+ # special case for isnull lookup
+ if lookup_type == 'isnull': return GeoFieldSQL([], [])
+
+ # When the input is not a GEOS geometry, attempt to construct one
+ # from the given string input.
+ if isinstance(value, GEOSGeometry):
+ pass
+ elif isinstance(value, (StringType, UnicodeType)):
+ try:
+ value = GEOSGeometry(value)
+ except GEOSException:
+ raise TypeError("Could not create geometry from lookup value: %s" % str(value))
+ else:
+ raise TypeError('Cannot use parameter of %s type as lookup parameter.' % type(value))
+
+ # Getting the SRID of the geometry, or defaulting to that of the field if
+ # it is None.
+ if value.srid is None: srid = self._srid
+ else: srid = value.srid
+
+ # The adaptor will be used by psycopg2 for quoting the WKT.
+ adapt = OracleSpatialAdaptor(value)
+ if srid != self._srid:
+ # Adding the necessary string substitutions and parameters
+ # to perform a geometry transformation.
+ return GeoFieldSQL(['%s(SDO_GEOMETRY(%%s, %s), %%s)' % (TRANSFORM, srid)],
+ [adapt, self._srid])
+ else:
+ return GeoFieldSQL(['SDO_GEOMETRY(%%s, %s)' % srid], [adapt])
+
+ else:
+ raise TypeError("Field has invalid lookup: %s" % lookup_type)
+
+ def get_db_prep_save(self, value):
+ "Prepares the value for saving in the database."
+ if not bool(value):
+ # Return an empty string for NULL -- but this doesn't work yet.
+ return ''
+ if isinstance(value, GEOSGeometry):
+ return OracleSpatialAdaptor(value)
+ else:
+ raise TypeError('Geometry Proxy should only return GEOSGeometry objects.')
+
+ def get_placeholder(self, value):
+ """
+ Provides a proper substitution value for Geometries that are not in the
+ SRID of the field. Specifically, this routine will substitute in the
+ SDO_CS.TRANSFORM() function call.
+ """
+ if isinstance(value, GEOSGeometry) and value.srid != self._srid:
+ # Adding Transform() to the SQL placeholder.
+ return '%s(SDO_GEOMETRY(%%s, %s), %s)' % (TRANSFORM, value.srid, self._srid)
+ elif value is None:
+ return '%s'
+ else:
+ return 'SDO_GEOMETRY(%%s, %s)' % self._srid
@@ -0,0 +1,45 @@
+"""
+ The GeometryColumns and SpatialRefSys models for the Oracle spatial
+ backend.
+
+ It should be noted that Oracle Spatial does not have database tables
+ named according to the OGC standard, so the closest analogs are used.
+ For example, the `USER_SDO_GEOM_METADATA` is used for the GeometryColumns
+ model and the `SDO_COORD_REF_SYS` is used for the SpatialRefSys model.
+"""
+from django.db import models
+from django.contrib.gis.models import SpatialRefSysMixin
+
+class GeometryColumns(models.Model):
+ "Maps to the Oracle USER_SDO_GEOM_METADATA table."
+ table_name = models.CharField(maxlength=32)
+ column_name = models.CharField(maxlength=1024)
+ srid = models.IntegerField(primary_key=True)
+ # TODO: Add support for `diminfo` column (type MDSYS.SDO_DIM_ARRAY).
+ class Meta:
+ db_table = 'USER_SDO_GEOM_METADATA'
+
+ @classmethod
+ def table_name_col(self):
+ return 'table_name'
+
+ def __unicode__(self):
+ return '%s - %s (SRID: %s)' % (self.table_name, self.column_name, self.srid)
+
+class SpatialRefSys(models.Model, SpatialRefSysMixin):
+ "Maps to the Oracle MDSYS.CS_SRS table."
+ cs_name = models.CharField(maxlength=68)
+ srid = models.IntegerField(primary_key=True)
+ auth_srid = models.IntegerField()
+ auth_name = models.CharField(maxlength=256)
+ wktext = models.CharField(maxlength=2046)
+ #cs_bounds = models.GeometryField()
+
+ class Meta:
+ # TODO: Figure out way to have this be MDSYS.CS_SRS without
+ # having django's quoting mess up the SQL.
+ db_table = 'CS_SRS'
+
+ @property
+ def wkt(self):
+ return self.wktext
@@ -0,0 +1,78 @@
+"""
+ This module contains the spatial lookup types, and the get_geo_where_clause()
+ routine for Oracle Spatial.
+"""
+from django.db import connection
+qn = connection.ops.quote_name
+
+ORACLE_GEOMETRY_FUNCTIONS = {
+ 'contains' : 'SDO_CONTAINS',
+ 'coveredby' : 'SDO_COVEREDBY',
+ 'covers' : 'SDO_COVERS',
+ 'disjoint' : 'SDO_DISJOINT',
+ 'dwithin' : ('SDO_WITHIN_DISTANCE', float),
+ 'intersects' : 'SDO_OVERLAPBDYINTERSECT', # TODO: Is this really the same as ST_Intersects()?
+ 'equals' : 'SDO_EQUAL',
+ 'exact' : 'SDO_EQUAL',
+ 'overlaps' : 'SDO_OVERLAPS',
+ 'same_as' : 'SDO_EQUAL',
+ #'relate' : ('SDO_RELATE', str), # Oracle uses a different syntax, e.g., 'mask=inside+touch'
+ 'touches' : 'SDO_TOUCH',
+ 'within' : 'SDO_INSIDE',
+ }
+
+# This lookup type does not require a mapping.
+MISC_TERMS = ['isnull']
+
+# Assacceptable lookup types for Oracle spatial.
+ORACLE_SPATIAL_TERMS = ORACLE_GEOMETRY_FUNCTIONS.keys()
+ORACLE_SPATIAL_TERMS += MISC_TERMS
+ORACLE_SPATIAL_TERMS = tuple(ORACLE_SPATIAL_TERMS) # Making immutable
+
+def get_geo_where_clause(lookup_type, table_prefix, field_name, value):
+ "Returns the SQL WHERE clause for use in Oracle spatial SQL construction."
+ if table_prefix.endswith('.'):
+ table_prefix = qn(table_prefix[:-1])+'.'
+ field_name = qn(field_name)
+
+ # See if a PostGIS Geometry function matches the lookup type next
+ lookup_info = ORACLE_GEOMETRY_FUNCTIONS.get(lookup_type, False)
+ if lookup_info:
+ # Lookup types that are tuples take tuple arguments, e.g., 'relate' and
+ # 'dwithin' lookup types.
+ if isinstance(lookup_info, tuple):
+ # First element of tuple is lookup type, second element is the type
+ # of the expected argument (e.g., str, float)
+ func, arg_type = lookup_info
+
+ # Ensuring that a tuple _value_ was passed in from the user
+ if not isinstance(value, tuple) or len(value) != 2:
+ raise TypeError('2-element tuple required for %s lookup type.' % lookup_type)
+
+ # Ensuring the argument type matches what we expect.
+ if not isinstance(value[1], arg_type):
+ raise TypeError('Argument type should be %s, got %s instead.' % (arg_type, type(value[1])))
+
+ if func == 'dwithin':
+ # TODO: test and consider adding different distance options.
+ return "%s(%s, %%s, 'distance=%s')" % (func, table_prefix + field_name, value[1])
+ else:
+ return "%s(%s, %%s, %%s) = 'TRUE'" % (func, table_prefix + field_name)
+ else:
+ # Returning the SQL necessary for the geometry function call. For example:
+ # SDO_CONTAINS("geoapp_country"."poly", SDO_GEOMTRY('POINT(5 23)', 4326)) = 'TRUE'
+ return "%s(%s, %%s) = 'TRUE'" % (lookup_info, table_prefix + field_name)
+
+ # Handling 'isnull' lookup type
+ if lookup_type == 'isnull':
+ return "%s%s IS %sNULL" % (table_prefix, field_name, (not value and 'NOT ' or ''))
+
+ raise TypeError("Got invalid lookup_type: %s" % repr(lookup_type))
+
+ASGML = 'SDO_UTIL.TO_GMLGEOMETRY'
+UNION = 'SDO_AGGR_UNION'
+TRANSFORM = 'SDO_CS.TRANSFORM'
+
+# Want to get SDO Geometries as WKT (much easier to instantiate GEOS proxies
+# from WKT than SDO_GEOMETRY(...) strings ;)
+GEOM_SELECT = 'SDO_UTIL.TO_WKTGEOMETRY(%s)'
@@ -119,7 +119,7 @@ def get_db_prep_lookup(self, lookup_type, value):
# Adding the necessary string substitutions and parameters
# to perform a geometry transformation.
return GeoFieldSQL(['%s(%%s,%%s)' % TRANSFORM],
- [adapt, self._srid])
+ [adapt, self._srid])
else:
return GeoFieldSQL(['%s'], [adapt])
else:
@@ -26,8 +26,8 @@ class Meta:
db_table = 'geometry_columns'
@classmethod
- def table_name(self):
- "Class method for returning the table name field for this model."
+ def table_name_col(self):
+ "Class method for returning the table name column for this model."
return 'f_table_name'
def __unicode__(self):
@@ -105,8 +105,8 @@
# These are the PostGIS-customized QUERY_TERMS -- a list of the lookup types
# allowed for geographic queries.
-POSTGIS_TERMS = list(POSTGIS_OPERATORS.keys()) # Getting the operators first
-POSTGIS_TERMS += list(POSTGIS_GEOMETRY_FUNCTIONS.keys()) # Adding on the Geometry Functions
+POSTGIS_TERMS = POSTGIS_OPERATORS.keys() # Getting the operators first
+POSTGIS_TERMS += POSTGIS_GEOMETRY_FUNCTIONS.keys() # Adding on the Geometry Functions
POSTGIS_TERMS += MISC_TERMS # Adding any other miscellaneous terms (e.g., 'isnull')
POSTGIS_TERMS = tuple(POSTGIS_TERMS) # Making immutable
Oops, something went wrong.

0 comments on commit ba9fa98

Please sign in to comment.