[1.2.X] Fixed #15378 -- Now properly handle OGR layers that have features with invalid geometries. Thanks, kunitoki for bug report and initial patch.
Backport of r15813 from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@15814 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
e8dcaa4bba
commit
ddb1cee0e5
|
@ -43,6 +43,9 @@ class ICity1(CityBase):
|
||||||
class ICity2(ICity1):
|
class ICity2(ICity1):
|
||||||
dt_time = models.DateTimeField(auto_now=True)
|
dt_time = models.DateTimeField(auto_now=True)
|
||||||
|
|
||||||
|
class Invalid(models.Model):
|
||||||
|
point = models.PointField()
|
||||||
|
|
||||||
# Mapping dictionaries for the models above.
|
# Mapping dictionaries for the models above.
|
||||||
co_mapping = {'name' : 'Name',
|
co_mapping = {'name' : 'Name',
|
||||||
'state' : {'name' : 'State'}, # ForeignKey's use another mapping dictionary for the _related_ Model (State in this case).
|
'state' : {'name' : 'State'}, # ForeignKey's use another mapping dictionary for the _related_ Model (State in this case).
|
||||||
|
|
|
@ -4,16 +4,19 @@ from decimal import Decimal
|
||||||
|
|
||||||
from django.utils.copycompat import copy
|
from django.utils.copycompat import copy
|
||||||
|
|
||||||
from django.contrib.gis.gdal import DataSource
|
from django.contrib.gis.gdal import DataSource, OGRException
|
||||||
from django.contrib.gis.tests.utils import mysql
|
from django.contrib.gis.tests.utils import mysql
|
||||||
from django.contrib.gis.utils.layermapping import LayerMapping, LayerMapError, InvalidDecimal, MissingForeignKey
|
from django.contrib.gis.utils.layermapping import LayerMapping, LayerMapError, InvalidDecimal, MissingForeignKey
|
||||||
|
|
||||||
from models import City, County, CountyFeat, Interstate, ICity1, ICity2, State, city_mapping, co_mapping, cofeat_mapping, inter_mapping
|
from models import \
|
||||||
|
City, County, CountyFeat, Interstate, ICity1, ICity2, Invalid, State, \
|
||||||
|
city_mapping, co_mapping, cofeat_mapping, inter_mapping
|
||||||
|
|
||||||
shp_path = os.path.realpath(os.path.join(os.path.dirname(__file__), os.pardir, 'data'))
|
shp_path = os.path.realpath(os.path.join(os.path.dirname(__file__), os.pardir, 'data'))
|
||||||
city_shp = os.path.join(shp_path, 'cities', 'cities.shp')
|
city_shp = os.path.join(shp_path, 'cities', 'cities.shp')
|
||||||
co_shp = os.path.join(shp_path, 'counties', 'counties.shp')
|
co_shp = os.path.join(shp_path, 'counties', 'counties.shp')
|
||||||
inter_shp = os.path.join(shp_path, 'interstates', 'interstates.shp')
|
inter_shp = os.path.join(shp_path, 'interstates', 'interstates.shp')
|
||||||
|
invalid_shp = os.path.join(shp_path, 'invalid', 'emptypoints.shp')
|
||||||
|
|
||||||
# Dictionaries to hold what's expected in the county shapefile.
|
# Dictionaries to hold what's expected in the county shapefile.
|
||||||
NAMES = ['Bexar', 'Galveston', 'Harris', 'Honolulu', 'Pueblo']
|
NAMES = ['Bexar', 'Galveston', 'Harris', 'Honolulu', 'Pueblo']
|
||||||
|
@ -266,3 +269,10 @@ class LayerMapTest(unittest.TestCase):
|
||||||
self.assertEqual(6, ICity1.objects.count())
|
self.assertEqual(6, ICity1.objects.count())
|
||||||
self.assertEqual(3, ICity2.objects.count())
|
self.assertEqual(3, ICity2.objects.count())
|
||||||
|
|
||||||
|
def test07_invalid_layer(self):
|
||||||
|
"Tests LayerMapping on invalid geometries. See #15378."
|
||||||
|
invalid_mapping = {'point': 'POINT'}
|
||||||
|
lm = LayerMapping(Invalid, invalid_shp, invalid_mapping,
|
||||||
|
source_srs=4326)
|
||||||
|
lm.save(silent=True)
|
||||||
|
|
||||||
|
|
|
@ -292,7 +292,10 @@ class LayerMapping(object):
|
||||||
|
|
||||||
if isinstance(model_field, GeometryField):
|
if isinstance(model_field, GeometryField):
|
||||||
# Verify OGR geometry.
|
# Verify OGR geometry.
|
||||||
|
try:
|
||||||
val = self.verify_geom(feat.geom, model_field)
|
val = self.verify_geom(feat.geom, model_field)
|
||||||
|
except OGRException:
|
||||||
|
raise LayerMapError('Could not retrieve geometry from feature.')
|
||||||
elif isinstance(model_field, models.base.ModelBase):
|
elif isinstance(model_field, models.base.ModelBase):
|
||||||
# The related _model_, not a field was passed in -- indicating
|
# The related _model_, not a field was passed in -- indicating
|
||||||
# another mapping for the related Model.
|
# another mapping for the related Model.
|
||||||
|
|
Loading…
Reference in New Issue