Пример #1
0
 def test09_authority(self):
     "Testing the authority name & code routines."
     for s in srlist:
         if hasattr(s, 'auth'):
             srs = SpatialReference(s.wkt)
             for target, tup in s.auth.items():
                 self.assertEqual(tup[0], srs.auth_name(target))
                 self.assertEqual(tup[1], srs.auth_code(target))
Пример #2
0
 def test02_bad_wkt(self):
     "Testing initialization on invalid WKT."
     for bad in bad_srlist:
         try:
             srs = SpatialReference(bad)
             srs.validate()
         except (SRSException, GDALException):
             pass
         else:
             self.fail('Should not have initialized on bad WKT "%s"!')
Пример #3
0
 def test13_attr_value(self):
     "Testing the attr_value() method."
     s1 = SpatialReference('WGS84')
     with self.assertRaises(TypeError):
         s1.__getitem__(0)
     with self.assertRaises(TypeError):
         s1.__getitem__(('GEOGCS', 'foo'))
     self.assertEqual('WGS 84', s1['GEOGCS'])
     self.assertEqual('WGS_1984', s1['DATUM'])
     self.assertEqual('EPSG', s1['AUTHORITY'])
     self.assertEqual(4326, int(s1['AUTHORITY', 1]))
     self.assertIsNone(s1['FOOBAR'])
Пример #4
0
 def test13_attr_value(self):
     "Testing the attr_value() method."
     s1 = SpatialReference("WGS84")
     with self.assertRaises(TypeError):
         s1.__getitem__(0)
     with self.assertRaises(TypeError):
         s1.__getitem__(("GEOGCS", "foo"))
     self.assertEqual("WGS 84", s1["GEOGCS"])
     self.assertEqual("WGS_1984", s1["DATUM"])
     self.assertEqual("EPSG", s1["AUTHORITY"])
     self.assertEqual(4326, int(s1["AUTHORITY", 1]))
     self.assertIsNone(s1["FOOBAR"])
Пример #5
0
    def test_unicode(self):
        wkt = (
            'PROJCS["DHDN / Soldner 39 Langschoß",'
            'GEOGCS["DHDN",DATUM["Deutsches_Hauptdreiecksnetz",'
            'SPHEROID["Bessel 1841",6377397.155,299.1528128,AUTHORITY["EPSG","7004"]],AUTHORITY["EPSG","6314"]],'
            'PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],'
            'UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],'
            'AUTHORITY["EPSG","4314"]],UNIT["metre",1,AUTHORITY["EPSG","9001"]],'
            'PROJECTION["Cassini_Soldner"],PARAMETER["latitude_of_origin",50.66738711],'
            'PARAMETER["central_meridian",6.28935703],PARAMETER["false_easting",0],'
            'PARAMETER["false_northing",0],AUTHORITY["mj10777.de","187939"],AXIS["x",NORTH],AXIS["y",EAST]]'
        )
        srs = SpatialReference(wkt)
        srs_list = [srs, srs.clone()]
        srs.import_wkt(wkt)

        for srs in srs_list:
            self.assertEqual(srs.name, 'DHDN / Soldner 39 Langschoß')
            self.assertEqual(srs.wkt, wkt)
            self.assertIn('Langschoß', srs.pretty_wkt)
            self.assertIn('Langschoß', srs.xml)
Пример #6
0
 def test13_attr_value(self):
     "Testing the attr_value() method."
     s1 = SpatialReference('WGS84')
     with self.assertRaises(TypeError):
         s1.__getitem__(0)
     with self.assertRaises(TypeError):
         s1.__getitem__(('GEOGCS', 'foo'))
     self.assertEqual('WGS 84', s1['GEOGCS'])
     self.assertEqual('WGS_1984', s1['DATUM'])
     self.assertEqual('EPSG', s1['AUTHORITY'])
     self.assertEqual(4326, int(s1['AUTHORITY', 1]))
     self.assertIsNone(s1['FOOBAR'])
Пример #7
0
 def test13_attr_value(self):
     "Testing the attr_value() method."
     s1 = SpatialReference("WGS84")
     with self.assertRaises(TypeError):
         s1.__getitem__(0)
     with self.assertRaises(TypeError):
         s1.__getitem__(("GEOGCS", "foo"))
     self.assertEqual("WGS 84", s1["GEOGCS"])
     self.assertEqual("WGS_1984", s1["DATUM"])
     self.assertEqual("EPSG", s1["AUTHORITY"])
     self.assertEqual(4326, int(s1["AUTHORITY", 1]))
     self.assertIsNone(s1["FOOBAR"])
