Example #1
0
 def test_nullable_boolean_imported(self):
     """LayerMapping import of GeoJSON with a nullable boolean value."""
     lm = LayerMapping(HasNulls, has_nulls_geojson, has_nulls_mapping)
     lm.save()
     self.assertEqual(HasNulls.objects.filter(boolean=True).count(), 1)
     self.assertEqual(HasNulls.objects.filter(boolean=False).count(), 1)
     self.assertEqual(HasNulls.objects.filter(boolean__isnull=True).count(), 1)
Example #2
0
 def test_nullable_datetime_imported(self):
     """LayerMapping import of GeoJSON with a nullable date/time value."""
     lm = LayerMapping(HasNulls, has_nulls_geojson, has_nulls_mapping)
     lm.save()
     self.assertEqual(HasNulls.objects.filter(datetime__lt=datetime.date(1994, 8, 15)).count(), 1)
     self.assertEqual(HasNulls.objects.filter(datetime='2018-11-29T03:02:52').count(), 1)
     self.assertEqual(HasNulls.objects.filter(datetime__isnull=True).count(), 1)
Example #3
0
 def test_null_number_imported(self):
     """LayerMapping import of GeoJSON with a null numeric value."""
     lm = LayerMapping(HasNulls, has_nulls_geojson, has_nulls_mapping)
     lm.save()
     self.assertEqual(HasNulls.objects.count(), 3)
     self.assertEqual(HasNulls.objects.filter(num=0).count(), 1)
     self.assertEqual(HasNulls.objects.filter(num__isnull=True).count(), 1)
Example #4
0
 def test_encoded_name(self):
     """ Test a layer containing utf-8-encoded name """
     city_shp = os.path.join(shp_path, 'ch-city', 'ch-city.shp')
     lm = LayerMapping(City, city_shp, city_mapping)
     lm.save(silent=True, strict=True)
     self.assertEqual(City.objects.count(), 1)
     self.assertEqual(City.objects.all()[0].name, "Zürich")
Example #5
0
    def handle(self, *args, **options):
        for num, abbrev in models.state_nums.items():
            print "Importing %s" % abbrev.upper()

            su_path = os.path.abspath(os.path.join(
                    os.path.dirname(models.__file__),
                    'data/su%s_d11.shp' % num))

            try:
                mapping = LayerMapping(models.District, su_path,
                                       models.upper_district_mapping,
                                       source_srs=4296,
                                       transform=True)
            except:
                print "Downloading su%s_d11_shp.zip" % num
                data = urllib2.urlopen("http://www.census.gov/geo/cob/bdy/su/su06shp/su%s_d11_shp.zip" % num).read()
                data = StringIO(data)
                zip = zipfile.ZipFile(data)
                zip.extractall(os.path.abspath(os.path.join(
                            os.path.dirname(models.__file__), 'data/')))
                mapping = LayerMapping(models.District, su_path,
                                       models.upper_district_mapping,
                                       source_srs=4296,
                                       transform=True)

            mapping.save(strict=True)

            if abbrev.lower() == 'dc' or abbrev.lower() == 'ne':
                continue
Example #6
0
 def test_textfield(self):
     "Tests that String content fits also in a TextField"
     mapping = copy(city_mapping)
     mapping['name_txt'] = 'Name'
     lm = LayerMapping(City, city_shp, mapping)
     lm.save(silent=True, strict=True)
     self.assertEqual(City.objects.count(), 3)
     self.assertEqual(City.objects.get(name='Houston').name_txt, "Houston")
Example #7
0
 def test_null_string_imported(self):
     "Test LayerMapping import of GeoJSON with a null string value."
     lm = LayerMapping(HasNulls, has_nulls_geojson, has_nulls_mapping)
     lm.save()
     self.assertEqual(HasNulls.objects.filter(name='None').count(), 0)
     num_empty = 1 if connection.features.interprets_empty_strings_as_nulls else 0
     self.assertEqual(HasNulls.objects.filter(name='').count(), num_empty)
     self.assertEqual(HasNulls.objects.filter(name__isnull=True).count(), 1)
Example #8
0
 def test_textfield(self):
     "Tests that String content fits also in a TextField"
     mapping = copy(city_mapping)
     mapping["name_txt"] = "Name"
     lm = LayerMapping(City, city_shp, mapping)
     lm.save(silent=True, strict=True)
     self.assertEqual(City.objects.count(), 3)
     self.assertEqual(City.objects.all().order_by("name_txt")[0].name_txt, "Houston")
