当前位置: 首页>>代码示例>>Python>>正文


Python gdal.DataSource方法代码示例

本文整理汇总了Python中django.contrib.gis.gdal.DataSource方法的典型用法代码示例。如果您正苦于以下问题:Python gdal.DataSource方法的具体用法?Python gdal.DataSource怎么用?Python gdal.DataSource使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在django.contrib.gis.gdal的用法示例。


在下文中一共展示了gdal.DataSource方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_simple_layermap

# 需要导入模块: from django.contrib.gis import gdal [as 别名]
# 或者: from django.contrib.gis.gdal import DataSource [as 别名]
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) 
开发者ID:nesdis,项目名称:djongo,代码行数:25,代码来源:tests.py

示例2: shape_to_csv

# 需要导入模块: from django.contrib.gis import gdal [as 别名]
# 或者: from django.contrib.gis.gdal import DataSource [as 别名]
def shape_to_csv(self, shp_path):
        csv_records = []
        ds = DataSource(shp_path)
        layer = ds[0]
        field_names = layer.fields
        for feat in layer:
            csv_record = dict((f, feat.get(f)) for f in field_names)
            csv_record["geom"] = feat.geom.wkt
            csv_records.append(csv_record)
        return csv_records 
开发者ID:archesproject,项目名称:arches,代码行数:12,代码来源:importer.py

示例3: mapping

# 需要导入模块: from django.contrib.gis import gdal [as 别名]
# 或者: from django.contrib.gis.gdal import DataSource [as 别名]
def mapping(data_source, geom_name='geom', layer_key=0, multi_geom=False):
    """
    Given a DataSource, generates a dictionary that may be used
    for invoking the LayerMapping utility.

    Keyword Arguments:
     `geom_name` => The name of the geometry field to use for the model.

     `layer_key` => The key for specifying which layer in the DataSource to use;
       defaults to 0 (the first layer).  May be an integer index or a string
       identifier for the layer.

     `multi_geom` => Boolean (default: False) - specify as multigeometry.
    """
    if isinstance(data_source, six.string_types):
        # Instantiating the DataSource from the string.
        data_source = DataSource(data_source)
    elif isinstance(data_source, DataSource):
        pass
    else:
        raise TypeError('Data source parameter must be a string or a DataSource object.')

    # Creating the dictionary.
    _mapping = {}

    # Generating the field name for each field in the layer.
    for field in data_source[layer_key].fields:
        mfield = field.lower()
        if mfield[-1:] == '_':
            mfield += 'field'
        _mapping[mfield] = field
    gtype = data_source[layer_key].geom_type
    if multi_geom and gtype.num in (1, 2, 3):
        prefix = 'MULTI'
    else:
        prefix = ''
    _mapping[geom_name] = prefix + str(gtype).upper()
    return _mapping 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:40,代码来源:ogrinspect.py

示例4: handle

# 需要导入模块: from django.contrib.gis import gdal [as 别名]
# 或者: from django.contrib.gis.gdal import DataSource [as 别名]
def handle(self, *args, **options):
        data_source, model_name = options.pop('data_source'), options.pop('model_name')
        if not gdal.HAS_GDAL:
            raise CommandError('GDAL is required to inspect geospatial data sources.')

        # Getting the OGR DataSource from the string parameter.
        try:
            ds = gdal.DataSource(data_source)
        except gdal.GDALException as msg:
            raise CommandError(msg)

        # Returning the output of ogrinspect with the given arguments
        # and options.
        from django.contrib.gis.utils.ogrinspect import _ogrinspect, mapping
        # Filter options to params accepted by `_ogrinspect`
        ogr_options = {k: v for k, v in options.items()
                       if k in inspect.getargspec(_ogrinspect).args and v is not None}
        output = [s for s in _ogrinspect(ds, model_name, **ogr_options)]

        if options['mapping']:
            # Constructing the keyword arguments for `mapping`, and
            # calling it on the data source.
            kwargs = {'geom_name': options['geom_name'],
                      'layer_key': options['layer_key'],
                      'multi_geom': options['multi_geom'],
                      }
            mapping_dict = mapping(ds, **kwargs)
            # This extra legwork is so that the dictionary definition comes
            # out in the same order as the fields in the model definition.
            rev_mapping = {v: k for k, v in mapping_dict.items()}
            output.extend(['', '# Auto-generated `LayerMapping` dictionary for %s model' % model_name,
                           '%s_mapping = {' % model_name.lower()])
            output.extend("    '%s' : '%s'," % (
                rev_mapping[ogr_fld], ogr_fld) for ogr_fld in ds[options['layer_key']].fields
            )
            output.extend(["    '%s' : '%s'," % (options['geom_name'], mapping_dict[options['geom_name']]), '}'])
        return '\n'.join(output) + '\n' 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:39,代码来源:ogrinspect.py