Пример #8
0
    def load_boundary_set(self, slug, definition, data_sources, options):
        BoundarySet.objects.filter(
            slug=slug).delete()  # also deletes boundaries

        boundary_set = BoundarySet.objects.create(
            slug=slug,
            last_updated=definition['last_updated'],
            name=definition['name'],
            singular=definition['singular'],
            domain=definition['domain'],
            authority=definition['authority'],
            source_url=definition['source_url'],
            licence_url=definition['licence_url'],
            start_date=definition['start_date'],
            end_date=definition['end_date'],
            notes=definition['notes'],
            extra=definition['extra'],
        )

        boundary_set.extent = [None, None, None,
                               None]  # [xmin, ymin, xmax, ymax]

        for data_source in data_sources:
            #log.info(_('Loading %(slug)s from %(source)s') % {'slug': slug, 'source': data_source.name})

            layer = data_source[0]
            layer.source = data_source  # to trace the layer back to its source

            if definition.get('srid'):
                srs = SpatialReference(definition['srid'])
            else:
                srs = layer.srs

            for feature in layer:
                feature = Feature(feature, definition, srs, boundary_set)
                feature.layer = layer  # to trace the feature back to its source

                if feature.is_valid():
                    #log.info(_('%(slug)s...') % {'slug': feature.slug})

                    boundary = self.load_boundary(feature, options['merge'])
                    boundary_set.extend(boundary.extent)

        if None not in boundary_set.extent:  # unless there are no features
            boundary_set.save()
Пример #9
0
def import_shapes(shapefile_path, logger):
    srs = SpatialReference(srs_wkt)
    lm = LayerMapping(Parcel,
                      shapefile_path, {
                          "shape_leng": "SHAPE_Leng",
                          "shape_area": "SHAPE_Area",
                          "map_par_id": "MAP_PAR_ID",
                          "loc_id": "LOC_ID",
                          "poly_type": "POLY_TYPE",
                          "map_no": "MAP_NO",
                          "source": "SOURCE",
                          "plan_id": "PLAN_ID",
                          "last_edit": "LAST_EDIT",
                          "town_id": "TOWN_ID",
                          "shape": "POLYGON"
                      },
                      source_srs=srs)
    lm.save(strict=True)
Пример #10
0
    def parse_layer(self, shapefile, layername, fields_of_interest):
        ds = DataSource(shapefile)
        lyr = ds[0]
        num_feat = lyr.num_feat

        overwrite = True
        if overwrite:
            try:
                old_lyr = Layer.objects.get(name=layername)
                old_lyr.delete()
            except:
                pass
        try:
            the_layer = Layer(name=layername)
            the_layer.save()
        except:
            raise Exception('Unable to create layer named %s' % layername)

        wgs84 = SpatialReference(4326)
        pctdone = 0
        for feat in lyr:
            try:
                the_geometry = feat.geom.transform(wgs84, clone=True)
                the_feature = Feature(layer=the_layer, geom=the_geometry.wkt)
                the_feature.save()
            except:
                raise Exception('Unable to create feature')

            try:
                for fld in lyr.fields:
                    if fld in fields_of_interest or len(
                            fields_of_interest) == 0:
                        the_attribute = Attribute(key=fld,
                                                  value=str(feat[fld]),
                                                  feature=the_feature)
                        the_attribute.save()
            except:
                raise Exception('Unable to create attribute')

            oldpctdone = pctdone
            pctdone = int((feat.fid * 100.) / float(num_feat))
            if pctdone > oldpctdone:
                print "Completed %d percent of %d features" % (pctdone,
                                                               num_feat)
Пример #11
0
    def srs(self):
        """
        Returns a GDAL SpatialReference object, if GDAL is installed.
        """
        if HAS_GDAL:
            if hasattr(self, '_srs'):
                # Returning a clone of the cached SpatialReference object.
                return self._srs.clone()
            else:
                # Attempting to cache a SpatialReference object.

                # Trying to get from WKT first.
                try:
                    self._srs = SpatialReference(self.wkt)
                    return self.srs
                except Exception, msg:
                    pass
                
                raise Exception('Could not get OSR SpatialReference from WKT: %s\nError:\n%s' % (self.wkt, msg))
Пример #12
0
 def get_dump_object(self, obj):
     data = {
         "type": "Feature",
         "properties": self._current,
     }
     if ((self.selected_fields is None or 'pk' in self.selected_fields) and
             'pk' not in data["properties"]):
         data["properties"]["pk"] = obj._meta.pk.value_to_string(obj)
     if self._geometry:
         if self._geometry.srid != self.srid:
             # If needed, transform the geometry in the srid of the global geojson srid
             if self._geometry.srid not in self._cts:
                 srs = SpatialReference(self.srid)
                 self._cts[self._geometry.srid] = CoordTransform(self._geometry.srs, srs)
             self._geometry.transform(self._cts[self._geometry.srid])
         data["geometry"] = eval(self._geometry.geojson)
     else:
         data["geometry"] = None
     return data