Example #9
0
def run(verbose=True):
    lm = LayerMapping(
        Neighborhood,
        world_shp,
        neighborhood_mapping,
        transform=False
    )

    lm.save(strict=True, verbose=verbose)
Example #10
0
 def handle_noargs(self, **options):        
     mapping = {
     'id' : 'DISTRICT',
     'area' : 'MULTIPOLYGON',
     }
     # this won't work if the app is renamed other than reps. Isn't there a better way?
     shp = os.path.abspath(os.path.join('reps', 'data', '2004legfinal_shp.shp'))
     lm = LayerMapping(District, shp, mapping, source_srs='NAD83')
     lm.save(strict=True, verbose=True)
Example #11
0
 def test_null_geom_with_unique(self):
     """LayerMapping may be created with a unique and a null geometry."""
     State.objects.bulk_create([State(name='Colorado'), State(name='Hawaii'), State(name='Texas')])
     hw = State.objects.get(name='Hawaii')
     hu = County.objects.create(name='Honolulu', state=hw, mpoly=None)
     lm = LayerMapping(County, co_shp, co_mapping, transform=False, unique='name')
     lm.save(silent=True, strict=True)
     hu.refresh_from_db()
     self.assertIsNotNone(hu.mpoly)
     self.assertEqual(hu.mpoly.ogr.num_coords, 449)
Example #12
0
def run(verbose=True) :
    shp = os.path.abspath(os.path.join(os.path.dirname(__file__), 'database/data/OregonRecClipRegion.shp'))
    lm = LayerMapping(
        ClipRegion, 
        shp, 
        mapping,
        transform=False, 
        encoding='iso-8859-1'
    )
    lm.save(strict=True, verbose=verbose)
Example #13
0
 def load(self):
     if Council.objects.count() > 0:
         print "Councils are already loaded!"
         return
     
     councils_mapping = {
         'name' : 'NAME',
         'poly': 'POLYGON'
     }
     lm = LayerMapping(Council, config.boundaries_file, 
                   councils_mapping, transform=False, encoding='iso-8859-1')
     lm.save(silent=False)
Example #14
0
 def load(self):
     if Neighbourhood.objects.count() > 0:
         print "Neighbourhoods are already loaded!"
         return
     
     neighbourhood_mapping = {
         'name' : 'MSOA04NM',
         'poly' : 'POLYGON'
     }
     
     lm = LayerMapping(Neighbourhood, config.neighbourhoods, 
                   neighbourhood_mapping, transform=False, encoding='iso-8859-1')
     lm.save(silent=False)
Example #15
0
def load_from_madagascar_ocha_wgs84_shp_file(root_directory,verbose=True):
    for root, dirs, files in os.walk(root_directory):
        files = glob.glob(root + '/*.shp')
        for file in files:
            try:
                lm = LayerMapping(LocationLevel, file, madagascar_wgs84_mapping, transform=True, encoding='iso-8859-1')
                lm.save(strict=True,verbose=verbose)
            except LayerMapError:
                continue
    location_level = LocationLevel.objects.all()
    for location in location_level:
        location.name_0 = "Madagascar"
        location.save()
Example #16
0
 def test_null_number_imported_not_allowed(self):
     """
     LayerMapping import of GeoJSON with nulls to fields that don't permit
     them.
     """
     lm = LayerMapping(DoesNotAllowNulls, has_nulls_geojson, has_nulls_mapping)
     lm.save(silent=True)
     # When a model fails to save due to IntegrityError (null in non-null
     # column), subsequent saves fail with "An error occurred in the current
     # transaction. You can't execute queries until the end of the 'atomic'
     # block." On Oracle and MySQL, the one object that did load appears in
     # this count. On other databases, no records appear.
     self.assertLessEqual(DoesNotAllowNulls.objects.count(), 1)
Example #17
0
 def load(self):
     if Postcode.objects.count() > 0:
         print "Postcodes are already loaded!"
         return
     
     postcode_mapping = {
         'name' : 'POSTCODE',
         'poly' : 'POLYGON',
     }
     
     for s in config.postcodes:
         lm = LayerMapping(Postcode,
                           config.postcodeDir+s, postcode_mapping, transform=False, 
                           encoding='iso-8859-1')
         lm.save(silent=False)
