2011-09-17 21:54:52 +02:00
|
|
|
import os
|
2014-08-21 18:47:57 +02:00
|
|
|
import re
|
2017-01-07 12:11:46 +01:00
|
|
|
from io import StringIO
|
2011-10-17 20:45:22 +02:00
|
|
|
|
2017-05-03 03:27:11 +02:00
|
|
|
from django.contrib.gis.gdal import GDAL_VERSION, Driver, GDALException
|
|
|
|
from django.contrib.gis.utils.ogrinspect import ogrinspect
|
2013-09-12 10:30:45 +02:00
|
|
|
from django.core.management import call_command
|
2014-09-17 23:58:20 +02:00
|
|
|
from django.db import connection, connections
|
2018-11-27 16:22:13 +01:00
|
|
|
from django.test import SimpleTestCase, TestCase, skipUnlessDBFeature
|
2015-05-05 20:11:02 +02:00
|
|
|
from django.test.utils import modify_settings
|
2011-10-17 20:45:22 +02:00
|
|
|
|
2015-02-10 16:07:44 +01:00
|
|
|
from ..test_data import TEST_DATA
|
2019-03-03 19:33:48 +01:00
|
|
|
from .models import AllOGRFields
|
2011-09-17 21:54:52 +02:00
|
|
|
|
2013-05-11 05:08:45 +02:00
|
|
|
|
2013-09-12 10:30:45 +02:00
|
|
|
class InspectDbTests(TestCase):
|
|
|
|
def test_geom_columns(self):
|
|
|
|
"""
|
|
|
|
Test the geo-enabled inspectdb command.
|
|
|
|
"""
|
|
|
|
out = StringIO()
|
2014-11-03 22:03:59 +01:00
|
|
|
call_command(
|
|
|
|
'inspectdb',
|
|
|
|
table_name_filter=lambda tn: tn == 'inspectapp_allogrfields',
|
|
|
|
stdout=out
|
|
|
|
)
|
2013-09-12 10:30:45 +02:00
|
|
|
output = out.getvalue()
|
2014-09-17 23:58:20 +02:00
|
|
|
if connection.features.supports_geometry_field_introspection:
|
|
|
|
self.assertIn('geom = models.PolygonField()', output)
|
|
|
|
self.assertIn('point = models.PointField()', output)
|
|
|
|
else:
|
|
|
|
self.assertIn('geom = models.GeometryField(', output)
|
|
|
|
self.assertIn('point = models.GeometryField(', output)
|
2013-09-12 10:30:45 +02:00
|
|
|
|
2014-11-03 22:03:59 +01:00
|
|
|
@skipUnlessDBFeature("supports_3d_storage")
|
|
|
|
def test_3d_columns(self):
|
|
|
|
out = StringIO()
|
|
|
|
call_command(
|
|
|
|
'inspectdb',
|
|
|
|
table_name_filter=lambda tn: tn == 'inspectapp_fields3d',
|
|
|
|
stdout=out
|
|
|
|
)
|
|
|
|
output = out.getvalue()
|
|
|
|
if connection.features.supports_geometry_field_introspection:
|
|
|
|
self.assertIn('point = models.PointField(dim=3)', output)
|
2020-11-09 01:55:50 +01:00
|
|
|
if connection.features.supports_geography:
|
2016-06-18 18:08:53 +02:00
|
|
|
self.assertIn('pointg = models.PointField(geography=True, dim=3)', output)
|
2020-11-09 01:55:50 +01:00
|
|
|
else:
|
|
|
|
self.assertIn('pointg = models.PointField(dim=3)', output)
|
2014-11-03 22:03:59 +01:00
|
|
|
self.assertIn('line = models.LineStringField(dim=3)', output)
|
|
|
|
self.assertIn('poly = models.PolygonField(dim=3)', output)
|
|
|
|
else:
|
|
|
|
self.assertIn('point = models.GeometryField(', output)
|
2016-06-18 18:08:53 +02:00
|
|
|
self.assertIn('pointg = models.GeometryField(', output)
|
2014-11-03 22:03:59 +01:00
|
|
|
self.assertIn('line = models.GeometryField(', output)
|
|
|
|
self.assertIn('poly = models.GeometryField(', output)
|
|
|
|
|
2013-09-12 10:30:45 +02:00
|
|
|
|
2015-05-05 20:11:02 +02:00
|
|
|
@modify_settings(
|
|
|
|
INSTALLED_APPS={'append': 'django.contrib.gis'},
|
|
|
|
)
|
2018-11-27 16:22:13 +01:00
|
|
|
class OGRInspectTest(SimpleTestCase):
|
2018-06-02 04:31:46 +02:00
|
|
|
expected_srid = 'srid=-1' if GDAL_VERSION < (2, 2) else ''
|
2013-02-01 09:34:39 +01:00
|
|
|
maxDiff = 1024
|
|
|
|
|
2011-09-17 21:54:52 +02:00
|
|
|
def test_poly(self):
|
|
|
|
shp_file = os.path.join(TEST_DATA, 'test_poly', 'test_poly.shp')
|
|
|
|
model_def = ogrinspect(shp_file, 'MyModel')
|
|
|
|
|
|
|
|
expected = [
|
|
|
|
'# This is an auto-generated Django model module created by ogrinspect.',
|
|
|
|
'from django.contrib.gis.db import models',
|
|
|
|
'',
|
2017-09-19 11:41:40 +02:00
|
|
|
'',
|
2011-09-17 21:54:52 +02:00
|
|
|
'class MyModel(models.Model):',
|
|
|
|
' float = models.FloatField()',
|
2019-02-08 01:10:34 +01:00
|
|
|
' int = models.BigIntegerField()',
|
2011-09-17 21:54:52 +02:00
|
|
|
' str = models.CharField(max_length=80)',
|
2018-06-02 04:31:46 +02:00
|
|
|
' geom = models.PolygonField(%s)' % self.expected_srid,
|
2011-09-17 21:54:52 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
self.assertEqual(model_def, '\n'.join(expected))
|
|
|
|
|
2015-05-05 20:11:02 +02:00
|
|
|
def test_poly_multi(self):
|
|
|
|
shp_file = os.path.join(TEST_DATA, 'test_poly', 'test_poly.shp')
|
|
|
|
model_def = ogrinspect(shp_file, 'MyModel', multi_geom=True)
|
2018-06-02 04:31:46 +02:00
|
|
|
self.assertIn('geom = models.MultiPolygonField(%s)' % self.expected_srid, model_def)
|
2015-05-06 11:57:43 +02:00
|
|
|
# Same test with a 25D-type geometry field
|
|
|
|
shp_file = os.path.join(TEST_DATA, 'gas_lines', 'gas_leitung.shp')
|
|
|
|
model_def = ogrinspect(shp_file, 'MyModel', multi_geom=True)
|
2018-06-12 19:34:58 +02:00
|
|
|
srid = '-1' if GDAL_VERSION < (2, 3) else '31253'
|
|
|
|
self.assertIn('geom = models.MultiLineStringField(srid=%s)' % srid, model_def)
|
2015-05-05 20:11:02 +02:00
|
|
|
|
2011-09-17 21:54:52 +02:00
|
|
|
def test_date_field(self):
|
|
|
|
shp_file = os.path.join(TEST_DATA, 'cities', 'cities.shp')
|
|
|
|
model_def = ogrinspect(shp_file, 'City')
|
|
|
|
|
|
|
|
expected = [
|
|
|
|
'# This is an auto-generated Django model module created by ogrinspect.',
|
|
|
|
'from django.contrib.gis.db import models',
|
|
|
|
'',
|
2017-09-19 11:41:40 +02:00
|
|
|
'',
|
2011-09-17 21:54:52 +02:00
|
|
|
'class City(models.Model):',
|
|
|
|
' name = models.CharField(max_length=80)',
|
2019-02-08 01:10:34 +01:00
|
|
|
' population = models.BigIntegerField()',
|
2011-09-17 21:54:52 +02:00
|
|
|
' density = models.FloatField()',
|
|
|
|
' created = models.DateField()',
|
2018-06-02 04:31:46 +02:00
|
|
|
' geom = models.PointField(%s)' % self.expected_srid,
|
2011-09-17 21:54:52 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
self.assertEqual(model_def, '\n'.join(expected))
|
|
|
|
|
|
|
|
def test_time_field(self):
|
|
|
|
# Getting the database identifier used by OGR, if None returned
|
|
|
|
# GDAL does not have the support compiled in.
|
|
|
|
ogr_db = get_ogr_db_string()
|
|
|
|
if not ogr_db:
|
2014-08-21 18:47:57 +02:00
|
|
|
self.skipTest("Unable to setup an OGR connection to your database")
|
2011-09-17 21:54:52 +02:00
|
|
|
|
2014-08-21 18:47:57 +02:00
|
|
|
try:
|
|
|
|
# Writing shapefiles via GDAL currently does not support writing OGRTime
|
|
|
|
# fields, so we need to actually use a database
|
|
|
|
model_def = ogrinspect(ogr_db, 'Measurement',
|
|
|
|
layer_key=AllOGRFields._meta.db_table,
|
|
|
|
decimal=['f_decimal'])
|
2014-12-17 22:00:05 +01:00
|
|
|
except GDALException:
|
2014-08-21 18:47:57 +02:00
|
|
|
self.skipTest("Unable to setup an OGR connection to your database")
|
2011-09-17 21:54:52 +02:00
|
|
|
|
2013-02-02 14:00:38 +01:00
|
|
|
self.assertTrue(model_def.startswith(
|
|
|
|
'# This is an auto-generated Django model module created by ogrinspect.\n'
|
|
|
|
'from django.contrib.gis.db import models\n'
|
|
|
|
'\n'
|
2017-09-19 11:41:40 +02:00
|
|
|
'\n'
|
2013-02-02 14:00:38 +01:00
|
|
|
'class Measurement(models.Model):\n'
|
|
|
|
))
|
|
|
|
|
|
|
|
# The ordering of model fields might vary depending on several factors (version of GDAL, etc.)
|
2018-06-02 04:54:26 +02:00
|
|
|
if connection.vendor == 'sqlite':
|
|
|
|
# SpatiaLite introspection is somewhat lacking (#29461).
|
|
|
|
self.assertIn(' f_decimal = models.CharField(max_length=0)', model_def)
|
|
|
|
else:
|
|
|
|
self.assertIn(' f_decimal = models.DecimalField(max_digits=0, decimal_places=0)', model_def)
|
2013-02-02 14:00:38 +01:00
|
|
|
self.assertIn(' f_int = models.IntegerField()', model_def)
|
2020-11-14 15:08:30 +01:00
|
|
|
if not connection.ops.mariadb:
|
2017-11-18 14:22:15 +01:00
|
|
|
# Probably a bug between GDAL and MariaDB on time fields.
|
|
|
|
self.assertIn(' f_datetime = models.DateTimeField()', model_def)
|
|
|
|
self.assertIn(' f_time = models.TimeField()', model_def)
|
2018-06-02 04:54:26 +02:00
|
|
|
if connection.vendor == 'sqlite':
|
|
|
|
self.assertIn(' f_float = models.CharField(max_length=0)', model_def)
|
|
|
|
else:
|
|
|
|
self.assertIn(' f_float = models.FloatField()', model_def)
|
|
|
|
max_length = 0 if connection.vendor == 'sqlite' else 10
|
|
|
|
self.assertIn(' f_char = models.CharField(max_length=%s)' % max_length, model_def)
|
2013-02-02 14:00:38 +01:00
|
|
|
self.assertIn(' f_date = models.DateField()', model_def)
|
|
|
|
|
2015-08-10 14:03:08 +02:00
|
|
|
# Some backends may have srid=-1
|
|
|
|
self.assertIsNotNone(re.search(r' geom = models.PolygonField\(([^\)])*\)', model_def))
|
2011-09-17 21:54:52 +02:00
|
|
|
|
2013-12-31 14:36:45 +01:00
|
|
|
def test_management_command(self):
|
|
|
|
shp_file = os.path.join(TEST_DATA, 'cities', 'cities.shp')
|
|
|
|
out = StringIO()
|
|
|
|
call_command('ogrinspect', shp_file, 'City', stdout=out)
|
|
|
|
output = out.getvalue()
|
|
|
|
self.assertIn('class City(models.Model):', output)
|
|
|
|
|
2017-09-19 11:41:40 +02:00
|
|
|
def test_mapping_option(self):
|
|
|
|
expected = (
|
2018-06-02 04:31:46 +02:00
|
|
|
" geom = models.PointField(%s)\n"
|
2017-09-19 11:41:40 +02:00
|
|
|
"\n"
|
|
|
|
"\n"
|
|
|
|
"# Auto-generated `LayerMapping` dictionary for City model\n"
|
|
|
|
"city_mapping = {\n"
|
|
|
|
" 'name': 'Name',\n"
|
|
|
|
" 'population': 'Population',\n"
|
|
|
|
" 'density': 'Density',\n"
|
|
|
|
" 'created': 'Created',\n"
|
|
|
|
" 'geom': 'POINT',\n"
|
2018-06-02 04:31:46 +02:00
|
|
|
"}\n" % self.expected_srid)
|
2017-09-19 11:41:40 +02:00
|
|
|
shp_file = os.path.join(TEST_DATA, 'cities', 'cities.shp')
|
|
|
|
out = StringIO()
|
|
|
|
call_command('ogrinspect', shp_file, '--mapping', 'City', stdout=out)
|
|
|
|
self.assertIn(expected, out.getvalue())
|
|
|
|
|
2011-09-17 21:54:52 +02:00
|
|
|
|
|
|
|
def get_ogr_db_string():
|
2013-04-15 20:27:58 +02:00
|
|
|
"""
|
|
|
|
Construct the DB string that GDAL will use to inspect the database.
|
|
|
|
GDAL will create its own connection to the database, so we re-use the
|
|
|
|
connection settings from the Django test.
|
|
|
|
"""
|
2011-09-17 21:54:52 +02:00
|
|
|
db = connections.databases['default']
|
|
|
|
|
|
|
|
# Map from the django backend into the OGR driver name and database identifier
|
2018-09-26 08:48:47 +02:00
|
|
|
# https://www.gdal.org/ogr/ogr_formats.html
|
2011-09-17 21:54:52 +02:00
|
|
|
#
|
2013-04-15 20:27:58 +02:00
|
|
|
# TODO: Support Oracle (OCI).
|
2011-09-17 21:54:52 +02:00
|
|
|
drivers = {
|
2013-04-15 20:27:58 +02:00
|
|
|
'django.contrib.gis.db.backends.postgis': ('PostgreSQL', "PG:dbname='%(db_name)s'", ' '),
|
|
|
|
'django.contrib.gis.db.backends.mysql': ('MySQL', 'MYSQL:"%(db_name)s"', ','),
|
|
|
|
'django.contrib.gis.db.backends.spatialite': ('SQLite', '%(db_name)s', '')
|
2011-09-17 21:54:52 +02:00
|
|
|
}
|
|
|
|
|
2014-09-17 23:34:28 +02:00
|
|
|
db_engine = db['ENGINE']
|
|
|
|
if db_engine not in drivers:
|
|
|
|
return None
|
|
|
|
|
|
|
|
drv_name, db_str, param_sep = drivers[db_engine]
|
2011-09-17 21:54:52 +02:00
|
|
|
|
|
|
|
# Ensure that GDAL library has driver support for the database.
|
|
|
|
try:
|
|
|
|
Driver(drv_name)
|
2015-11-15 19:52:47 +01:00
|
|
|
except GDALException:
|
2011-09-17 21:54:52 +02:00
|
|
|
return None
|
|
|
|
|
2016-08-09 18:46:14 +02:00
|
|
|
# SQLite/SpatiaLite in-memory databases
|
2013-04-15 20:27:58 +02:00
|
|
|
if db['NAME'] == ":memory:":
|
|
|
|
return None
|
|
|
|
|
2011-09-17 21:54:52 +02:00
|
|
|
# Build the params of the OGR database connection string
|
2013-04-15 20:27:58 +02:00
|
|
|
params = [db_str % {'db_name': db['NAME']}]
|
2013-10-22 12:21:07 +02:00
|
|
|
|
2011-09-17 21:54:52 +02:00
|
|
|
def add(key, template):
|
|
|
|
value = db.get(key, None)
|
|
|
|
# Don't add the parameter if it is not in django's settings
|
|
|
|
if value:
|
|
|
|
params.append(template % value)
|
|
|
|
add('HOST', "host='%s'")
|
|
|
|
add('PORT', "port='%s'")
|
|
|
|
add('USER', "user='%s'")
|
|
|
|
add('PASSWORD', "password='%s'")
|
|
|
|
|
2013-04-15 20:27:58 +02:00
|
|
|
return param_sep.join(params)
|