Пример #13
0
    def handle(self, *args, **options):
        if 'shape_file' not in options:
            raise CommandError(_('Missing locations shape file path'))
        else:
            try:
                path = os.path.abspath(options['shape_file'])
            except Exception as e:
                raise CommandError(_('Error: %(msg)s' % {"msg": e}))
            else:
                pre_create = Household.objects.count()
                count = 0
                failed = 0
                srs = SpatialReference('+proj=longlat +datum=WGS84 +no_defs')
                ds = DataSource(path)
                layer = ds[0]
                locations = Location.objects.exclude(parent=None)\
                    .order_by('pk')
                code_field = options.get('code')

                for feature in layer:
                    geom = feature.geom.transform(srs, True)
                    point = GEOSGeometry(geom.centroid.wkt, srid=srs.srid)
                    polygon = GEOSGeometry(geom.wkt, srid=srs.srid)
                    try:
                        plocations = locations.filter(geom__covers=point)
                        blocations = locations.filter(geom__covers=polygon)
                        if plocations.count() and blocations.count():
                            location = blocations.first()
                            hh_id = feature.get(code_field)
                            Household.objects.create(hh_id=hh_id,
                                                     geom=MultiPoint(point),
                                                     bgeom=polygon,
                                                     location=location)
                            count += 1
                    except Location.DoesNotExist:
                        failed += 1
                        continue

                self.stdout.write("Updated {} failed {}".format(count, failed))
                created = Household.objects.count()
                self.stdout.write("Created {} households".format(created -
                                                                 pre_create))
Пример #14
0
 def get_dump_object(self, obj):
     data = {
         "type": "Feature",
         "properties": self._current,
     }
     if self._geometry:
         if self._geometry.srid != self.srid:
             # If needed, transform the geometry in the srid of the global geojson srid
             if not HAS_GDAL:
                 raise SerializationError(
                     'Unable to convert geometry to SRID %s when GDAL is not installed.' % self.srid
                 )
             if self._geometry.srid not in self._cts:
                 srs = SpatialReference(self.srid)
                 self._cts[self._geometry.srid] = CoordTransform(self._geometry.srs, srs)
             self._geometry.transform(self._cts[self._geometry.srid])
         data["geometry"] = eval(self._geometry.geojson)
     else:
         data["geometry"] = None
     return data
Пример #15
0
    def setUp(self):
        self.definition = definition = Definition({
            'last_updated':
            date(2000, 1, 1),
            'encoding':
            'utf-8',
            'name':
            'Districts',
            'name_func':
            clean_attr('Name'),
            'id_func':
            attr('ID'),
            'slug_func':
            attr('Code'),
            'is_valid_func':
            lambda f: f.get('ID') == '1',
            'label_point_func':
            lambda f: Point(0, 1),
        })

        self.fields = {
            'Name': 'VALID',
            'ID': '1',
            'Code': '\tFoo—Bar–Baz \r Bzz\n',  # m-dash, n-dash
        }

        self.boundary_set = BoundarySet(
            last_updated=definition['last_updated'],
            name=definition['name'],
            singular=definition['singular'],
        )

        self.feature = Feature(FeatureProxy(self.fields), definition)

        self.other = Feature(
            FeatureProxy({
                'Name': 'INVALID',
                'ID': 100,
                'Code': 3,
            }), definition, SpatialReference(4269), self.boundary_set)
Пример #16
0
    def handle(self, *args, **options):
        if 'shape_file' not in options:
            raise CommandError(_('Missing locations shape file path'))
        else:
            try:
                path = os.path.abspath(options['shape_file'])
            except Exception as e:
                raise CommandError(_('Error: %(msg)s' % {"msg": e}))
            else:
                code = options['code']
                code_is_integer = options['code_is_integer']
                count = 0
                failed = 0
                srs = SpatialReference('+proj=longlat +datum=WGS84 +no_defs')
                ds = DataSource(path)
                layer = ds[0]

                for feature in layer:
                    if feature.get('manual_c_1') != 'Targeted':
                        continue
                    try:
                        location = self._get_location(feature, code,
                                                      code_is_integer)
                    except Location.DoesNotExist:
                        failed += 1
                        continue
                    else:
                        if isinstance(feature.geom, geometries.Polygon):
                            geom = geometries.MultiPolygon('MULTIPOLYGON',
                                                           srs=srs)
                            geom.add(feature.geom.transform(srs, True))
                        else:
                            geom = feature.geom.transform(srs, True)
                        location.geom = geom.wkt
                        location.save()
                        count += 1

                self.stdout.write("Updated {} locations, failed {}".format(
                    count, failed))