Example #18
0
 def handle_noargs(self, **options):
     mappings = {
         "tl_2009_04_zcta5.shp": {"name": "ZCTA5CE", "area": "MULTIPOLYGON", "type": "Zip Code"},
         "tl_2009_04_cbsa.shp": {"name": "NAME", "area": "MULTIPOLYGON", "type": "MSA"},
         "tl_2009_04_county.shp": {"name": "NAME", "area": "MULTIPOLYGON", "type": "County"},
         "tl_2009_04_cousub.shp": {"name": "NAME", "area": "MULTIPOLYGON", "type": "County Subdivision"},
         "tl_2009_04_elsd.shp": {"name": "NAME", "area": "MULTIPOLYGON", "type": "Elementary School District"},
         "tl_2009_04_place.shp": {"name": "NAME", "area": "MULTIPOLYGON", "type": "Place"},
         "tl_2009_04_scsd.shp": {"name": "NAME", "area": "MULTIPOLYGON", "type": "Secondary School District"},
         "tl_2009_04_unsd.shp": {"name": "NAME", "area": "MULTIPOLYGON", "type": "Unified School District"},
     }
     for filename, mapping in mappings.iteritems():
         ty = mapping.pop("type")
         # this won't work if the app is renamed other than reps. Isn't there a better way?
         shp = os.path.abspath(os.path.join("reps", "data", "places", filename))
         lm = LayerMapping(Place, shp, mapping, source_srs="NAD83")
         lm.save(strict=True, verbose=True)
         places = Place.objects.filter(feature_type__isnull=True).update(feature_type=ty)
Example #19
0
def load_shape(request):
	from django.contrib.gis.utils.layermapping import LayerMapping
	from django.contrib.gis.gdal import DataSource
	from opengis.models import ThailandProvince

	ds = DataSource('/Users/apple/Projects/OpenGIS/OpenGIS/opengis_platform/files/shape/thailand_province/changwat_region_Project.shp')

	mapping = {
		'geocode' : 'CODE',
	    'name_th' : 'TNAME',
	    'name' : 'ENAME',
	    'region' : 'POLYGON',
	}

	lm = LayerMapping(ThailandProvince, ds, mapping, encoding='tis-620')
	lm.save(verbose=True)
	
	return HttpResponse('')
Example #20
0
    def _load_provinces(self, verbose=True):

        # Notify
        print 'Loading provinces...'

        mapping = {
            'name': 'NAAM_PROV',
            'geometry': 'POLYGON'
        }

        # Empty existing table
        Province.objects.all().delete()

        # Load shapefile into database
        lm = LayerMapping(Province, self._provinces_shp, mapping, transform=False)  # No transformation, leave in WGS84
        lm.save(verbose=verbose)

        # Calculate job duration
        self._calculate_duration()
Example #21
0
    def _load_municipalities(self, verbose=True):

        # Notify
        print 'Loading municipalities...'

        mapping = {
            'name': 'GM_NAAM',
            'geometry': 'POLYGON'
        }

        # Empty existing municipalities table
        Municipality.objects.all().delete()

        # Load shapefile into database, no transformation, leave in WGS84
        lm = LayerMapping(Municipality, self._municipality_shp, mapping, transform=False)
        lm.save(verbose=verbose)

        # Calculate job duration
        self._calculate_duration()
Example #22
0
    def test_layermap_strict(self):
        "Testing the `strict` keyword, and import of a LineString shapefile."
        # When the `strict` keyword is set an error encountered will force
        # the importation to stop.
        with self.assertRaises(InvalidDecimal):
            lm = LayerMapping(Interstate, inter_shp, inter_mapping)
            lm.save(silent=True, strict=True)
        Interstate.objects.all().delete()

        # This LayerMapping should work b/c `strict` is not set.
        lm = LayerMapping(Interstate, inter_shp, inter_mapping)
        lm.save(silent=True)

        # Two interstate should have imported correctly.
        self.assertEqual(2, Interstate.objects.count())

        # Verifying the values in the layer w/the model.
        ds = DataSource(inter_shp)

        # Only the first two features of this shapefile are valid.
        valid_feats = ds[0][:2]
        for feat in valid_feats:
            istate = Interstate.objects.get(name=feat['Name'].value)

            if feat.fid == 0:
                self.assertEqual(Decimal(str(feat['Length'])), istate.length)
            elif feat.fid == 1:
                # Everything but the first two decimal digits were truncated,
                # because the Interstate model's `length` field has decimal_places=2.
                self.assertAlmostEqual(feat.get('Length'), float(istate.length), 2)

            for p1, p2 in zip(feat.geom, istate.path):
                self.assertAlmostEqual(p1[0], p2[0], 6)
                self.assertAlmostEqual(p1[1], p2[1], 6)
