Changeset 5397
- Timestamp:
- 05/31/07 20:15:35 (2 years ago)
- Files:
-
- django/branches/gis/django/contrib/gis/db/models/fields/__init__.py (modified) (3 diffs)
- django/branches/gis/django/contrib/gis/db/models/GeoMixin.py (modified) (2 diffs)
- django/branches/gis/django/contrib/gis/db/models/postgis.py (modified) (1 diff)
- django/branches/gis/django/contrib/gis/gdal (added)
- django/branches/gis/django/contrib/gis/gdal/DataSource.py (added)
- django/branches/gis/django/contrib/gis/gdal/Feature.py (added)
- django/branches/gis/django/contrib/gis/gdal/Field.py (added)
- django/branches/gis/django/contrib/gis/gdal/__init__.py (added)
- django/branches/gis/django/contrib/gis/gdal/Layer.py (added)
- django/branches/gis/django/contrib/gis/gdal/libgdal.py (added)
- django/branches/gis/django/contrib/gis/gdal/LICENSE (added)
- django/branches/gis/django/contrib/gis/gdal/OGRError.py (added)
- django/branches/gis/django/contrib/gis/gdal/OGRGeometry.py (added)
- django/branches/gis/django/contrib/gis/gdal/SpatialReference.py (added)
- django/branches/gis/django/contrib/gis/models.py (modified) (5 diffs)
- django/branches/gis/django/contrib/gis/tests/__init__.py (modified) (1 diff)
- django/branches/gis/django/contrib/gis/tests/test_gdal_ds.py (added)
- django/branches/gis/django/contrib/gis/tests/test_gdal_geom.py (added)
- django/branches/gis/django/contrib/gis/tests/test_gdal_srs.py (added)
- django/branches/gis/django/contrib/gis/tests/test_point (added)
- django/branches/gis/django/contrib/gis/tests/test_point/test_point.dbf (added)
- django/branches/gis/django/contrib/gis/tests/test_point/test_point.prj (added)
- django/branches/gis/django/contrib/gis/tests/test_point/test_point.shp (added)
- django/branches/gis/django/contrib/gis/tests/test_point/test_point.shx (added)
- django/branches/gis/django/contrib/gis/tests/test_poly (added)
- django/branches/gis/django/contrib/gis/tests/test_poly/test_poly.dbf (added)
- django/branches/gis/django/contrib/gis/tests/test_poly/test_poly.prj (added)
- django/branches/gis/django/contrib/gis/tests/test_poly/test_poly.shp (added)
- django/branches/gis/django/contrib/gis/tests/test_poly/test_poly.shx (added)
- django/branches/gis/django/contrib/gis/tests/test_spatialrefsys.py (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/gis/django/contrib/gis/db/models/fields/__init__.py
r5382 r5397 7 7 8 8 #TODO: Flesh out widgets. 9 #TODO: geosoperations through fields as proxy.9 #TODO: GEOS and GDAL/OGR operations through fields as proxy. 10 10 #TODO: pythonic usage, like "for point in zip.polygon" and "if point in polygon". 11 11 … … 39 39 40 40 Takes the style object (provides syntax highlighting) as well as the 41 database table and field. The dimensions can be specified via 42 the dim keyword as well. 41 database table and field. 43 42 """ 44 43 sql = style.SQL_KEYWORD('SELECT ') + \ … … 82 81 super(GeometryField, self).contribute_to_class(cls, name) 83 82 84 # Adding the WKT accessor function for geometry83 # Adding needed accessor functions 85 84 setattr(cls, 'get_%s_geos' % self.name, curry(cls._get_GEOM_geos, field=self)) 85 setattr(cls, 'get_%s_ogr' % self.name, curry(cls._get_GEOM_ogr, field=self, srid=self._srid)) 86 86 setattr(cls, 'get_%s_wkt' % self.name, curry(cls._get_GEOM_wkt, field=self)) 87 87 setattr(cls, 'get_%s_centroid' % self.name, curry(cls._get_GEOM_centroid, field=self)) django/branches/gis/django/contrib/gis/db/models/GeoMixin.py
r5008 r5397 1 1 # GEOS Routines 2 2 from django.contrib.gis.geos import GEOSGeometry, hex_to_wkt, centroid, area 3 from django.contrib.gis.gdal import OGRGeometry, SpatialReference 3 4 4 5 # Until model subclassing is a possibility, a mixin class is used to add … … 12 13 "Gets a GEOS Python object for the geometry." 13 14 return GEOSGeometry(getattr(self, field.attname), 'hex') 15 16 def _get_GEOM_ogr(self, field, srid): 17 "Gets an OGR Python object for the geometry." 18 return OGRGeometry(hex_to_wkt(getattr(self, field.attname)), 19 SpatialReference('EPSG:%d' % srid)) 14 20 15 21 def _get_GEOM_wkt(self, field): django/branches/gis/django/contrib/gis/db/models/postgis.py
r5336 r5397 1 1 # This module is meant to re-define the helper routines used by the 2 2 # django.db.models.query objects to be customized for PostGIS. 3 from copy import copy4 3 from django.db import backend 5 4 from django.db.models.query import LOOKUP_SEPARATOR, find_field, FieldFound, QUERY_TERMS, get_where_clause django/branches/gis/django/contrib/gis/models.py
r4997 r5397 1 1 import re 2 2 from django.db import models 3 4 # Checking for the presence of GDAL 5 try: 6 from django.contrib.gis.gdal import SpatialReference 7 HAS_OSR = True 8 except ImportError: 9 HAS_OSR = False 3 10 4 11 """ … … 6 13 """ 7 14 8 # For pulling out the spheroid from the spatial reference string. 15 # For pulling out the spheroid from the spatial reference string. This 16 # regular expression is used only if the user does not have GDAL installed. 9 17 # TODO: Flattening not used in all ellipsoids, could also be a minor axis, or 'b' 10 18 # parameter. 11 19 spheroid_regex = re.compile(r'.+SPHEROID\[\"(?P<name>.+)\",(?P<major>\d+(\.\d+)?),(?P<flattening>\d{3}\.\d+),') 12 13 # For pulling out the projected coordinate system units. Python regexs are greedy14 # by default, so this should get the units of the projection instead (PROJCS)15 # of the units for the geographic coordinate system (GEOGCS).16 unit_regex = re.compile(r'^PROJCS.+UNIT\[\"(?P<units>[a-z]+)\", ?(?P<conversion>[0-9\.]+), ?(AUTHORITY\[\"(?P<authority>[a-z0-9 \.]+)\",[ ]?\"(?P<code>\d+)\"\])?', re.I)17 20 18 21 # This is the global 'geometry_columns' from PostGIS. … … 21 24 f_table_catalog = models.CharField(maxlength=256) 22 25 f_table_schema = models.CharField(maxlength=256) 23 f_table_name = models.CharField(maxlength=256 )26 f_table_name = models.CharField(maxlength=256, primary_key=True) 24 27 f_geometry_column = models.CharField(maxlength=256) 25 28 coord_dimension = models.IntegerField() … … 30 33 db_table = 'geometry_columns' 31 34 35 def __str__(self): 36 return "%s.%s - %dD %s field (SRID: %d)" % (self.f_table_name, self.f_geometry_column, self.coord_dimension, self.type, self.srid) 37 32 38 # This is the global 'spatial_ref_sys' table from PostGIS. 33 39 # See PostGIS Documentation at Ch. 4.2.1 … … 37 43 auth_srid = models.IntegerField() 38 44 srtext = models.CharField(maxlength=2048) 39 proj4 text = models.CharField(maxlength=2048)45 proj4 = models.CharField(maxlength=2048, db_column='proj4text') 40 46 41 47 class Meta: 42 48 db_table = 'spatial_ref_sys' 49 50 def _cache_osr(self): 51 "Caches a GDAL OSR object for this Spatial Reference." 52 if HAS_OSR: 53 if not hasattr(self, '_srs'): 54 # Trying to get from WKT first 55 try: 56 self._srs = SpatialReference(self.srtext, 'wkt') 57 return 58 except: 59 pass 60 61 # Trying the proj4 text next 62 try: 63 self._srs = SpatialReference(self.proj4, 'proj4') 64 return 65 except: 66 pass 67 68 raise Exception, 'Could not get a OSR Spatial Reference.' 69 else: 70 raise Exception, 'GDAL is not installed!' 71 72 @property 73 def srs(self): 74 self._cache_osr() 75 return self._srs.clone() 43 76 44 77 @property 45 def spheroid(self): 46 "Pulls out the spheroid from the srtext." 47 m = spheroid_regex.match(self.srtext) 48 if m: 49 return (m.group('name'), float(m.group('major')), float(m.group('flattening'))) 78 def ellipsoid(self): 79 """Returns a tuple of the ellipsoid parameters: 80 (semimajor axis, semiminor axis, and inverse flattening).""" 81 if HAS_OSR: 82 # Setting values initially to False 83 self._cache_osr() 84 major = self._srs.semi_major 85 minor = self._srs.semi_minor 86 invflat = self._srs.inverse_flattening 87 return (major, minor, invflat) 50 88 else: 51 return None 89 m = spheroid_regex.match(self.srtext) 90 if m: return (float(m.group('major')), float(m.group('flattening'))) 91 else: return None 52 92 53 93 @property 54 def projected_units(self): 55 "If the spatial reference system is projected, get the units or return None." 56 m = unit_regex.match(self.srtext) 57 if m: 58 if m.group('authority'): 59 authority = (m.group('authority'), int(m.group('code'))) 60 else: 61 authority = None 62 return (m.group('units'), float(m.group('conversion')), authority) 63 else: 64 return None 94 def name(self): 95 "Returns the projection name." 96 self._cache_osr() 97 return self._srs.name 98 99 @property 100 def spheroid(self): 101 "Returns the spheroid for this spatial reference." 102 self._cache_osr() 103 return self._srs['spheroid'] 104 105 @property 106 def datum(self): 107 "Returns the datum for this spatial reference." 108 self._cache_osr() 109 return self._srs['datum'] 110 111 @property 112 def projected(self): 113 "Is this Spatial Reference projected?" 114 self._cache_osr() 115 return self._srs.projected 116 117 @property 118 def local(self): 119 "Is this Spatial Reference local?" 120 self._cache_osr() 121 return self._srs.local 122 123 @property 124 def geographic(self): 125 "Is this Spatial Reference geographic?" 126 self._cache_osr() 127 return self._srs.geographic 128 129 @property 130 def linear_name(self): 131 "Returns the linear units name." 132 self._cache_osr() 133 return self._srs.linear_name 134 135 @property 136 def linear_units(self): 137 "Returns the linear units." 138 self._cache_osr() 139 return self._srs.linear_units 140 141 @property 142 def angular_units(self): 143 "Returns the angular units." 144 self._cache_osr() 145 return self._srs.angular_units 146 147 @property 148 def angular_name(self): 149 "Returns the name of the angular units." 150 self._cache_osr() 151 return self._srs.angular_name 65 152 66 153 def __str__(self): 67 return "%d - %s " % (self.srid, self.auth_name) 154 "Returns the string representation. If GDAL is installed, it will be 'pretty' OGC WKT." 155 if HAS_OSR: 156 self._cache_osr() 157 if hasattr(self, '_srs'): return str(self._srs) 158 return "%d:%s " % (self.srid, self.auth_name) django/branches/gis/django/contrib/gis/tests/__init__.py
r5008 r5397 1 from unittest import TestSuite, makeSuite, TextTestRunner 2 import test_geos, test_gdal_ds, test_gdal_srs, test_gdal_geom, test_spatialrefsys 3 4 def suite(): 5 s = TestSuite() 6 s.addTest(test_geos.suite()) 7 s.addTest(test_gdal_ds.suite()) 8 s.addTest(test_gdal_srs.suite()) 9 s.addTest(test_gdal_geom.suite()) 10 s.addTest(test_spatialrefsys.suite()) 11 return s 12 13 def run(verbosity=2): 14 TextTestRunner(verbosity=verbosity).run(suite())