Пример #17
0
class GeoSampaMiner(MapMiner):
    mapMinerName = "Geo Sampa"
    mapMinerId = "geosampa"
    """Miner for the Geo Sampa's database"""
    _basecrs = SpatialReference(29183)
    _crs = {"type": "name", "properties": {"name": "EPSG:29183"}}

    #TODO: Refactor this with the decorator @staticmethod
    #TODO: Comment this method (and the whole class)
    def _getBusStops(regions: FeatureCollection) -> [Point]:
        ret = GeoSampa_BusStops.objects.all()[:0]
        for feature in regions['features']:
            #This represents a simple exterior linear ring.  Interior rings are
            #not considered.
            geom = geos.Polygon(
                feature['geometry']['coordinates'][0],
                srid=int(regions['crs']['properties']['name'].split(':')[1]))
            partial = GeoSampa_BusStops.objects.filter(mpoint__within=geom)
            #TODO: Replace union with Collect
            #//https://docs.djangoproject.com/en/2.0/ref/contrib/gis/geoquerysets/#intersects
            ret = ret.union(partial)
        featuresList = []
        for busStop in ret.all():
            GeoSampaMiner._reproject(busStop.mpoint)
            featuresList.append(
                Feature(id=busStop.id,
                        properties={
                            'name': busStop.name,
                            'description': busStop.description,
                            'address': busStop.address
                        },
                        geometry=Point(busStop.mpoint)))
        return FeatureCollection(featuresList, crs=GeoSampaMiner._crs)

    @classmethod
    def _initialize(cls):
        pass

    _availableQueries = {"Bus Stops": _getBusStops}
Пример #18
0
def bbox(lhs, minx, miny, maxx, maxy, crs=None, bboverlaps=True):
    """ Create a bounding box filter for the given spatial attribute.

        :param lhs: the field to compare
        :param minx: the lower x part of the bbox
        :param miny: the lower y part of the bbox
        :param maxx: the upper x part of the bbox
        :param maxy: the upper y part of the bbox
        :param crs: the CRS the bbox is expressed in
        :type lhs: :class:`django.db.models.F`
        :return: a comparison expression object
        :rtype: :class:`django.db.models.Q`
    """
    assert isinstance(lhs, F)
    box = Polygon.from_bbox((minx, miny, maxx, maxy))

    if crs:
        box.srid = SpatialReference(crs).srid
        box.transform(4326)

    if bboverlaps:
        return Q(**{"%s__bboverlaps" % lhs.name: box})
    return Q(**{"%s__intersects" % lhs.name: box})
Пример #19
0
    def create_from_geojson(cls, geojson):
        """Create from GeoJSON (lon/lat coordinates)"""
        try:
            geojson_dict = json.loads(geojson)
        except ValueError as e:
            raise ValidationError(e.message)
        if geojson_dict['type'] != 'FeatureCollection':
            raise ValidationError('GeoJSON must contain a FeatureCollection')

        geoms = []
        web_mercator = SpatialReference(3857)

        def add_polygon(geom_dict):
            try:
                geom = GEOSGeometry(json.dumps(geom_dict), 4326)
            except GEOSException:
                raise ValidationError('GeoJSON is not valid')
            geom.transform(web_mercator)
            geoms.append(geom)

        for feature in geojson_dict['features']:
            geom_dict = feature['geometry']
            if geom_dict['type'] == 'Polygon':
                add_polygon(geom_dict)
            elif geom_dict['type'] == 'MultiPolygon':
                for polygon in geom_dict['coordinates']:
                    add_polygon({'type': 'Polygon', 'coordinates': polygon})
            else:
                raise ValidationError(
                    'GeoJSON features must be Polygons or MultiPolygons')

        bounds = MultiPolygon(geoms)
        if not bounds.valid:
            raise ValidationError('GeoJSON is not valid: %s' %
                                  bounds.valid_reason)

        return InstanceBounds.objects.create(geom=bounds)
Пример #20
0
 def get_dump_object(self, obj):
     data = {
         "type": "Feature",
         "id": obj.pk if self.id_field is None else getattr(obj, self.id_field),
         "properties": self._current,
     }
     if (
         self.selected_fields is None or "pk" in self.selected_fields
     ) and "pk" not in data["properties"]:
         data["properties"]["pk"] = obj._meta.pk.value_to_string(obj)
     if self._geometry:
         if self._geometry.srid != self.srid:
             # If needed, transform the geometry in the srid of the global
             # geojson srid.
             if self._geometry.srid not in self._cts:
                 srs = SpatialReference(self.srid)
                 self._cts[self._geometry.srid] = CoordTransform(
                     self._geometry.srs, srs
                 )
             self._geometry.transform(self._cts[self._geometry.srid])
         data["geometry"] = json.loads(self._geometry.geojson)
     else:
         data["geometry"] = None
     return data