Example #23
0
    def test_test_fid_range_step(self):
        "Tests the `fid_range` keyword and the `step` keyword of .save()."
        # Function for clearing out all the counties before testing.
        def clear_counties():
            County.objects.all().delete()

        State.objects.bulk_create([
            State(name='Colorado'), State(name='Hawaii'), State(name='Texas')
        ])

        # Initializing the LayerMapping object to use in these tests.
        lm = LayerMapping(County, co_shp, co_mapping, transform=False, unique='name')

        # Bad feature id ranges should raise a type error.
        bad_ranges = (5.0, 'foo', co_shp)
        for bad in bad_ranges:
            self.assertRaises(TypeError, lm.save, fid_range=bad)

        # Step keyword should not be allowed w/`fid_range`.
        fr = (3, 5)  # layer[3:5]
        self.assertRaises(LayerMapError, lm.save, fid_range=fr, step=10)
        lm.save(fid_range=fr)

        # Features IDs 3 & 4 are for Galveston County, Texas -- only
        # one model is returned because the `unique` keyword was set.
        qs = County.objects.all()
        self.assertEqual(1, qs.count())
        self.assertEqual('Galveston', qs[0].name)

        # Features IDs 5 and beyond for Honolulu County, Hawaii, and
        # FID 0 is for Pueblo County, Colorado.
        clear_counties()
        lm.save(fid_range=slice(5, None), silent=True, strict=True)  # layer[5:]
        lm.save(fid_range=slice(None, 1), silent=True, strict=True)  # layer[:1]

        # Only Pueblo & Honolulu counties should be present because of
        # the `unique` keyword.  Have to set `order_by` on this QuerySet
        # or else MySQL will return a different ordering than the other dbs.
        qs = County.objects.order_by('name')
        self.assertEqual(2, qs.count())
        hi, co = tuple(qs)
        hi_idx, co_idx = tuple(map(NAMES.index, ('Honolulu', 'Pueblo')))
        self.assertEqual('Pueblo', co.name)
        self.assertEqual(NUMS[co_idx], len(co.mpoly))
        self.assertEqual('Honolulu', hi.name)
        self.assertEqual(NUMS[hi_idx], len(hi.mpoly))

        # Testing the `step` keyword -- should get the same counties
        # regardless of we use a step that divides equally, that is odd,
        # or that is larger than the dataset.
        for st in (4, 7, 1000):
            clear_counties()
            lm.save(step=st, strict=True)
            self.county_helper(county_feat=False)
Example #24
0
    def test_simple_layermap(self):
        "Test LayerMapping import of a simple point shapefile."
        # Setting up for the LayerMapping.
        lm = LayerMapping(City, city_shp, city_mapping)
        lm.save()

        # There should be three cities in the shape file.
        self.assertEqual(3, City.objects.count())

        # Opening up the shapefile, and verifying the values in each
        # of the features made it to the model.
        ds = DataSource(city_shp)
        layer = ds[0]
        for feat in layer:
            city = City.objects.get(name=feat['Name'].value)
            self.assertEqual(feat['Population'].value, city.population)
            self.assertEqual(Decimal(str(feat['Density'])), city.density)
            self.assertEqual(feat['Created'].value, city.dt)

            # Comparing the geometries.
            pnt1, pnt2 = feat.geom, city.point
            self.assertAlmostEqual(pnt1.x, pnt2.x, 5)
            self.assertAlmostEqual(pnt1.y, pnt2.y, 5)
Example #25
0
    def test_simple_layermap(self):
        "Test LayerMapping import of a simple point shapefile."
        # Setting up for the LayerMapping.
        lm = LayerMapping(City, city_shp, city_mapping)
        lm.save()

        # There should be three cities in the shape file.
        self.assertEqual(3, City.objects.count())

        # Opening up the shapefile, and verifying the values in each
        # of the features made it to the model.
        ds = DataSource(city_shp)
        layer = ds[0]
        for feat in layer:
            city = City.objects.get(name=feat['Name'].value)
            self.assertEqual(feat['Population'].value, city.population)
            self.assertEqual(Decimal(str(feat['Density'])), city.density)
            self.assertEqual(feat['Created'].value, city.dt)

            # Comparing the geometries.
            pnt1, pnt2 = feat.geom, city.point
            self.assertAlmostEqual(pnt1.x, pnt2.x, 5)
            self.assertAlmostEqual(pnt1.y, pnt2.y, 5)