示例5: mapping

# 需要导入模块: from django.contrib.gis import gdal [as 别名]
# 或者: from django.contrib.gis.gdal import DataSource [as 别名]
def mapping(data_source, geom_name='geom', layer_key=0, multi_geom=False):
    """
    Given a DataSource, generate a dictionary that may be used
    for invoking the LayerMapping utility.

    Keyword Arguments:
     `geom_name` => The name of the geometry field to use for the model.

     `layer_key` => The key for specifying which layer in the DataSource to use;
       defaults to 0 (the first layer).  May be an integer index or a string
       identifier for the layer.

     `multi_geom` => Boolean (default: False) - specify as multigeometry.
    """
    if isinstance(data_source, str):
        # Instantiating the DataSource from the string.
        data_source = DataSource(data_source)
    elif isinstance(data_source, DataSource):
        pass
    else:
        raise TypeError('Data source parameter must be a string or a DataSource object.')

    # Creating the dictionary.
    _mapping = {}

    # Generating the field name for each field in the layer.
    for field in data_source[layer_key].fields:
        mfield = field.lower()
        if mfield[-1:] == '_':
            mfield += 'field'
        _mapping[mfield] = field
    gtype = data_source[layer_key].geom_type
    if multi_geom:
        gtype.to_multi()
    _mapping[geom_name] = str(gtype).upper()
    return _mapping 
开发者ID:reBiocoder,项目名称:bioforum,代码行数:38,代码来源:ogrinspect.py

示例6: ogrinfo

# 需要导入模块: from django.contrib.gis import gdal [as 别名]
# 或者: from django.contrib.gis.gdal import DataSource [as 别名]
def ogrinfo(data_source, num_features=10):
    """
    Walk the available layers in the supplied `data_source`, displaying
    the fields for the first `num_features` features.
    """

    # Checking the parameters.
    if isinstance(data_source, str):
        data_source = DataSource(data_source)
    elif isinstance(data_source, DataSource):
        pass
    else:
        raise Exception('Data source parameter must be a string or a DataSource object.')

    for i, layer in enumerate(data_source):
        print("data source : %s" % data_source.name)
        print("==== layer %s" % i)
        print("  shape type: %s" % GEO_CLASSES[layer.geom_type.num].__name__)
        print("  # features: %s" % len(layer))
        print("         srs: %s" % layer.srs)
        extent_tup = layer.extent.tuple
        print("      extent: %s - %s" % (extent_tup[0:2], extent_tup[2:4]))
        print("Displaying the first %s features ====" % num_features)

        width = max(*map(len, layer.fields))
        fmt = " %%%ss: %%s" % width
        for j, feature in enumerate(layer[:num_features]):
            print("=== Feature %s" % j)
            for fld_name in layer.fields:
                type_name = feature[fld_name].type_name
                output = fmt % (fld_name, type_name)
                val = feature.get(fld_name)
                if val:
                    if isinstance(val, str):
                        val_fmt = ' ("%s")'
                    else:
                        val_fmt = ' (%s)'
                    output += val_fmt % val
                else:
                    output += ' (None)'
                print(output) 
开发者ID:reBiocoder,项目名称:bioforum,代码行数:43,代码来源:ogrinfo.py

示例7: _from_file

