2011-10-17 18:45:22 +00:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2011-09-17 19:54:52 +00:00
|
|
|
import os
|
2011-10-17 18:45:22 +00:00
|
|
|
|
2011-09-17 19:54:52 +00:00
|
|
|
from django.db import connections
|
|
|
|
from django.test import TestCase
|
2013-02-02 13:00:38 +00:00
|
|
|
from django.contrib.gis.gdal import Driver
|
2011-09-17 19:54:52 +00:00
|
|
|
from django.contrib.gis.geometry.test_data import TEST_DATA
|
|
|
|
from django.contrib.gis.utils.ogrinspect import ogrinspect
|
2011-10-17 18:45:22 +00:00
|
|
|
|
|
|
|
from .models import AllOGRFields
|
2011-09-17 19:54:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OGRInspectTest(TestCase):
|
2013-02-01 08:34:39 +00:00
|
|
|
maxDiff = 1024
|
|
|
|
|
2011-09-17 19:54:52 +00: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',
|
|
|
|
'',
|
|
|
|
'class MyModel(models.Model):',
|
|
|
|
' float = models.FloatField()',
|
|
|
|
' int = models.FloatField()',
|
|
|
|
' str = models.CharField(max_length=80)',
|
|
|
|
' geom = models.PolygonField(srid=-1)',
|
|
|
|
' objects = models.GeoManager()',
|
|
|
|
]
|
|
|
|
|
|
|
|
self.assertEqual(model_def, '\n'.join(expected))
|
|
|
|
|
|
|
|
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',
|
|
|
|
'',
|
|
|
|
'class City(models.Model):',
|
|
|
|
' name = models.CharField(max_length=80)',
|
|
|
|
' population = models.FloatField()',
|
|
|
|
' density = models.FloatField()',
|
|
|
|
' created = models.DateField()',
|
|
|
|
' geom = models.PointField(srid=-1)',
|
|
|
|
' objects = models.GeoManager()',
|
|
|
|
]
|
|
|
|
|
|
|
|
self.assertEqual(model_def, '\n'.join(expected))
|
|
|
|
|
|
|
|
def test_time_field(self):
|
|
|
|
# Only possible to test this on PostGIS at the momemnt. MySQL
|
|
|
|
# complains about permissions, and SpatiaLite/Oracle are
|
|
|
|
# insanely difficult to get support compiled in for in GDAL.
|
|
|
|
if not connections['default'].ops.postgis:
|
2013-04-15 17:36:44 +00:00
|
|
|
self.skipTest("This database does not support 'ogrinspect'ion")
|
2011-09-17 19:54:52 +00:00
|
|
|
|
|
|
|
# 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:
|
2013-04-15 17:36:44 +00:00
|
|
|
self.skipTest("Your GDAL installation does not support PostGIS databases")
|
2011-09-17 19:54:52 +00:00
|
|
|
|
2013-04-15 17:36:44 +00:00
|
|
|
# Writing shapefiles via GDAL currently does not support writing OGRTime
|
2011-09-17 19:54:52 +00:00
|
|
|
# fields, so we need to actually use a database
|
|
|
|
model_def = ogrinspect(ogr_db, 'Measurement',
|
|
|
|
layer_key=AllOGRFields._meta.db_table,
|
|
|
|
decimal=['f_decimal'])
|
|
|
|
|
2013-02-02 13:00:38 +00: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'
|
|
|
|
'class Measurement(models.Model):\n'
|
|
|
|
))
|
|
|
|
|
|
|
|
# The ordering of model fields might vary depending on several factors (version of GDAL, etc.)
|
|
|
|
self.assertIn(' f_decimal = models.DecimalField(max_digits=0, decimal_places=0)', model_def)
|
|
|
|
self.assertIn(' f_int = models.IntegerField()', model_def)
|
|
|
|
self.assertIn(' f_datetime = models.DateTimeField()', model_def)
|
|
|
|
self.assertIn(' f_time = models.TimeField()', model_def)
|
|
|
|
self.assertIn(' f_float = models.FloatField()', model_def)
|
|
|
|
self.assertIn(' f_char = models.CharField(max_length=10)', model_def)
|
|
|
|
self.assertIn(' f_date = models.DateField()', model_def)
|
|
|
|
|
|
|
|
self.assertTrue(model_def.endswith(
|
|
|
|
' geom = models.PolygonField()\n'
|
|
|
|
' objects = models.GeoManager()'
|
|
|
|
))
|
2011-09-17 19:54:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_ogr_db_string():
|
2013-04-15 18:27:58 +00: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 19:54:52 +00:00
|
|
|
db = connections.databases['default']
|
|
|
|
|
|
|
|
# Map from the django backend into the OGR driver name and database identifier
|
|
|
|
# http://www.gdal.org/ogr/ogr_formats.html
|
|
|
|
#
|
2013-04-15 18:27:58 +00:00
|
|
|
# TODO: Support Oracle (OCI).
|
2011-09-17 19:54:52 +00:00
|
|
|
drivers = {
|
2013-04-15 18:27:58 +00: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 19:54:52 +00:00
|
|
|
}
|
|
|
|
|
2013-04-15 18:27:58 +00:00
|
|
|
drv_name, db_str, param_sep = drivers[db['ENGINE']]
|
2011-09-17 19:54:52 +00:00
|
|
|
|
|
|
|
# Ensure that GDAL library has driver support for the database.
|
|
|
|
try:
|
|
|
|
Driver(drv_name)
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
2013-04-15 18:27:58 +00:00
|
|
|
# SQLite/Spatialite in-memory databases
|
|
|
|
if db['NAME'] == ":memory:":
|
|
|
|
return None
|
|
|
|
|
2011-09-17 19:54:52 +00:00
|
|
|
# Build the params of the OGR database connection string
|
2013-04-15 18:27:58 +00:00
|
|
|
params = [db_str % {'db_name': db['NAME']}]
|
2011-09-17 19:54:52 +00: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 18:27:58 +00:00
|
|
|
return param_sep.join(params)
|