Example #26
0
    def test_init(self):
        "Testing LayerMapping initialization."

        # Model field that does not exist.
        bad1 = copy(city_mapping)
        bad1['foobar'] = 'FooField'

        # Shapefile field that does not exist.
        bad2 = copy(city_mapping)
        bad2['name'] = 'Nombre'

        # Nonexistent geographic field type.
        bad3 = copy(city_mapping)
        bad3['point'] = 'CURVE'

        # Incrementing through the bad mapping dictionaries and
        # ensuring that a LayerMapError is raised.
        for bad_map in (bad1, bad2, bad3):
            with self.assertRaises(LayerMapError):
                LayerMapping(City, city_shp, bad_map)

        # A LookupError should be thrown for bogus encodings.
        with self.assertRaises(LookupError):
            LayerMapping(City, city_shp, city_mapping, encoding='foobar')
Example #27
0
    def test_model_inheritance(self):
        "Tests LayerMapping on inherited models.  See #12093."
        icity_mapping = {
            "name": "Name",
            "population": "Population",
            "density": "Density",
            "point": "POINT",
            "dt": "Created",
        }
        # Parent model has geometry field.
        lm1 = LayerMapping(ICity1, city_shp, icity_mapping)
        lm1.save()

        # Grandparent has geometry field.
        lm2 = LayerMapping(ICity2, city_shp, icity_mapping)
        lm2.save()

        self.assertEqual(6, ICity1.objects.count())
        self.assertEqual(3, ICity2.objects.count())
Example #28
0
    def test_model_inheritance(self):
        "Tests LayerMapping on inherited models.  See #12093."
        icity_mapping = {'name': 'Name',
                         'population': 'Population',
                         'density': 'Density',
                         'point': 'POINT',
                         'dt': 'Created',
                         }

        # Parent model has geometry field.
        lm1 = LayerMapping(ICity1, city_shp, icity_mapping)
        lm1.save()

        # Grandparent has geometry field.
        lm2 = LayerMapping(ICity2, city_shp, icity_mapping)
        lm2.save()

        self.assertEqual(6, ICity1.objects.count())
        self.assertEqual(3, ICity2.objects.count())
Example #29
0
    def test03_layermap_strict(self):
        "Testing the `strict` keyword, and import of a LineString shapefile."
        # When the `strict` keyword is set an error encountered will force
        # the importation to stop.
        try:
            lm = LayerMapping(Interstate, inter_shp, inter_mapping)
            lm.save(silent=True, strict=True)
        except InvalidDecimal:
            # No transactions for geoms on MySQL; delete added features.
            if mysql: Interstate.objects.all().delete()
        else:
            self.fail(
                'Should have failed on strict import with invalid decimal values.'
            )

        # This LayerMapping should work b/c `strict` is not set.
        lm = LayerMapping(Interstate, inter_shp, inter_mapping)
        lm.save(silent=True)

        # Two interstate should have imported correctly.
        self.assertEqual(2, Interstate.objects.count())

        # Verifying the values in the layer w/the model.
        ds = DataSource(inter_shp)

        # Only the first two features of this shapefile are valid.
        valid_feats = ds[0][:2]
        for feat in valid_feats:
            istate = Interstate.objects.get(name=feat['Name'].value)

            if feat.fid == 0:
                self.assertEqual(Decimal(str(feat['Length'])), istate.length)
            elif feat.fid == 1:
                # Everything but the first two decimal digits were truncated,
                # because the Interstate model's `length` field has decimal_places=2.
                self.assertAlmostEqual(feat.get('Length'),
                                       float(istate.length), 2)

            for p1, p2 in zip(feat.geom, istate.path):
                self.assertAlmostEqual(p1[0], p2[0], 6)
                self.assertAlmostEqual(p1[1], p2[1], 6)