# 需要导入模块: from django.contrib.gis import gdal [as 别名]
# 或者: from django.contrib.gis.gdal import DataSource [as 别名]
def _from_file(self, fileobj, tmpdir):
        if zipfile.is_zipfile(fileobj):
            with zipfile.ZipFile(fileobj) as zf:
                extracted = []
                for item in zf.infolist():
                    fname = os.path.abspath(os.path.join(tmpdir, item.filename))
                    if fname.startswith(tmpdir):
                        zf.extract(item, tmpdir)
                        extracted.append(fname)
                for path in extracted:
                    if path.endswith('.shp'):
                        fname = path
        else:
            # NOTE: is_zipfile() seeks to end of file or at least 110 bytes.
            fileobj.seek(0)
            with tempfile.NamedTemporaryFile(dir=tmpdir, delete=False) as fp:
                shutil.copyfileobj(fileobj, fp)
            fname = fp.name
        # Attempt to union all geometries from GDAL data source.
        try:
            geoms = gdal.DataSource(fname)[0].get_geoms()
            geom = reduce(lambda g1, g2: g1.union(g2), geoms)
            if not geom.srs:
                raise gdal.GDALException('Cannot determine SRS')
        except (gdal.GDALException, IndexError):
            raise forms.ValidationError(
                GeometryField.default_error_messages['invalid_geom'],
                code='invalid_geom')
        return geom 
开发者ID:bkg,项目名称:django-spillway,代码行数:31,代码来源:fields.py

示例8: mapping

# 需要导入模块: from django.contrib.gis import gdal [as 别名]
# 或者: from django.contrib.gis.gdal import DataSource [as 别名]
def mapping(data_source, geom_name='geom', layer_key=0, multi_geom=False):
    """
    Given a DataSource, generates a dictionary that may be used
    for invoking the LayerMapping utility.

    Keyword Arguments:
     `geom_name` => The name of the geometry field to use for the model.

     `layer_key` => The key for specifying which layer in the DataSource to use;
       defaults to 0 (the first layer).  May be an integer index or a string
       identifier for the layer.

     `multi_geom` => Boolean (default: False) - specify as multigeometry.
    """
    if isinstance(data_source, six.string_types):
        # Instantiating the DataSource from the string.
        data_source = DataSource(data_source)
    elif isinstance(data_source, DataSource):
        pass
    else:
        raise TypeError('Data source parameter must be a string or a DataSource object.')

    # Creating the dictionary.
    _mapping = {}

    # Generating the field name for each field in the layer.
    for field in data_source[layer_key].fields:
        mfield = field.lower()
        if mfield[-1:] == '_':
            mfield += 'field'
        _mapping[mfield] = field
    gtype = data_source[layer_key].geom_type
    if multi_geom:
        gtype.to_multi()
    _mapping[geom_name] = str(gtype).upper()
    return _mapping 
开发者ID:drexly,项目名称:openhgsenti,代码行数:38,代码来源:ogrinspect.py

示例9: handle

# 需要导入模块: from django.contrib.gis import gdal [as 别名]
# 或者: from django.contrib.gis.gdal import DataSource [as 别名]
def handle(self, *args, **options):
        data_source, model_name = options.pop('data_source'), options.pop('model_name')
        if not gdal.HAS_GDAL:
            raise CommandError('GDAL is required to inspect geospatial data sources.')

        # Getting the OGR DataSource from the string parameter.
        try:
            ds = gdal.DataSource(data_source)
        except gdal.GDALException as msg:
            raise CommandError(msg)

        # Returning the output of ogrinspect with the given arguments
        # and options.
        from django.contrib.gis.utils.ogrinspect import _ogrinspect, mapping
        # Filter options to params accepted by `_ogrinspect`
        ogr_options = {k: v for k, v in options.items()
                       if k in get_func_args(_ogrinspect) and v is not None}
        output = [s for s in _ogrinspect(ds, model_name, **ogr_options)]

        if options['mapping']:
            # Constructing the keyword arguments for `mapping`, and
            # calling it on the data source.
            kwargs = {'geom_name': options['geom_name'],
                      'layer_key': options['layer_key'],
                      'multi_geom': options['multi_geom'],
                      }
            mapping_dict = mapping(ds, **kwargs)
            # This extra legwork is so that the dictionary definition comes
            # out in the same order as the fields in the model definition.
            rev_mapping = {v: k for k, v in mapping_dict.items()}
            output.extend(['', '# Auto-generated `LayerMapping` dictionary for %s model' % model_name,
                           '%s_mapping = {' % model_name.lower()])
            output.extend("    '%s' : '%s'," % (
                rev_mapping[ogr_fld], ogr_fld) for ogr_fld in ds[options['layer_key']].fields
            )
            output.extend(["    '%s' : '%s'," % (options['geom_name'], mapping_dict[options['geom_name']]), '}'])
        return '\n'.join(output) + '\n' 