Пример #21
0
    def data(self):
        '''
        Read the output file and provide an iterable result
        '''
        if not hasattr(self, '_data'):
            if self.raster_obj is None:
                self._data = None
                return None
            layer = geom_extent = geom_type = spatial_ref = geom_srid = None
            # If we get here, then we have successfully determined the file type
            # that was provided, using OGR.  ogr_obj contains the OGR DataSource
            # object, and fn contains the name of the file we read to get that.

            # We only support single layer uploads, if there is more than one
            # layer then we will raise an exception
            driver = self.raster_obj.driver.name

            layer = None
            geom_extent = self.raster_obj.extent
            geom_type = 99

            srs = self.raster_obj.srs
            geos_extent = Polygon.from_bbox(self.raster_obj.extent)
            ogr_extent = geos_extent.ogr
            srid = epsg = None
            # User supplied SRID, so we will use that...
            if self._srid:
                srs = None
                geom_srid = self._srid
                epsg = str('EPSG:%s' % (geom_srid,))
                logger.debug('Setting output SRID to %s',
                             epsg)
                try:
                    srs = SpatialReference(epsg)

                    srs.validate()
                    geom_srid = srs.srid
                except Exception, e:
                    logger.debug('Invalid SRS (or none): %s', e,
                                 exc_info=True)
                    srs = None
            # No SRID! Let's try to detect it
            if srs and not geom_srid:
                if not srs.srid:
                    try:
                        srs.identify_epsg()
                        logger.debug('Auto-detect of SRID yielded %s',
                                     geom_srid)
                    except Exception as e:
                        logger.debug(
                            'SRID identification failed: %s', e,
                            exc_info=True)
                else:
                    geom_srid = srs.srid
                epsg = str('EPSG:%s' % (geom_srid,))
                logger.debug('Final SRID of file is %s', geom_srid)

            if srs and not geom_srid:
                '''
                Still no SRID - but we have an srs - so let's try to 
                reproject...
                '''
                try:
                    reprojection = CoordTransform(
                        r.srs, SpatialReference('EPSG:4326'))
                    ogr_extent.transform(reprojection)
                    geos_extent = ogr_extent.geos
                    geom_srid = geos_extent.srid
                except Exception, e:
                    if logger.isEnabledFor(logging.DEBUG):
                        logger.exception('Failed to transform: %s', e)
                    raise FormatException('Unable to determine valid SRID ' +
                                          'for this data')
Пример #22
0
 def wkt(self):
     from django.contrib.gis.gdal import SpatialReference
     return SpatialReference(self.proj4text).wkt
Пример #23
0
def add_srs_entry(srs, auth_name='EPSG', auth_srid=None, ref_sys_name=None,
                  database=None):
    """
    Take a GDAL SpatialReference system and add its information to the
    `spatial_ref_sys` table of the spatial backend. Doing this enables
    database-level spatial transformations for the backend.  Thus, this utility
    is useful for adding spatial reference systems not included by default with
    the backend:

    >>> from django import add_srs_entry
    >>> add_srs_entry(3857)

    Keyword Arguments:
     auth_name:
       This keyword may be customized with the value of the `auth_name` field.
       Defaults to 'EPSG'.

     auth_srid:
       This keyword may be customized with the value of the `auth_srid` field.
       Defaults to the SRID determined by GDAL.

     ref_sys_name:
       For SpatiaLite users only, sets the value of the `ref_sys_name` field.
       Defaults to the name determined by GDAL.

     database:
      The name of the database connection to use; the default is the value
      of `django.db.DEFAULT_DB_ALIAS` (at the time of this writing, its value
      is 'default').
    """
    database = database or DEFAULT_DB_ALIAS
    connection = connections[database]

    if not hasattr(connection.ops, 'spatial_version'):
        raise Exception('The `add_srs_entry` utility only works '
                        'with spatial backends.')
    if not connection.features.supports_add_srs_entry:
        raise Exception('This utility does not support your database backend.')
    SpatialRefSys = connection.ops.spatial_ref_sys()

    # If argument is not a `SpatialReference` instance, use it as parameter
    # to construct a `SpatialReference` instance.
    if not isinstance(srs, SpatialReference):
        srs = SpatialReference(srs)

    if srs.srid is None:
        raise Exception('Spatial reference requires an SRID to be '
                        'compatible with the spatial backend.')

    # Initializing the keyword arguments dictionary for both PostGIS
    # and SpatiaLite.
    kwargs = {'srid': srs.srid,
              'auth_name': auth_name,
              'auth_srid': auth_srid or srs.srid,
              'proj4text': srs.proj4,
              }

    # Backend-specific fields for the SpatialRefSys model.
    srs_field_names = {f.name for f in SpatialRefSys._meta.get_fields()}
    if 'srtext' in srs_field_names:
        kwargs['srtext'] = srs.wkt
    if 'ref_sys_name' in srs_field_names:
        # SpatiaLite specific
        kwargs['ref_sys_name'] = ref_sys_name or srs.name

    # Creating the spatial_ref_sys model.
    try:
        # Try getting via SRID only, because using all kwargs may
        # differ from exact wkt/proj in database.
        SpatialRefSys.objects.using(database).get(srid=srs.srid)
    except SpatialRefSys.DoesNotExist:
        SpatialRefSys.objects.using(database).create(**kwargs)