Example #30
0
    def test_model_inheritance(self):
        "Tests LayerMapping on inherited models.  See #12093."
        icity_mapping = {
            "name": "Name",
            "population": "Population",
            "density": "Density",
            "point": "POINT",
            "dt": "Created",
        }

        # Parent model has geometry field.
        lm1 = LayerMapping(ICity1, city_shp, icity_mapping)
        lm1.save()

        # Grandparent has geometry field.
        lm2 = LayerMapping(ICity2, city_shp, icity_mapping)
        lm2.save()

        self.assertEqual(6, ICity1.objects.count())
        self.assertEqual(3, ICity2.objects.count())
Example #31
0
 def test_layermapping_default_db(self):
     lm = LayerMapping(City, city_shp, city_mapping)
     self.assertEqual(lm.using, 'other')
Example #32
0
 def test_charfield_too_short(self):
     mapping = copy(city_mapping)
     mapping['name_short'] = 'Name'
     lm = LayerMapping(City, city_shp, mapping)
     with self.assertRaises(InvalidString):
         lm.save(silent=True, strict=True)
Example #33
0
    def test_test_fid_range_step(self):
        "Tests the `fid_range` keyword and the `step` keyword of .save()."

        # Function for clearing out all the counties before testing.
        def clear_counties():
            County.objects.all().delete()

        State.objects.bulk_create([
            State(name='Colorado'),
            State(name='Hawaii'),
            State(name='Texas')
        ])

        # Initializing the LayerMapping object to use in these tests.
        lm = LayerMapping(County,
                          co_shp,
                          co_mapping,
                          transform=False,
                          unique='name')

        # Bad feature id ranges should raise a type error.
        bad_ranges = (5.0, 'foo', co_shp)
        for bad in bad_ranges:
            with self.assertRaises(TypeError):
                lm.save(fid_range=bad)

        # Step keyword should not be allowed w/`fid_range`.
        fr = (3, 5)  # layer[3:5]
        with self.assertRaises(LayerMapError):
            lm.save(fid_range=fr, step=10)
        lm.save(fid_range=fr)

        # Features IDs 3 & 4 are for Galveston County, Texas -- only
        # one model is returned because the `unique` keyword was set.
        qs = County.objects.all()
        self.assertEqual(1, qs.count())
        self.assertEqual('Galveston', qs[0].name)

        # Features IDs 5 and beyond for Honolulu County, Hawaii, and
        # FID 0 is for Pueblo County, Colorado.
        clear_counties()
        lm.save(fid_range=slice(5, None), silent=True,
                strict=True)  # layer[5:]
        lm.save(fid_range=slice(None, 1), silent=True,
                strict=True)  # layer[:1]

        # Only Pueblo & Honolulu counties should be present because of
        # the `unique` keyword.  Have to set `order_by` on this QuerySet
        # or else MySQL will return a different ordering than the other dbs.
        qs = County.objects.order_by('name')
        self.assertEqual(2, qs.count())
        hi, co = tuple(qs)
        hi_idx, co_idx = tuple(map(NAMES.index, ('Honolulu', 'Pueblo')))
        self.assertEqual('Pueblo', co.name)
        self.assertEqual(NUMS[co_idx], len(co.mpoly))
        self.assertEqual('Honolulu', hi.name)
        self.assertEqual(NUMS[hi_idx], len(hi.mpoly))

        # Testing the `step` keyword -- should get the same counties
        # regardless of we use a step that divides equally, that is odd,
        # or that is larger than the dataset.
        for st in (4, 7, 1000):
            clear_counties()
            lm.save(step=st, strict=True)
            self.county_helper(county_feat=False)