开发者ID:drexly,项目名称:openhgsenti,代码行数:39,代码来源:ogrinspect.py

示例10: test_layermap_strict

# 需要导入模块: from django.contrib.gis import gdal [as 别名]
# 或者: from django.contrib.gis.gdal import DataSource [as 别名]
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) 
开发者ID:nesdis,项目名称:djongo,代码行数:36,代码来源:tests.py

示例11: ogrinfo

# 需要导入模块: from django.contrib.gis import gdal [as 别名]
# 或者: from django.contrib.gis.gdal import DataSource [as 别名]
def ogrinfo(data_source, num_features=10):
    """
    Walks the available layers in the supplied `data_source`, displaying
    the fields for the first `num_features` features.
    """

    # Checking the parameters.
    if isinstance(data_source, str):
        data_source = DataSource(data_source)
    elif isinstance(data_source, DataSource):
        pass
    else:
        raise Exception('Data source parameter must be a string or a DataSource object.')

    for i, layer in enumerate(data_source):
        print("data source : %s" % data_source.name)
        print("==== layer %s" % i)
        print("  shape type: %s" % GEO_CLASSES[layer.geom_type.num].__name__)
        print("  # features: %s" % len(layer))
        print("         srs: %s" % layer.srs)
        extent_tup = layer.extent.tuple
        print("      extent: %s - %s" % (extent_tup[0:2], extent_tup[2:4]))
        print("Displaying the first %s features ====" % num_features)

        width = max(*map(len, layer.fields))
        fmt = " %%%ss: %%s" % width
        for j, feature in enumerate(layer[:num_features]):
            print("=== Feature %s" % j)
            for fld_name in layer.fields:
                type_name = feature[fld_name].type_name
                output = fmt % (fld_name, type_name)
                val = feature.get(fld_name)
                if val:
                    if isinstance(val, str):
                        val_fmt = ' ("%s")'
                    else:
                        val_fmt = ' (%s)'
                    output += val_fmt % val
                else:
                    output += ' (None)'
                print(output)

# For backwards compatibility. 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:45,代码来源:ogrinfo.py

示例12: load_areas

# 需要导入模块: from django.contrib.gis import gdal [as 别名]
# 或者: from django.contrib.gis.gdal import DataSource [as 别名]
def load_areas(geo_layer):
    geo_shape_file = geo_layer.geo_shape_file

    if not geo_shape_file:
        geo_layer.stale_areas = False
        geo_layer.save()
        return

    # Create temporary file with same content
    # This is necessary in server where the file is
    # originally in s3 server and GDAL expects file in local
    # disk.
    # Then load data from that file
    filename, extension = os.path.splitext(geo_shape_file.file.name)
    f = tempfile.NamedTemporaryFile(suffix=extension,
                                    dir=settings.BASE_DIR)
    f.write(geo_shape_file.file.read())

    # Flush the file before reading it with GDAL
    # Otherwise, for small files, GDAL may attempt to read before
    # the write is complete and will raise an exception.
    f.flush()

    if extension == '.zip':
        with tempfile.TemporaryDirectory(
            dir=settings.BASE_DIR
        ) as tmpdirname:
            zipfile.ZipFile(f.name, 'r').extractall(tmpdirname)
            files = os.listdir(tmpdirname)
            shape_file = next((f for f in files if f.endswith('.shp')),
                              None)
            data_source = DataSource(os.path.join(tmpdirname, shape_file))
    else:
        data_source = DataSource(f.name)

    f.close()


    # If more than one layer exists, extract from the first layer
    if data_source.layer_count == 1:
        layer = data_source[0]

        added_areas = []
        for feature in layer:
            # Each feature is a geo area
            geo_area = _save_geo_area(geo_layer, feature)
            added_areas.append(geo_area.id)

        # Delete all previous geo areas that have not been added
        GeoArea.objects.filter(
            geo_layer=geo_layer,
        ).exclude(id__in=added_areas).delete()

    geo_layer.stale_areas = False
    geo_layer.save() 
开发者ID:awemulya,项目名称:kobo-predict,代码行数:57,代码来源:loader.py


注:本文中的django.contrib.gis.gdal.DataSource方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。