Пример #24
0
 def test12_coordtransform(self):
     "Testing initialization of a CoordTransform."
     target = SpatialReference('WGS84')
     CoordTransform(SpatialReference(srlist[0].wkt), target)
Пример #25
0
def transform_geometry(geometry, source_wkt):
    """Transform geometry into the database's spatial reference system."""
    source = SpatialReference(source_wkt)
    dest = SpatialReference(DB_SRID)
    transform = CoordTransform(source, dest)
    return geometry.transform(transform, clone=True)
Пример #26
0
 def test01_wkt(self):
     "Testing initialization on valid OGC WKT."
     for s in srlist:
         srs = SpatialReference(s.wkt)
Пример #27
0
def add_srs_entry(srs,
                  auth_name='EPSG',
                  auth_srid=None,
                  ref_sys_name=None,
                  database=None):
    """
    This function takes a GDAL SpatialReference system and adds its information
    to the `spatial_ref_sys` table of the spatial backend.  Doing this enables
    database-level spatial transformations for the backend.  Thus, this utility
    is useful for adding spatial reference systems not included by default with
    the backend -- for example, the so-called "Google Maps Mercator Projection"
    is excluded in PostGIS 1.3 and below, and the following adds it to the
    `spatial_ref_sys` table:

    >>> from django.contrib.gis.utils import add_srs_entry
    >>> add_srs_entry(900913)

    Keyword Arguments:
     auth_name:
       This keyword may be customized with the value of the `auth_name` field.
       Defaults to 'EPSG'.

     auth_srid:
       This keyword may be customized with the value of the `auth_srid` field.
       Defaults to the SRID determined by GDAL.

     ref_sys_name:
       For SpatiaLite users only, sets the value of the `ref_sys_name` field.
       Defaults to the name determined by GDAL.

     database:
      The name of the database connection to use; the default is the value
      of `django.db.DEFAULT_DB_ALIAS` (at the time of this writing, it's value
      is 'default').
    """
    from django.db import connections, DEFAULT_DB_ALIAS
    if not database:
        database = DEFAULT_DB_ALIAS
    connection = connections[database]

    if not hasattr(connection.ops, 'spatial_version'):
        raise Exception('The `add_srs_entry` utility only works '
                        'with spatial backends.')
    if connection.ops.oracle or connection.ops.mysql:
        raise Exception('This utility does not support the '
                        'Oracle or MySQL spatial backends.')
    SpatialRefSys = connection.ops.spatial_ref_sys()

    # If argument is not a `SpatialReference` instance, use it as parameter
    # to construct a `SpatialReference` instance.
    if not isinstance(srs, SpatialReference):
        srs = SpatialReference(srs)

    if srs.srid is None:
        raise Exception('Spatial reference requires an SRID to be '
                        'compatible with the spatial backend.')

    # Initializing the keyword arguments dictionary for both PostGIS
    # and SpatiaLite.
    kwargs = {
        'srid': srs.srid,
        'auth_name': auth_name,
        'auth_srid': auth_srid or srs.srid,
        'proj4text': srs.proj4,
    }

    # Backend-specific fields for the SpatialRefSys model.
    if connection.ops.postgis:
        kwargs['srtext'] = srs.wkt
    if connection.ops.spatialite:
        kwargs['ref_sys_name'] = ref_sys_name or srs.name

    # Creating the spatial_ref_sys model.
    try:
        # Try getting via SRID only, because using all kwargs may
        # differ from exact wkt/proj in database.
        SpatialRefSys.objects.using(database).get(srid=srs.srid)
    except SpatialRefSys.DoesNotExist:
        SpatialRefSys.objects.using(database).create(**kwargs)