Example #34
0
    def test_layermap_unique_multigeometry_fk(self):
        "Testing the `unique`, and `transform`, geometry collection conversion, and ForeignKey mappings."
        # All the following should work.

        # Telling LayerMapping that we want no transformations performed on the data.
        lm = LayerMapping(County, co_shp, co_mapping, transform=False)

        # Specifying the source spatial reference system via the `source_srs` keyword.
        lm = LayerMapping(County, co_shp, co_mapping, source_srs=4269)
        lm = LayerMapping(County, co_shp, co_mapping, source_srs='NAD83')

        # Unique may take tuple or string parameters.
        for arg in ('name', ('name', 'mpoly')):
            lm = LayerMapping(County,
                              co_shp,
                              co_mapping,
                              transform=False,
                              unique=arg)

        # Now test for failures

        # Testing invalid params for the `unique` keyword.
        for e, arg in ((TypeError, 5.0), (ValueError, 'foobar'),
                       (ValueError, ('name', 'mpolygon'))):
            with self.assertRaises(e):
                LayerMapping(County,
                             co_shp,
                             co_mapping,
                             transform=False,
                             unique=arg)

        # No source reference system defined in the shapefile, should raise an error.
        if connection.features.supports_transform:
            with self.assertRaises(LayerMapError):
                LayerMapping(County, co_shp, co_mapping)

        # Passing in invalid ForeignKey mapping parameters -- must be a dictionary
        # mapping for the model the ForeignKey points to.
        bad_fk_map1 = copy(co_mapping)
        bad_fk_map1['state'] = 'name'
        bad_fk_map2 = copy(co_mapping)
        bad_fk_map2['state'] = {'nombre': 'State'}
        with self.assertRaises(TypeError):
            LayerMapping(County, co_shp, bad_fk_map1, transform=False)
        with self.assertRaises(LayerMapError):
            LayerMapping(County, co_shp, bad_fk_map2, transform=False)

        # There exist no State models for the ForeignKey mapping to work -- should raise
        # a MissingForeignKey exception (this error would be ignored if the `strict`
        # keyword is not set).
        lm = LayerMapping(County,
                          co_shp,
                          co_mapping,
                          transform=False,
                          unique='name')
        with self.assertRaises(MissingForeignKey):
            lm.save(silent=True, strict=True)

        # Now creating the state models so the ForeignKey mapping may work.
        State.objects.bulk_create([
            State(name='Colorado'),
            State(name='Hawaii'),
            State(name='Texas')
        ])

        # If a mapping is specified as a collection, all OGR fields that
        # are not collections will be converted into them.  For example,
        # a Point column would be converted to MultiPoint. Other things being done
        # w/the keyword args:
        #  `transform=False`: Specifies that no transform is to be done; this
        #    has the effect of ignoring the spatial reference check (because the
        #    county shapefile does not have implicit spatial reference info).
        #
        #  `unique='name'`: Creates models on the condition that they have
        #    unique county names; geometries from each feature however will be
        #    appended to the geometry collection of the unique model.  Thus,
        #    all of the various islands in Honolulu county will be in in one
        #    database record with a MULTIPOLYGON type.
        lm = LayerMapping(County,
                          co_shp,
                          co_mapping,
                          transform=False,
                          unique='name')
        lm.save(silent=True, strict=True)

        # A reference that doesn't use the unique keyword; a new database record will
        # created for each polygon.
        lm = LayerMapping(CountyFeat, co_shp, cofeat_mapping, transform=False)
        lm.save(silent=True, strict=True)

        # The county helper is called to ensure integrity of County models.
        self.county_helper()
from django.core.management import setup_environ
from beckett import settings
setup_environ(settings)
import django, glob
from beckett.finance.models import Zip
from django.contrib.gis.gdal.datasource import DataSource
from django.contrib.gis.models import GeometryColumns, SpatialRefSys
from django.contrib.gis.gdal import SpatialReference
from django.contrib.gis.utils.layermapping import LayerMapping

files = glob.glob("*.shp")
print files
for file in files:
    ds = DataSource(file)
    print ds.layer_count
    layer = ds[0]
    print layer.fields
    print layer.extent.wkt
    print layer.geom_type
    #print layer.get_fields('ZCTA')
    srs = SpatialReference('NAD83')
    mapping = {
        'code': 'ZCTA',
        'poly': 'POLYGON',
    }
    try:
        lm = LayerMapping(Zip, ds, mapping, source_srs=srs)
        lm.save(verbose=True)
    except:
        print "dups"
Example #36
0
 def test_data_source_str(self):
     lm = LayerMapping(City, str(city_shp), city_mapping)
     lm.save()
     self.assertEqual(City.objects.count(), 3)
Example #37
0
def run(verbose=True):
    for k, v in class_dict.items():
        lm = LayerMapping(k, v[0], v[1], transform=False, encoding='utf8')

        lm.save(strict=True, verbose=verbose)
Example #38
0
 def test_uuids_imported(self):
     """LayerMapping import of GeoJSON with UUIDs."""
     lm = LayerMapping(HasNulls, has_nulls_geojson, has_nulls_mapping)
     lm.save()
     self.assertEqual(HasNulls.objects.filter(uuid='1378c26f-cbe6-44b0-929f-eb330d4991f5').count(), 1)
