2011-03-30 16:56:53 +00:00
|
|
|
import os
|
2017-06-02 18:47:17 +00:00
|
|
|
import re
|
2019-02-01 00:50:16 +00:00
|
|
|
from datetime import datetime
|
2020-11-27 17:58:28 +00:00
|
|
|
from pathlib import Path
|
2008-08-05 18:13:06 +00:00
|
|
|
|
2017-05-03 01:27:11 +00:00
|
|
|
from django.contrib.gis.gdal import (
|
2019-02-08 00:10:34 +00:00
|
|
|
DataSource, Envelope, GDALException, OGRGeometry,
|
2017-05-03 01:27:11 +00:00
|
|
|
)
|
2019-02-01 00:50:16 +00:00
|
|
|
from django.contrib.gis.gdal.field import (
|
|
|
|
OFTDateTime, OFTInteger, OFTReal, OFTString,
|
|
|
|
)
|
2017-09-02 07:38:17 +00:00
|
|
|
from django.test import SimpleTestCase
|
2015-02-10 15:07:44 +00:00
|
|
|
|
|
|
|
from ..test_data import TEST_DATA, TestDS, get_ds_file
|
2013-05-11 03:08:45 +00:00
|
|
|
|
2017-06-02 18:47:17 +00:00
|
|
|
wgs_84_wkt = (
|
|
|
|
'GEOGCS["GCS_WGS_1984",DATUM["WGS_1984",SPHEROID["WGS_1984",'
|
|
|
|
'6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",'
|
|
|
|
'0.017453292519943295]]'
|
|
|
|
)
|
|
|
|
# Using a regex because of small differences depending on GDAL versions.
|
2018-06-08 18:20:01 +00:00
|
|
|
wgs_84_wkt_regex = r'^GEOGCS\["(GCS_)?WGS[ _](19)?84".*$'
|
2017-06-02 18:47:17 +00:00
|
|
|
|
2019-02-01 00:50:16 +00:00
|
|
|
datetime_format = '%Y-%m-%dT%H:%M:%S'
|
|
|
|
|
2017-05-03 01:27:11 +00:00
|
|
|
# List of acceptable data sources.
|
|
|
|
ds_list = (
|
|
|
|
TestDS(
|
|
|
|
'test_point', nfeat=5, nfld=3, geom='POINT', gtype=1, driver='ESRI Shapefile',
|
|
|
|
fields={'dbl': OFTReal, 'int': OFTInteger, 'str': OFTString},
|
|
|
|
extent=(-1.35011, 0.166623, -0.524093, 0.824508), # Got extent from QGIS
|
2017-06-02 18:47:17 +00:00
|
|
|
srs_wkt=wgs_84_wkt,
|
2017-05-03 01:27:11 +00:00
|
|
|
field_values={
|
|
|
|
'dbl': [float(i) for i in range(1, 6)],
|
|
|
|
'int': list(range(1, 6)),
|
|
|
|
'str': [str(i) for i in range(1, 6)],
|
|
|
|
},
|
|
|
|
fids=range(5)
|
|
|
|
),
|
|
|
|
TestDS(
|
|
|
|
'test_vrt', ext='vrt', nfeat=3, nfld=3, geom='POINT', gtype='Point25D',
|
2019-02-08 00:10:34 +00:00
|
|
|
driver='OGR_VRT',
|
2017-05-03 01:27:11 +00:00
|
|
|
fields={
|
|
|
|
'POINT_X': OFTString,
|
|
|
|
'POINT_Y': OFTString,
|
|
|
|
'NUM': OFTString,
|
|
|
|
}, # VRT uses CSV, which all types are OFTString.
|
|
|
|
extent=(1.0, 2.0, 100.0, 523.5), # Min/Max from CSV
|
|
|
|
field_values={
|
|
|
|
'POINT_X': ['1.0', '5.0', '100.0'],
|
|
|
|
'POINT_Y': ['2.0', '23.0', '523.5'],
|
|
|
|
'NUM': ['5', '17', '23'],
|
|
|
|
},
|
|
|
|
fids=range(1, 4)
|
|
|
|
),
|
|
|
|
TestDS(
|
|
|
|
'test_poly', nfeat=3, nfld=3, geom='POLYGON', gtype=3,
|
|
|
|
driver='ESRI Shapefile',
|
|
|
|
fields={'float': OFTReal, 'int': OFTInteger, 'str': OFTString},
|
|
|
|
extent=(-1.01513, -0.558245, 0.161876, 0.839637), # Got extent from QGIS
|
2017-06-02 18:47:17 +00:00
|
|
|
srs_wkt=wgs_84_wkt,
|
2019-02-01 00:50:16 +00:00
|
|
|
),
|
|
|
|
TestDS(
|
|
|
|
'has_nulls', nfeat=3, nfld=6, geom='POLYGON', gtype=3,
|
|
|
|
driver='GeoJSON', ext='geojson',
|
|
|
|
fields={
|
|
|
|
'uuid': OFTString,
|
|
|
|
'name': OFTString,
|
|
|
|
'num': OFTReal,
|
|
|
|
'integer': OFTInteger,
|
|
|
|
'datetime': OFTDateTime,
|
|
|
|
'boolean': OFTInteger,
|
|
|
|
},
|
|
|
|
extent=(-75.274200, 39.846504, -74.959717, 40.119040), # Got extent from QGIS
|
|
|
|
field_values={
|
|
|
|
'uuid': [
|
|
|
|
'1378c26f-cbe6-44b0-929f-eb330d4991f5',
|
|
|
|
'fa2ba67c-a135-4338-b924-a9622b5d869f',
|
|
|
|
'4494c1f3-55ab-4256-b365-12115cb388d5',
|
|
|
|
],
|
|
|
|
'name': ['Philadelphia', None, 'north'],
|
|
|
|
'num': [1.001, None, 0.0],
|
|
|
|
'integer': [5, None, 8],
|
|
|
|
'boolean': [True, None, False],
|
|
|
|
'datetime': [
|
|
|
|
datetime.strptime('1994-08-14T11:32:14', datetime_format),
|
|
|
|
None,
|
|
|
|
datetime.strptime('2018-11-29T03:02:52', datetime_format),
|
|
|
|
]
|
|
|
|
},
|
|
|
|
fids=range(3),
|
|
|
|
),
|
2017-05-03 01:27:11 +00:00
|
|
|
)
|
2013-05-11 03:08:45 +00:00
|
|
|
|
|
|
|
bad_ds = (TestDS('foo'),)
|
|
|
|
|
|
|
|
|
2017-09-02 07:38:17 +00:00
|
|
|
class DataSourceTest(SimpleTestCase):
|
2008-08-05 18:13:06 +00:00
|
|
|
|
|
|
|
def test01_valid_shp(self):
|
|
|
|
"Testing valid SHP Data Source files."
|
|
|
|
|
|
|
|
for source in ds_list:
|
|
|
|
# Loading up the data source
|
|
|
|
ds = DataSource(source.ds)
|
|
|
|
|
|
|
|
# Making sure the layer count is what's expected (only 1 layer in a SHP file)
|
|
|
|
self.assertEqual(1, len(ds))
|
|
|
|
|
|
|
|
# Making sure GetName works
|
|
|
|
self.assertEqual(source.ds, ds.name)
|
|
|
|
|
|
|
|
# Making sure the driver name matches up
|
|
|
|
self.assertEqual(source.driver, str(ds.driver))
|
|
|
|
|
|
|
|
# Making sure indexing works
|
2017-09-02 07:38:17 +00:00
|
|
|
msg = 'Index out of range when accessing layers in a datasource: %s.'
|
|
|
|
with self.assertRaisesMessage(IndexError, msg % len(ds)):
|
|
|
|
ds.__getitem__(len(ds))
|
2010-11-01 21:56:48 +00:00
|
|
|
|
2017-09-02 07:38:17 +00:00
|
|
|
with self.assertRaisesMessage(IndexError, 'Invalid OGR layer name given: invalid.'):
|
|
|
|
ds.__getitem__('invalid')
|
2017-09-03 22:00:07 +00:00
|
|
|
|
2020-11-27 17:58:28 +00:00
|
|
|
def test_ds_input_pathlib(self):
|
|
|
|
test_shp = Path(get_ds_file('test_point', 'shp'))
|
|
|
|
ds = DataSource(test_shp)
|
|
|
|
self.assertEqual(len(ds), 1)
|
|
|
|
|
2008-08-05 18:13:06 +00:00
|
|
|
def test02_invalid_shp(self):
|
|
|
|
"Testing invalid SHP files for the Data Source."
|
|
|
|
for source in bad_ds:
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(GDALException):
|
|
|
|
DataSource(source.ds)
|
2008-08-05 18:13:06 +00:00
|
|
|
|
|
|
|
def test03a_layers(self):
|
|
|
|
"Testing Data Source Layers."
|
|
|
|
for source in ds_list:
|
|
|
|
ds = DataSource(source.ds)
|
|
|
|
|
|
|
|
# Incrementing through each layer, this tests DataSource.__iter__
|
2010-11-01 21:56:48 +00:00
|
|
|
for layer in ds:
|
2008-08-05 18:13:06 +00:00
|
|
|
# Making sure we get the number of features we expect
|
|
|
|
self.assertEqual(len(layer), source.nfeat)
|
|
|
|
|
|
|
|
# Making sure we get the number of fields we expect
|
|
|
|
self.assertEqual(source.nfld, layer.num_fields)
|
|
|
|
self.assertEqual(source.nfld, len(layer.fields))
|
|
|
|
|
2014-02-15 12:40:41 +00:00
|
|
|
# Testing the layer's extent (an Envelope), and its properties
|
2016-06-23 20:24:18 +00:00
|
|
|
self.assertIsInstance(layer.extent, Envelope)
|
|
|
|
self.assertAlmostEqual(source.extent[0], layer.extent.min_x, 5)
|
|
|
|
self.assertAlmostEqual(source.extent[1], layer.extent.min_y, 5)
|
|
|
|
self.assertAlmostEqual(source.extent[2], layer.extent.max_x, 5)
|
|
|
|
self.assertAlmostEqual(source.extent[3], layer.extent.max_y, 5)
|
2008-08-05 18:13:06 +00:00
|
|
|
|
|
|
|
# Now checking the field names.
|
|
|
|
flds = layer.fields
|
2013-10-17 08:17:41 +00:00
|
|
|
for f in flds:
|
2015-04-27 14:59:16 +00:00
|
|
|
self.assertIn(f, source.fields)
|
2010-11-01 21:56:48 +00:00
|
|
|
|
2008-08-05 18:13:06 +00:00
|
|
|
# Negative FIDs are not allowed.
|
2017-09-02 07:38:17 +00:00
|
|
|
with self.assertRaisesMessage(IndexError, 'Negative indices are not allowed on OGR Layers.'):
|
2016-01-17 11:26:39 +00:00
|
|
|
layer.__getitem__(-1)
|
2017-09-02 07:38:17 +00:00
|
|
|
with self.assertRaisesMessage(IndexError, 'Invalid feature id: 50000.'):
|
2016-01-17 11:26:39 +00:00
|
|
|
layer.__getitem__(50000)
|
2008-08-05 18:13:06 +00:00
|
|
|
|
|
|
|
if hasattr(source, 'field_values'):
|
|
|
|
# Testing `Layer.get_fields` (which uses Layer.__iter__)
|
2017-05-27 23:08:46 +00:00
|
|
|
for fld_name, fld_value in source.field_values.items():
|
|
|
|
self.assertEqual(fld_value, layer.get_fields(fld_name))
|
2008-08-05 18:13:06 +00:00
|
|
|
|
|
|
|
# Testing `Layer.__getitem__`.
|
|
|
|
for i, fid in enumerate(source.fids):
|
|
|
|
feat = layer[fid]
|
|
|
|
self.assertEqual(fid, feat.fid)
|
|
|
|
# Maybe this should be in the test below, but we might as well test
|
|
|
|
# the feature values here while in this loop.
|
2017-05-27 23:08:46 +00:00
|
|
|
for fld_name, fld_value in source.field_values.items():
|
|
|
|
self.assertEqual(fld_value[i], feat.get(fld_name))
|
2010-11-01 21:56:48 +00:00
|
|
|
|
2017-09-02 07:38:17 +00:00
|
|
|
msg = 'Index out of range when accessing field in a feature: %s.'
|
|
|
|
with self.assertRaisesMessage(IndexError, msg % len(feat)):
|
|
|
|
feat.__getitem__(len(feat))
|
|
|
|
|
|
|
|
with self.assertRaisesMessage(IndexError, 'Invalid OFT field name given: invalid.'):
|
|
|
|
feat.__getitem__('invalid')
|
|
|
|
|
2008-08-05 18:13:06 +00:00
|
|
|
def test03b_layer_slice(self):
|
|
|
|
"Test indexing and slicing on Layers."
|
|
|
|
# Using the first data-source because the same slice
|
|
|
|
# can be used for both the layer and the control values.
|
|
|
|
source = ds_list[0]
|
|
|
|
ds = DataSource(source.ds)
|
|
|
|
|
|
|
|
sl = slice(1, 3)
|
|
|
|
feats = ds[0][sl]
|
|
|
|
|
|
|
|
for fld_name in ds[0].fields:
|
|
|
|
test_vals = [feat.get(fld_name) for feat in feats]
|
|
|
|
control_vals = source.field_values[fld_name][sl]
|
|
|
|
self.assertEqual(control_vals, test_vals)
|
|
|
|
|
2008-10-26 22:24:21 +00:00
|
|
|
def test03c_layer_references(self):
|
2012-10-07 14:20:29 +00:00
|
|
|
"""
|
2012-10-08 00:28:19 +00:00
|
|
|
Ensure OGR objects keep references to the objects they belong to.
|
2012-10-07 14:20:29 +00:00
|
|
|
"""
|
2008-10-26 22:24:21 +00:00
|
|
|
source = ds_list[0]
|
|
|
|
|
|
|
|
# See ticket #9448.
|
|
|
|
def get_layer():
|
|
|
|
# This DataSource object is not accessible outside this
|
|
|
|
# scope. However, a reference should still be kept alive
|
|
|
|
# on the `Layer` returned.
|
|
|
|
ds = DataSource(source.ds)
|
|
|
|
return ds[0]
|
|
|
|
|
|
|
|
# Making sure we can call OGR routines on the Layer returned.
|
|
|
|
lyr = get_layer()
|
|
|
|
self.assertEqual(source.nfeat, len(lyr))
|
2010-11-01 21:56:48 +00:00
|
|
|
self.assertEqual(source.gtype, lyr.geom_type.num)
|
2008-10-26 22:24:21 +00:00
|
|
|
|
2012-10-07 14:20:29 +00:00
|
|
|
# Same issue for Feature/Field objects, see #18640
|
|
|
|
self.assertEqual(str(lyr[0]['str']), "1")
|
|
|
|
|
2008-08-05 18:13:06 +00:00
|
|
|
def test04_features(self):
|
|
|
|
"Testing Data Source Features."
|
|
|
|
for source in ds_list:
|
|
|
|
ds = DataSource(source.ds)
|
|
|
|
|
|
|
|
# Incrementing through each layer
|
|
|
|
for layer in ds:
|
|
|
|
# Incrementing through each feature in the layer
|
|
|
|
for feat in layer:
|
|
|
|
# Making sure the number of fields, and the geometry type
|
|
|
|
# are what's expected.
|
|
|
|
self.assertEqual(source.nfld, len(list(feat)))
|
|
|
|
self.assertEqual(source.gtype, feat.geom_type)
|
|
|
|
|
|
|
|
# Making sure the fields match to an appropriate OFT type.
|
|
|
|
for k, v in source.fields.items():
|
|
|
|
# Making sure we get the proper OGR Field instance, using
|
|
|
|
# a string value index for the feature.
|
2015-04-27 09:51:46 +00:00
|
|
|
self.assertIsInstance(feat[k], v)
|
2017-08-14 09:32:37 +00:00
|
|
|
self.assertIsInstance(feat.fields[0], str)
|
2008-08-05 18:13:06 +00:00
|
|
|
|
|
|
|
# Testing Feature.__iter__
|
2012-10-06 20:56:47 +00:00
|
|
|
for fld in feat:
|
2017-05-27 23:08:46 +00:00
|
|
|
self.assertIn(fld.name, source.fields)
|
2010-11-01 21:56:48 +00:00
|
|
|
|
2008-08-05 18:13:06 +00:00
|
|
|
def test05_geometries(self):
|
|
|
|
"Testing Geometries from Data Source Features."
|
|
|
|
for source in ds_list:
|
|
|
|
ds = DataSource(source.ds)
|
|
|
|
|
|
|
|
# Incrementing through each layer and feature.
|
|
|
|
for layer in ds:
|
|
|
|
for feat in layer:
|
|
|
|
g = feat.geom
|
|
|
|
|
|
|
|
# Making sure we get the right Geometry name & type
|
|
|
|
self.assertEqual(source.geom, g.geom_name)
|
|
|
|
self.assertEqual(source.gtype, g.geom_type)
|
|
|
|
|
|
|
|
# Making sure the SpatialReference is as expected.
|
|
|
|
if hasattr(source, 'srs_wkt'):
|
2017-06-02 18:47:17 +00:00
|
|
|
self.assertIsNotNone(re.match(wgs_84_wkt_regex, g.srs.wkt))
|
2008-08-05 18:13:06 +00:00
|
|
|
|
2009-11-10 01:54:35 +00:00
|
|
|
def test06_spatial_filter(self):
|
|
|
|
"Testing the Layer.spatial_filter property."
|
|
|
|
ds = DataSource(get_ds_file('cities', 'shp'))
|
|
|
|
lyr = ds[0]
|
|
|
|
|
|
|
|
# When not set, it should be None.
|
2015-04-27 14:59:16 +00:00
|
|
|
self.assertIsNone(lyr.spatial_filter)
|
2009-11-10 01:54:35 +00:00
|
|
|
|
|
|
|
# Must be set a/an OGRGeometry or 4-tuple.
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
lyr._set_spatial_filter('foo')
|
2009-11-10 01:54:35 +00:00
|
|
|
|
|
|
|
# Setting the spatial filter with a tuple/list with the extent of
|
|
|
|
# a buffer centering around Pueblo.
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
lyr._set_spatial_filter(list(range(5)))
|
2009-11-10 01:54:35 +00:00
|
|
|
filter_extent = (-105.609252, 37.255001, -103.609252, 39.255001)
|
|
|
|
lyr.spatial_filter = (-105.609252, 37.255001, -103.609252, 39.255001)
|
|
|
|
self.assertEqual(OGRGeometry.from_bbox(filter_extent), lyr.spatial_filter)
|
|
|
|
feats = [feat for feat in lyr]
|
|
|
|
self.assertEqual(1, len(feats))
|
|
|
|
self.assertEqual('Pueblo', feats[0].get('Name'))
|
|
|
|
|
|
|
|
# Setting the spatial filter with an OGRGeometry for buffer centering
|
|
|
|
# around Houston.
|
2014-09-04 12:15:09 +00:00
|
|
|
filter_geom = OGRGeometry(
|
|
|
|
'POLYGON((-96.363151 28.763374,-94.363151 28.763374,'
|
|
|
|
'-94.363151 30.763374,-96.363151 30.763374,-96.363151 28.763374))'
|
|
|
|
)
|
2009-11-10 01:54:35 +00:00
|
|
|
lyr.spatial_filter = filter_geom
|
|
|
|
self.assertEqual(filter_geom, lyr.spatial_filter)
|
|
|
|
feats = [feat for feat in lyr]
|
|
|
|
self.assertEqual(1, len(feats))
|
|
|
|
self.assertEqual('Houston', feats[0].get('Name'))
|
|
|
|
|
|
|
|
# Clearing the spatial filter by setting it to None. Now
|
|
|
|
# should indicate that there are 3 features in the Layer.
|
|
|
|
lyr.spatial_filter = None
|
|
|
|
self.assertEqual(3, len(lyr))
|
2010-11-01 21:56:48 +00:00
|
|
|
|
2011-03-30 16:56:53 +00:00
|
|
|
def test07_integer_overflow(self):
|
|
|
|
"Testing that OFTReal fields, treated as OFTInteger, do not overflow."
|
|
|
|
# Using *.dbf from Census 2010 TIGER Shapefile for Texas,
|
|
|
|
# which has land area ('ALAND10') stored in a Real field
|
|
|
|
# with no precision.
|
|
|
|
ds = DataSource(os.path.join(TEST_DATA, 'texas.dbf'))
|
|
|
|
feat = ds[0][0]
|
|
|
|
# Reference value obtained using `ogrinfo`.
|
|
|
|
self.assertEqual(676586997978, feat.get('ALAND10'))
|