Пример #28
0
def read_geometry_archive(archive_id):
    """Read an archive of a single shapefile (and associated) files.

    This will load zipped archives of shape files and create entries
    for a single shapefile (basename of files).

    A single shapefile will consist of a collection of one or many features
    of varying types. We produce a single ``GeometryCollection`` of those
    data. Hence, we associate a single shapefile with a single
    ``GeometryCollection``.

    We may need to do more checks/validation to make sure the user only
    added a single shape file or provide a more explicit upload interface
    where they upload the ``shp``, ``dbf``, etc. files individually and
    we assert that they match.

    """
    archive = GeometryArchive.objects.get(id=archive_id)

    # TODO: add a setting like this:
    workdir = getattr(settings, 'GEODATA_WORKDIR', None)
    with tempfile.TemporaryDirectory(dir=workdir) as tmpdir:
        with archive.file.yield_local_path() as archive_path:
            logger.info(f'The geometry archive: {archive_path}')

            # Unzip the contents to the working dir
            with zipfile.ZipFile(archive_path, 'r') as zip_ref:
                zip_ref.extractall(tmpdir)

        msg = 'There must be one and only one shapefile in the archive. Found ({})'
        shape_files = glob(os.path.join(tmpdir, '*.shp'))
        if len(shape_files) > 1:
            raise ValidationError(msg.format(len(shape_files)))
        elif len(shape_files) == 0:
            shape_files = glob(os.path.join(tmpdir, '**/*.shp'))
            if len(shape_files) != 1:
                raise ValidationError(msg.format(len(shape_files)))
        shape_file = shape_files[0]

        # load each shapefile using fiona
        with fiona.open(shape_file) as shapes:
            geometry_entry, created = get_or_create_no_commit(
                Geometry,
                defaults=dict(name=archive.file.name),
                geometry_archive=archive)

            shapes.meta  # TODO: dump this JSON into the model entry

            crs_wkt = shapes.meta['crs_wkt']
            logger.info(f'Geometry crs_wkt: {crs_wkt}')
            spatial_ref = SpatialReference(crs_wkt)
            logger.info(f'Geometry SRID: {spatial_ref.srid}')

            collection = []
            for item in shapes:
                geom = shape(item['geometry'])  # not optimal?
                # TODO: check this
                collection.append(
                    transform_geometry(
                        GEOSGeometry(memoryview(
                            dumps(geom, srid=spatial_ref.srid)),
                                     srid=spatial_ref.srid),
                        crs_wkt,
                    ))

    geometry_entry.data = GeometryCollection(*collection)
    geometry_entry.footprint = geometry_entry.data.convex_hull
    bounds = geometry_entry.footprint.extent  # (xmin, ymin, xmax, ymax)
    coords = [
        (bounds[0], bounds[3]),  # (xmin, ymax)
        (bounds[2], bounds[3]),  # (xmax, ymax)
        (bounds[2], bounds[1]),  # (xmax, ymin)
        (bounds[0], bounds[1]),  # (xmin, ymin)
        (bounds[0], bounds[3]),  # Close the loop
    ]
    geometry_entry.outline = Polygon(coords)

    geometry_entry.save()

    return True
Пример #29
0
def convert_shapefile(shapefilename, srid=4674):
    """
    shapefilename: considera nomenclatura de shapefile do IBGE para determinar se é UF 
                   ou Municípios.
                   ex. 55UF2500GC_SIR.shp para UF e 55MU2500GC_SIR.shp para Municípios
    srid: 4674 (Projeção SIRGAS 2000)
    """
    # /home/nando/Desktop/IBGE/2010/55MU2500GC_SIR.shp
    ds = DataSource(shapefilename)

    is_uf = shapefilename.upper().find('UF') != -1

    transform_coord = None
    if srid != SRID:
        transform_coord = CoordTransform(SpatialReference(srid), SpatialReference(SRID))

    if is_uf:
        model = UF
    else:
        model = Municipio

    ct = 0
    for f in ds[0]:

        # 3D para 2D se necessário
        if f.geom.coord_dim != 2:
            f.geom.coord_dim = 2

        # converte para MultiPolygon se necessário
        if isinstance(f.geom, Polygon):
            g = OGRGeometry(OGRGeomType('MultiPolygon'))
            g.add(f.geom)
        else:
            g = f.geom

        # transforma coordenadas se necessário
        if transform_coord:
            g.transform(transform_coord)

        # força 2D
        g.coord_dim = 2
        kwargs = {}

        if is_uf:
            kwargs['nome'] = capitalize_name(unicode(f.get(CAMPO_NOME_UF),'latin1'))
            kwargs['geom'] = g.ewkt
            kwargs['id_ibge'] = f.get(CAMPO_GEOCODIGO_UF)
            kwargs['regiao'] = capitalize_name(unicode(f.get(CAMPO_REGIAO_UF), 'latin1'))
            kwargs['uf'] = UF_SIGLAS_DICT.get(kwargs['id_ibge'])
        else:
            kwargs['nome'] = capitalize_name(unicode(f.get(CAMPO_NOME_MU),'latin1'))
            kwargs['geom'] = g.ewkt
            kwargs['id_ibge'] = f.get(CAMPO_GEOCODIGO_MU)
            kwargs['uf'] = UF.objects.get(pk=f.get(CAMPO_GEOCODIGO_MU)[:2])
            kwargs['uf_sigla'] = kwargs['uf'].uf
            kwargs['nome_abreviado'] = slugify(kwargs['nome'])
            # tenta corrigir nomes duplicados, são em torno de 242 nomes repetidos
            # adicionando a sigla do estado no final
            if Municipio.objects.filter(nome_abreviado=kwargs['nome_abreviado']).count() > 0:
                kwargs['nome_abreviado'] = u'%s-%s' % (kwargs['nome_abreviado'], kwargs['uf_sigla'].lower())

        instance = model(**kwargs)
        instance.save()

        ct += 1

    print(ct, (is_uf and "Unidades Federativas criadas" or "Municipios criados"))