Example #39
0
 def test_charfield_too_short(self):
     mapping = copy(city_mapping)
     mapping['name_short'] = 'Name'
     lm = LayerMapping(City, city_shp, mapping)
     with self.assertRaises(InvalidString):
         lm.save(silent=True, strict=True)
Example #40
0
def load_from_gdam_shp_file(file_name, verbose):
    ind_shp = os.path.abspath(os.path.join(os.path.dirname(__file__) + "/../", file_name))
    lm = LayerMapping(LocationLevel, ind_shp, location_level_mapping, transform=False, encoding='iso-8859-1')
    lm.save(strict=True, verbose=verbose)
Example #41
0
    def test_layermap_unique_multigeometry_fk(self):
        "Testing the `unique`, and `transform`, geometry collection conversion, and ForeignKey mappings."
        # All the following should work.
        try:
            # Telling LayerMapping that we want no transformations performed on the data.
            lm = LayerMapping(County, co_shp, co_mapping, transform=False)

            # Specifying the source spatial reference system via the `source_srs` keyword.
            lm = LayerMapping(County, co_shp, co_mapping, source_srs=4269)
            lm = LayerMapping(County, co_shp, co_mapping, source_srs='NAD83')

            # Unique may take tuple or string parameters.
            for arg in ('name', ('name', 'mpoly')):
                lm = LayerMapping(County, co_shp, co_mapping, transform=False, unique=arg)
        except Exception:
            self.fail('No exception should be raised for proper use of keywords.')

        # Testing invalid params for the `unique` keyword.
        for e, arg in ((TypeError, 5.0), (ValueError, 'foobar'), (ValueError, ('name', 'mpolygon'))):
            self.assertRaises(e, LayerMapping, County, co_shp, co_mapping, transform=False, unique=arg)

        # No source reference system defined in the shapefile, should raise an error.
        if connection.features.supports_transform:
            self.assertRaises(LayerMapError, LayerMapping, County, co_shp, co_mapping)

        # Passing in invalid ForeignKey mapping parameters -- must be a dictionary
        # mapping for the model the ForeignKey points to.
        bad_fk_map1 = copy(co_mapping)
        bad_fk_map1['state'] = 'name'
        bad_fk_map2 = copy(co_mapping)
        bad_fk_map2['state'] = {'nombre': 'State'}
        self.assertRaises(TypeError, LayerMapping, County, co_shp, bad_fk_map1, transform=False)
        self.assertRaises(LayerMapError, LayerMapping, County, co_shp, bad_fk_map2, transform=False)

        # There exist no State models for the ForeignKey mapping to work -- should raise
        # a MissingForeignKey exception (this error would be ignored if the `strict`
        # keyword is not set).
        lm = LayerMapping(County, co_shp, co_mapping, transform=False, unique='name')
        self.assertRaises(MissingForeignKey, lm.save, silent=True, strict=True)

        # Now creating the state models so the ForeignKey mapping may work.
        State.objects.bulk_create([
            State(name='Colorado'), State(name='Hawaii'), State(name='Texas')
        ])

        # If a mapping is specified as a collection, all OGR fields that
        # are not collections will be converted into them.  For example,
        # a Point column would be converted to MultiPoint. Other things being done
        # w/the keyword args:
        #  `transform=False`: Specifies that no transform is to be done; this
        #    has the effect of ignoring the spatial reference check (because the
        #    county shapefile does not have implicit spatial reference info).
        #
        #  `unique='name'`: Creates models on the condition that they have
        #    unique county names; geometries from each feature however will be
        #    appended to the geometry collection of the unique model.  Thus,
        #    all of the various islands in Honolulu county will be in in one
        #    database record with a MULTIPOLYGON type.
        lm = LayerMapping(County, co_shp, co_mapping, transform=False, unique='name')
        lm.save(silent=True, strict=True)

        # A reference that doesn't use the unique keyword; a new database record will
        # created for each polygon.
        lm = LayerMapping(CountyFeat, co_shp, cofeat_mapping, transform=False)
        lm.save(silent=True, strict=True)

        # The county helper is called to ensure integrity of County models.
        self.county_helper()
def run(verbose=True):
    WorldBorder.objects.all().delete()
    lm = LayerMapping(WorldBorder, world_shp_simple, world_mapping,
                      transform=False, encoding='iso-8859-1', unique='name')

    lm.save(strict=True, verbose=verbose)
Example #43
0
 def test_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)