Пример #30
0
 def test07_boolean_props(self):
     "Testing the boolean properties."
     for s in srlist:
         srs = SpatialReference(s.wkt)
         self.assertEqual(s.projected, srs.projected)
         self.assertEqual(s.geographic, srs.geographic)
Пример #31
0
 def __init__(self):
     self.srs = SpatialReference(settings.SRID)
     self.fields = list(features[0].keys())
     self.mock_features = features
Пример #32
0
def tile_url(layer, z, x, y, style=None, internal=True):
    """Construct actual tile request to QGIS Server.

    Different than tile_url_format, this method will return url for requesting
    a tile, with all parameters filled out.

    :param layer: Layer to use
    :type layer: Layer

    :param z: TMS coordinate, zoom parameter
    :type z: int, str

    :param x: TMS coordinate, longitude parameter
    :type x: int, str

    :param y: TMS coordinate, latitude parameter
    :type y: int, str

    :param style: Layer style to choose
    :type style: str

    :param internal: Flag to switch between public url and internal url.
        Public url will be served by Django Geonode (proxified).
    :type internal: bool

    :return: Tile url
    :rtype: str
    """
    try:
        qgis_layer = QGISServerLayer.objects.get(layer=layer)
    except QGISServerLayer.DoesNotExist:
        msg = 'No QGIS Server Layer for existing layer {0}'.format(layer.name)
        logger.debug(msg)
        raise

    x = int(x)
    y = int(y)
    z = int(z)

    # Call the WMS
    top, left = num2deg(x, y, z)
    bottom, right = num2deg(x + 1, y + 1, z)

    transform = CoordTransform(SpatialReference(4326), SpatialReference(3857))
    top_left_corner = Point(left, top, srid=4326)
    bottom_right_corner = Point(right, bottom, srid=4326)
    top_left_corner.transform(transform)
    bottom_right_corner.transform(transform)

    bottom = bottom_right_corner.y
    right = bottom_right_corner.x
    top = top_left_corner.y
    left = top_left_corner.x

    bbox = ','.join([str(val) for val in [left, bottom, right, top]])

    if not style:
        style = 'default'

    if style not in [s.name for s in qgis_layer.styles.all()]:
        style = qgis_layer.default_style.name

    query_string = {
        'SERVICE': 'WMS',
        'VERSION': '1.3.0',
        'REQUEST': 'GetMap',
        'BBOX': bbox,
        'CRS': 'EPSG:3857',
        'WIDTH': '256',
        'HEIGHT': '256',
        'MAP': qgis_layer.qgis_project_path,
        'LAYERS': layer.name,
        'STYLE': style,
        'FORMAT': 'image/png',
        'TRANSPARENT': 'true',
        'DPI': '96',
        'MAP_RESOLUTION': '96',
        'FORMAT_OPTIONS': 'dpi:96'
    }
    qgis_server_url = qgis_server_endpoint(internal)
    url = Request('GET', qgis_server_url, params=query_string).prepare().url

    return url
Пример #33
0
 def test03_get_wkt(self):
     "Testing getting the WKT."
     for s in srlist:
         srs = SpatialReference(s.wkt)
         self.assertEqual(s.wkt, srs.wkt)
Пример #34
0
 def wkt(self):
     if hasattr(self, 'srtext'):
         return self.srtext
     from django.contrib.gis.gdal import SpatialReference
     return SpatialReference(self.proj4text).wkt
Пример #35
0
 def test12_coordtransform(self):
     "Testing initialization of a CoordTransform."
     target = SpatialReference('WGS84')
     for s in srlist:
         if s.proj:
             ct = CoordTransform(SpatialReference(s.wkt), target)
Пример #36
0
from muni.models import Run
from muni.models import Path
from muni.models import Point
from muni.models import Stop
from muni.models import Direction
from muni.models import DirectionStop
from muni.models import DirectionPath
from muni.models import Speed
from muni.models import StopId2Tag
import django.contrib.gis
from django.db import connection, transaction
from django.contrib.gis.gdal import SpatialReference, CoordTransform, OGRGeometry

proj4 = '+proj=lcc +lat_1=37.06666666666667 +lat_2=38.43333333333333 +lat_0=36.5 +lon_0=-120.5 +x_0=2000000 +y_0=500000.0000000001 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=us-ft +no_defs'
srid = '100000'
ccsf = SpatialReference(proj4)
wgs84 = SpatialReference('WGS84')
ct = CoordTransform(wgs84, ccsf)

url = "http://webservices.nextbus.com/service/publicXMLFeed?command=routeConfig&a=sf-muni"
print(url)
#r = open('r.config','r')
#routes = r.read()
routes = ''
if (len(routes)):
    dom = minidom.parse('r.config')
else:
    dom = minidom.parse(urlopen(url))


def fixlat(n):