API GDAL¶
GDAL stands for Geospatial Data Abstraction Library, and is a veritable "Swiss army knife" of GIS data functionality. A subset of GDAL is the OGR Simple Features Library, which specializes in reading and writing vector geographic data in a variety of standard formats.
GeoDjango provides a high-level Python interface for some of the capabilities of OGR, including the reading and coordinate transformation of vector spatial data and minimal support for GDAL's features with respect to raster (image) data.
Catatan
Although the module is named gdal
, GeoDjango only supports some of the
capabilities of OGR and GDAL's raster features at this time.
Ikhtisar¶
Data Contoh¶
The GDAL/OGR tools described here are designed to help you read in your geospatial data, in order for most of them to be useful you have to have some data to work with. If you're starting out and don't yet have any data of your own to use, GeoDjango tests contain a number of simple data sets that you can use for testing. You can download them here:
$ wget https://raw.githubusercontent.com/django/django/master/tests/gis_tests/data/cities/cities.{shp,prj,shx,dbf}
$ wget https://raw.githubusercontent.com/django/django/master/tests/gis_tests/data/rasters/raster.tif
Vector Data Source Object¶
DataSource
¶
DataSource
adalah sebuah pembungkus untuk obyek sumber data OGR yang mendukung membaca data dari beragam dari bentuk berkas geospasial didukung-OGR dan sumber data menggunakan sederhana, konsisten antarmuka. Setiap sumber data diwakili oleh sebuah obyek DataSource
yang mengandung satu atau lebih lapisan data. Setiap lapisan, diwakili oleh Layer
object, mengandung beberapa nomor dari fitur-fitur geografik (Feature
), informasi tentang jenis dari fitur-fitur mengandung di lapisan itu (sebagai contoh titik, poligon, dll.), sama halnya nama-nama dan jenis-jenis dari bidang tambahan apapun (Field
) dari data yang mungkin terhubung dengan setiap fitur di lapisan itu.
-
class
DataSource
(ds_input, encoding='utf-8')¶ Pembangun untuk
DataSource
hanya membutuhkan satu parameter: jalur dari berkas anda ingin baca. Bagaimanapun, OGR juga mendukung beragam sumber data lebih rumit, termasuk basisdata, yang mungkin diakses dengan melewatkan string nama khusus daripada jalur. Untuk informasi lebih, lihat dokumentasi OGR Vector Formats. Sifatname
dari sebuah instanceDataSource
memberikan nama OGR dari sumber data pokok yang itu sedang gunakan.Pilihan parameter
encoding
mengizinkan anda menentukan penyandian bukan-standar dari string di sumber. Ini khususnya berguna ketika anda mendapatkan pengecualianDjangoUnicodeDecodeError
selagi membaca nilai bidang.Sekali anda telah membuat
DataSource
anda, anda dapat menemukan seberapa banyak lapisan data itu kandung dengan mengakses sifatlayer_count
, atau (setara) dengan menggunakan fungsilen()
. Untuk informasi pada mengakses lapisan dari data mereka sendiri, lihat bagian lain:>>> from django.contrib.gis.gdal import DataSource >>> ds = DataSource('/path/to/your/cities.shp') >>> ds.name '/path/to/your/cities.shp' >>> ds.layer_count # This file only contains one layer 1
-
layer_count
¶
Mengembalikan sejumlah lapisan di sumber data.
-
name
¶
Mengembalikan nama dari sumber data.
-
Lapisan
¶
-
class
Layer
¶ Layer
adalah sebuah pembungkus untuk lapisan dari data di obyekDataSource
. Anda tidak pernah membuat obyekLayer
secara langsung. Sebagai gantinya, anda mengambil mereka dari obyekDataSource
, yang pada dasarnya wadah standar Python dari obyekLayer
. Sebagai contoh, anda dapat mengakses lapisan khusus dengan indeksnya (sebagai contohds[0]
untuk mengakses lapisan pertama), atau anda dapat mengulang terhadap semua lapisan di wadah dalam perulanganloop
.Layer
itu sendiri bertindak sebagai sebuah wadah untuk fitur-fitur geometris.Khususnya, semua fitur di lapisan yang diberikan mempunyai jenis geometri sama. Sifat
geom_type
dari lapisan adalah sebuahOGRGeomType
yang mencirikan jenis fitur. Kami dapat menggunakan itu untuk mencetak beberapa informasi dasar tentang setiap lapisan diDataSource
:>>> for layer in ds: ... print('Layer "%s": %i %ss' % (layer.name, len(layer), layer.geom_type.name)) ... Layer "cities": 3 Points
Keluaran contoh adalah dari sumber data kota, dimuat diatas, yang ternyata mengandung satu lapisan, dipanggil
"cities"
, yang mengandung tida titik fitur. Untuk kemudahan, contoh-contoh diatas menganggap bahwa anda telah menyimpan lapisan itu di variabellayer
:>>> layer = ds[0]
-
name
¶
Mengembalikan nama lapisan ini di sumber data.
>>> layer.name 'cities'
-
num_feat
¶
Mengembalikan sejumlah fitur-fitur di lapisan. Sama seperti
len(layer)
:>>> layer.num_feat 3
-
geom_type
¶
Mengembalikan jenis geometri dari lapisan, sebagai sebuah obyek
OGRGeomType
:>>> layer.geom_type.name 'Point'
-
num_fields
¶
Mengembalikan sejumlah bidang di lapisan, yaitu sejumlah bidang dari data terhubung dengan setiap fitur di lapisan:
>>> layer.num_fields 4
-
fields
¶
Mengembalikan daftar nama dari setiap bidang di lapisan ini:
>>> layer.fields ['Name', 'Population', 'Density', 'Created']
Mengembalikan daftar dari jenis-jenis data dari setiap bidang di lapisan ini. Ini adalah subkelas dari
Field
, diobrolkan dibawah:>>> [ft.__name__ for ft in layer.field_types] ['OFTString', 'OFTReal', 'OFTReal', 'OFTDate']
-
field_widths
¶
Mengembalikan daftar dari bidang maksimal untuk setiap bidang dalam lapisan ini:
>>> layer.field_widths [80, 11, 24, 10]
-
field_precisions
¶
Mengembalikan daftar dari angka ketelitian untuk setiap dari bidang-bidang dalam lapisan ini. Ini tidak berarti (dan disetel ke nol) untuk bidang bukan-numerik:
>>> layer.field_precisions [0, 0, 15, 0]
-
extent
¶
Mengembalikan tingkatan spasial dari lapisan ini, sebagai sebuah obyek
Envelope
:>>> layer.extent.tuple (-104.609252, 29.763374, -95.23506, 38.971823)
-
srs
¶
Sifat yang mengembalikan
SpatialReference
terkait dengan lapisan ini:>>> print(layer.srs) GEOGCS["GCS_WGS_1984", DATUM["WGS_1984", SPHEROID["WGS_1984",6378137,298.257223563]], PRIMEM["Greenwich",0], UNIT["Degree",0.017453292519943295]]
Jika
Layer
tidak mempunyai informasi acuan spasial terkait dengan itu, `` None`` dikembalikan.-
spatial_filter
¶
Property that may be used to retrieve or set a spatial filter for this layer. A spatial filter can only be set with an
OGRGeometry
instance, a 4-tuple extent, orNone
. When set with something other thanNone
, only features that intersect the filter will be returned when iterating over the layer:>>> print(layer.spatial_filter) None >>> print(len(layer)) 3 >>> [feat.get('Name') for feat in layer] ['Pueblo', 'Lawrence', 'Houston'] >>> ks_extent = (-102.051, 36.99, -94.59, 40.00) # Extent for state of Kansas >>> layer.spatial_filter = ks_extent >>> len(layer) 1 >>> [feat.get('Name') for feat in layer] ['Lawrence'] >>> layer.spatial_filter = None >>> len(layer) 3
-
get_fields
()¶
Sebuah metode yang mengembalikan sebuah daftar dari nilai-nilai dari bidang yang diberikan untuk setiap fitur dalam lapisan:
>>> layer.get_fields('Name') ['Pueblo', 'Lawrence', 'Houston']
-
get_geoms
(geos=False)¶
A method that returns a list containing the geometry of each feature in the layer. If the optional argument
geos
is set toTrue
then the geometries are converted toGEOSGeometry
objects. Otherwise, they are returned asOGRGeometry
objects:>>> [pt.tuple for pt in layer.get_geoms()] [(-104.609252, 38.255001), (-95.23506, 38.971823), (-95.363151, 29.763374)]
-
test_capability
(capability)¶
Returns a boolean indicating whether this layer supports the given capability (a string). Examples of valid capability strings include:
'RandomRead'
,'SequentialWrite'
,'RandomWrite'
,'FastSpatialFilter'
,'FastFeatureCount'
,'FastGetExtent'
,'CreateField'
,'Transactions'
,'DeleteFeature'
, and'FastSetNextByIndex'
.-
Feature
¶
-
class
Feature
¶ Feature
membungkus fitur OGR. Anda tidak pernah membuat obyekFeature
secara langsung. Sebagai gantinya, anda mengambil mereka dari obyekLayer
. Setiap fitur terdiri dari sebuah geometri dan sekumpulan bidang mengandung sifat-sifat tambahan. Geometri dari sebuah bidang adalah dapat diakses melalui sifatgeom
nya, yang mengembalikan sebuah obyekOGRGeometry
. SebuahFeature
berperilaku seperti wadah Python standar untuk bidangnya, yang itu dikembalikan sebagai obyekField
: anda dapat mengakses sebuah bidang secara langsung berdasarkan indeks atau namanya, atau dapat berulang terhadap bidang-bidang fitur, sebagai contoh di sebuah perulanganfor
.-
geom
¶
Mengembalikan geometri untuk fotur ini, sebagai sebuah obyek
OGRGeometry
:>>> city.geom.tuple (-104.609252, 38.255001)
-
get
¶
Sebuah metode yang mengembalikan nilai dari bidang yang diberikan (ditentukan oleh nama) untuk fitur ini, bukan sebuah obyek pembungkus
Field
:>>> city.get('Population') 102121
-
geom_type
¶
Returns the type of geometry for this feature, as an
OGRGeomType
object. This will be the same for all features in a given layer and is equivalent to theLayer.geom_type
property of theLayer
object the feature came from.-
num_fields
¶
Returns the number of fields of data associated with the feature. This will be the same for all features in a given layer and is equivalent to the
Layer.num_fields
property of theLayer
object the feature came from.-
fields
¶
Returns a list of the names of the fields of data associated with the feature. This will be the same for all features in a given layer and is equivalent to the
Layer.fields
property of theLayer
object the feature came from.-
fid
¶
Mengembalikan penciri fitur dalam lapisan:
>>> city.fid 0
-
layer_name
¶
Mengembalikan nama dari
Layer
yang berasal fitur. Ini akan menjadi sama untuk semua fitur dalam lapisan yang diberikan:>>> city.layer_name 'cities'
-
index
¶
Sebuah metode yang mengembalikan indeks dari nama bidang yang diberikan. Ini akan sama untuk semua fitur-fitur dalam lapisan yang diberikan:
>>> city.index('Population') 1
-
Field
¶
-
class
Field
¶ -
name
¶
Mengembalikan nama dari bidang ini:
>>> city['Name'].name 'Name'
-
type
¶
Mengembalikan jenis OGR dari bidang ini, sebagai sebuah integer. Dictionary
FIELD_CLASSES
memetakan nilai-nilai ini kedalam subkelas dariField
:>>> city['Density'].type 2
-
type_name
¶
Mengembalikan string dengan nama dari jenis data dari bidang ini:
>>> city['Name'].type_name 'String'
-
value
¶
Mengembalikan nilai dari bidang ini. Kelas
Field
itu sendiri mengembalikan nilai sebagai sebuah string, tetapi setiap subkelas mengembalikan nilai dalam bentuk paling sesuai:>>> city['Population'].value 102121
-
width
¶
Mengembalikan lebar bidang ini:
>>> city['Name'].width 80
-
precision
¶
Mengembalikan ketelitian numerik dari bidang ini. Ini tidak berarti (dan disetel ke nol) untuk bidang-bidang bukan-numerik:
>>> city['Density'].precision 15
-
as_double
()¶
Mengembalikan nilai dari bidang sebagai double (float):
>>> city['Density'].as_double() 874.7
-
as_int
()¶
Mengembalikan nilai dari bidang sebagai integer:
>>> city['Population'].as_int() 102121
-
as_string
()¶
Mengembalikan nilai dari bidang sebagai deretan kalimat:
>>> city['Name'].as_string() 'Pueblo'
-
as_datetime
()¶
Mengembalikan nilai dari bidang sebagai tuple dari komponen tanggal dan waktu:
>>> city['Created'].as_datetime() (c_long(1999), c_long(5), c_long(23), c_long(0), c_long(0), c_long(0), c_long(0))
-
Driver
¶
-
class
Driver
(dr_input)¶ Kelas
Driver
digunakan secara mendalam untuk membungkus sebuah driverDataSource
OGR.-
driver_count
¶
Mengembalikan sejumlah driver vektor OGR saat ini terdaftar.
-
Geometri OGR¶
OGRGeometry
¶
Obyek-obyek OGRGeometry
berbagi fungsi mirip dengan obyek GEOSGeometry
dan pembungkus tipis disekitar perwakilan geometri internal OGR. Dengan demikian, mereka mengizinkan untuk lebih efektid mengakses ke data ketika menggunakan DataSource
. Tidak seprti pasangan GEOS nya, OGRGeometry
mendukung sistem acuan spasial dan perubahan kordinat:
>>> from django.contrib.gis.gdal import OGRGeometry
>>> polygon = OGRGeometry('POLYGON((0 0, 5 0, 5 5, 0 5))')
-
class
OGRGeometry
(geom_input, srs=None)¶ Obyek ini adalah sebuah pembungkus untuk kelas OGR Geometry. Obyek-obyek ini diinstasiasikan secara langsung dari parameter
geom_input
yang diberikan, yang mungkin berupa string mengandung WKT, HEX, GeoJSON, sebuahbuffer
mengandung data WKB, atau sebuah obyekOGRGeomType
. Obyek-obyek ini juga dikembalikan dari atributFeature.geom
, ketika membaca data vektor dariLayer
(yaitu pada giliran bagian dari sebuahDataSource
).-
classmethod
from_gml
(gml_string)¶
New in Django 1.11.Membangun sebuah
OGRGeometry
dari string GML yang diberikan.-
classmethod
from_bbox
(bbox)¶
Membangun sebuah
Polygon
dari kotak-terikat diberikan (4-tuple).-
__len__
()¶
Mengembalikan sejumlah titik dalam sebuah
LineString
, sejumlah geometri dalam sebuahGeometryCollection
. Tidak diberlakukan ke jenis geometri lain.-
__iter__
()¶
Iterates over the points in a
LineString
, the rings in aPolygon
, or the geometries in aGeometryCollection
. Not applicable to other geometry types.-
__getitem__
()¶
Returns the point at the specified index for a
LineString
, the interior ring at the specified index for aPolygon
, or the geometry at the specified index in aGeometryCollection
. Not applicable to other geometry types.-
dimension
¶
Mengembalikan sejumlah dimensi kordinat dari geometri, yaitu 0 untuk titik, 1 untuk baris, dan sebagainya:
>> polygon.dimension 2
-
coord_dim
¶
Returns or sets the coordinate dimension of this geometry. For example, the value would be 2 for two-dimensional geometries.
-
geom_count
¶
Mengembalikan sejumlah unsur dalam geometri ini:
>>> polygon.geom_count 1
-
point_count
¶
Mengembalikan sejumlah titik digunakan untuk menggambarkan geometri ini:
>>> polygon.point_count 4
-
num_points
¶
Nama lain untuk
point_count
.-
num_coords
¶
Nama lain untuk
point_count
.-
geom_type
¶
Mengembalikan jenis dari geometri ini, sebagai sebuah obyek
OGRGeomType
.-
geom_name
¶
Mengembalikan nama dari jenis dari geometri ini:
>>> polygon.geom_name 'POLYGON'
-
area
¶
Mengembalikan kawasan dari geometri ini, atau 0 untuk geometri yang tidak mengandung sebuah kawasan:
>>> polygon.area 25.0
-
envelope
¶
Mengembalikan sampul dari geometri ini, sebagai sebuah obyek
Envelope
.-
extent
¶
Returns the envelope of this geometry as a 4-tuple, instead of as an
Envelope
object:>>> point.extent (0.0, 0.0, 5.0, 5.0)
-
srs
¶
Sifat ini mengendalikan acuan spasial untuk geometri ini, atau
None
jika tidak ada sisterm acuan spasia telah diberikan ke itu. Jika diberikan, mengakses sifat ini mengembalikan sebuah obyekSpatialReference
. Itu mungkin disetel dengan obyekSpatialReference
lain, atau masukan apapun yangSpatialReference
terima. Contoh:>>> city.geom.srs.name 'GCS_WGS_1984'
-
srid
¶
Returns or sets the spatial reference identifier corresponding to
SpatialReference
of this geometry. ReturnsNone
if there is no spatial reference information associated with this geometry, or if an SRID cannot be determined.-
geos
¶
Mengebalikan obyek
GEOSGeometry
sesuai pada geometri ini.-
gml
¶
Mengembalikan sebuah string perwakilan dari geometri ini dalam bentuk GML:
>>> OGRGeometry('POINT(1 2)').gml '<gml:Point><gml:coordinates>1,2</gml:coordinates></gml:Point>'
-
hex
¶
Mengembalikan perwakilan string dari geometri ini dalam bentuk HEX WKB:
>>> OGRGeometry('POINT(1 2)').hex '0101000000000000000000F03F0000000000000040'
-
json
¶
Mengembalikan string perwakilan dari geometri ini dalam bentuk JSON:
>>> OGRGeometry('POINT(1 2)').json '{ "type": "Point", "coordinates": [ 1.000000, 2.000000 ] }'
-
kml
¶
Mengembalikan perwakilan string dari geometri ini dalam bentuk KML.
-
wkb_size
¶
Mengembalikan ukuran dari penyangga WKB untuk menahan perwakilan WKB dari geometri ini:
>>> OGRGeometry('POINT(1 2)').wkb_size 21
-
wkb
¶
Mengembalikan sebuah
buffer
mengandung perwakilan WKB dari geometri ini.-
wkt
¶
Mengembalikan perwakilan string dari geometri ini dalam bentuk WKT.
-
ewkt
¶
Mengembalikan perwakilan EWKT dari geometri ini.
-
clone
()¶
Mengembalikan klon baru
OGRGeometry
dari obyek geometri ini.-
close_rings
()¶
Jika ada lingkaran apapun dalam geometri ini yang belum ditutup, rutin ini akan melakukannya dengan menambahkan titik awalan ke akhiran:
>>> triangle = OGRGeometry('LINEARRING (0 0,0 1,1 0)') >>> triangle.close_rings() >>> triangle.wkt 'LINEARRING (0 0,0 1,1 0,0 0)'
-
transform
(coord_trans, clone=False)¶
Transforms this geometry to a different spatial reference system. May take a
CoordTransform
object, aSpatialReference
object, or any other input accepted bySpatialReference
(including spatial reference WKT and PROJ.4 strings, or an integer SRID).By default nothing is returned and the geometry is transformed in-place. However, if the
clone
keyword is set toTrue
then a transformed clone of this geometry is returned instead.-
intersects
(other)¶
Mengembalikan
True
jika geometri ini memotong ke lain, sebaliknya mengembalikanFalse
.-
equals
(other)¶
Mengembalikan
True
jika geometri ini setara dengan lain, sebaliknya mengembalikanTrue
.-
disjoint
(other)¶
Returns
True
if this geometry is spatially disjoint to (i.e. does not intersect) the other, otherwise returnsFalse
.-
touches
(other)¶
Mengembalikan
True
jika geometri ini menyentuh lainnya, sebaliknya mengembalikanFalse
.-
crosses
(other)¶
Mengembalikan
True
jika geometri ini bersilangan ke lainnya, sebaliknya mengembalikanFalse
.-
within
(other)¶
Returns
True
if this geometry is contained within the other, otherwise returnsFalse
.-
contains
(other)¶
Returns
True
if this geometry contains the other, otherwise returnsFalse
.-
overlaps
(other)¶
Returns
True
if this geometry overlaps the other, otherwise returnsFalse
.-
boundary
()¶
The boundary of this geometry, as a new
OGRGeometry
object.-
convex_hull
¶
The smallest convex polygon that contains this geometry, as a new
OGRGeometry
object.-
difference
()¶
Returns the region consisting of the difference of this geometry and the other, as a new
OGRGeometry
object.-
intersection
()¶
Returns the region consisting of the intersection of this geometry and the other, as a new
OGRGeometry
object.-
sym_difference
()¶
Returns the region consisting of the symmetric difference of this geometry and the other, as a new
OGRGeometry
object.-
union
()¶
Returns the region consisting of the union of this geometry and the other, as a new
OGRGeometry
object.-
tuple
¶
Mengembalikan kordinat-kordinat dari titik geometri sebagai sebuah tuple, kordinat-kordinat dari baris geometri sebagai sebuah tuple dari tuple, dan sebagainya:
>>> OGRGeometry('POINT (1 2)').tuple (1.0, 2.0) >>> OGRGeometry('LINESTRING (1 2,3 4)').tuple ((1.0, 2.0), (3.0, 4.0))
-
coords
¶
Sebuah nama lain untuk
tuple
.-
classmethod
-
class
Point
¶ -
x
¶
Mengembalikan kordinat X dari titik ini:
>>> OGRGeometry('POINT (1 2)').x 1.0
-
y
¶
Mengembalikan kordinat Y dari titik ini:
>>> OGRGeometry('POINT (1 2)').y 2.0
-
z
¶
Returns the Z coordinate of this point, or
None
if the point does not have a Z coordinate:>>> OGRGeometry('POINT (1 2 3)').z 3.0
-
-
class
LineString
¶ -
x
¶
Mengembalikan sebuah daftar dari kordinat X dalam baris ini:
>>> OGRGeometry('LINESTRING (1 2,3 4)').x [1.0, 3.0]
-
y
¶
Mengembalikan sebuah daftar dari kordinat Y dalam baris ini:
>>> OGRGeometry('LINESTRING (1 2,3 4)').y [2.0, 4.0]
-
z
¶
Returns a list of Z coordinates in this line, or
None
if the line does not have Z coordinates:>>> OGRGeometry('LINESTRING (1 2 3,4 5 6)').z [3.0, 6.0]
-
OGRGeomType
¶
-
class
OGRGeomType
(type_input)¶ Kelas ini mengizinkan untuk gambaran dari jenis geometri OGR dalam beberapa cara:
>>> from django.contrib.gis.gdal import OGRGeomType >>> gt1 = OGRGeomType(3) # Using an integer for the type >>> gt2 = OGRGeomType('Polygon') # Using a string >>> gt3 = OGRGeomType('POLYGON') # It's case-insensitive >>> print(gt1 == 3, gt1 == 'Polygon') # Equivalence works w/non-OGRGeomType objects True True
-
name
¶
Returns a short-hand string form of the OGR Geometry type:
>>> gt1.name 'Polygon'
-
num
¶
Mengembalikan sejumlah kaitan pada jenis geometri OGR:
>>> gt1.num 3
-
django
¶
Returns the Django field type (a subclass of GeometryField) to use for storing this OGR type, or
None
if there is no appropriate Django type:>>> gt1.django 'PolygonField'
-
Envelope
¶
-
class
Envelope
(*args)¶ Represents an OGR Envelope structure that contains the minimum and maximum X, Y coordinates for a rectangle bounding box. The naming of the variables is compatible with the OGR Envelope C structure.
-
min_x
¶
Nilai minimal kordinat X
-
min_y
¶
Nilai maksimal kordinat X.
-
max_x
¶
Nilai minimal kordinat Y.
-
max_y
¶
Nilai maksimal kordinat Y.
-
ur
¶
Kordinat atas-kanan, sebagai sebuah tuple.
-
ll
¶
Kordinat kiri-bawah, sebagai sebuah tuple.
-
tuple
¶
A tuple representing the envelope.
-
wkt
¶
A string representing this envelope as a polygon in WKT format.
-
expand_to_include
(*args)¶
-
Coordinate System Object¶
SpatialReference
¶
-
class
SpatialReference
(srs_input)¶ Spatial reference objects are initialized on the given
srs_input
, which may be one of the following:- OGC Well Known Text (WKT) (sebuah string)
- Kode EPSG(integer atau string)
- String PROJ.4
- A shorthand string for well-known standards (
'WGS84'
,'WGS72'
,'NAD27'
,'NAD83'
)
Contoh:
>>> wgs84 = SpatialReference('WGS84') # shorthand string >>> wgs84 = SpatialReference(4326) # EPSG code >>> wgs84 = SpatialReference('EPSG:4326') # EPSG string >>> proj4 = '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs ' >>> wgs84 = SpatialReference(proj4) # PROJ.4 string >>> wgs84 = SpatialReference("""GEOGCS["WGS 84", DATUM["WGS_1984", SPHEROID["WGS 84",6378137,298.257223563, AUTHORITY["EPSG","7030"]], AUTHORITY["EPSG","6326"]], PRIMEM["Greenwich",0, AUTHORITY["EPSG","8901"]], UNIT["degree",0.01745329251994328, AUTHORITY["EPSG","9122"]], AUTHORITY["EPSG","4326"]]""") # OGC WKT
-
__getitem__
(target)¶
Returns the value of the given string attribute node,
None
if the node doesn't exist. Can also take a tuple as a parameter, (target, child), where child is the index of the attribute in the WKT. For example:>>> wkt = 'GEOGCS["WGS 84", DATUM["WGS_1984, ... AUTHORITY["EPSG","4326"]]') >>> srs = SpatialReference(wkt) # could also use 'WGS84', or 4326 >>> print(srs['GEOGCS']) WGS 84 >>> print(srs['DATUM']) WGS_1984 >>> print(srs['AUTHORITY']) EPSG >>> print(srs['AUTHORITY', 1]) # The authority value 4326 >>> print(srs['TOWGS84', 4]) # the fourth value in this wkt 0 >>> print(srs['UNIT|AUTHORITY']) # For the units authority, have to use the pipe symbol. EPSG >>> print(srs['UNIT|AUTHORITY', 1]) # The authority value for the units 9122
-
attr_value
(target, index=0)¶
The attribute value for the given target node (e.g.
'PROJCS'
). The index keyword specifies an index of the child node to return.-
auth_name
(target)¶
Returns the authority name for the given string target node.
-
auth_code
(target)¶
Returns the authority code for the given string target node.
-
clone
()¶
Returns a clone of this spatial reference object.
-
identify_epsg
()¶
Metode ini memeriksa WKT dari
SpatialReference
ini dan akan menambahkan node-node wewenang EPSG dimana sebuah penciri EPSG dapat diterapkan.-
from_esri
()¶
Morphs this SpatialReference from ESRI's format to EPSG
-
to_esri
()¶
Morphs this SpatialReference to ESRI's format.
-
validate
()¶
Memeriksa untuk melihat jika acuan spasial diberikan adalah sah, jika tidak sebuah pengecualian akan dimunculkan.
-
import_epsg
(epsg)¶
Import spatial reference from EPSG code.
-
import_proj
(proj)¶
Import spatial reference from PROJ.4 string.
-
import_user_input
(user_input)¶
-
import_wkt
(wkt)¶
Import spatial reference from WKT.
-
import_xml
(xml)¶
Import spatial reference from XML.
-
name
¶
Returns the name of this Spatial Reference.
-
srid
¶
Returns the SRID of top-level authority, or
None
if undefined.-
linear_name
¶
Mengambalikan nama dari satuan linear.
-
linear_units
¶
Mengembalikan nilai dari satuan linear.
-
angular_name
¶
Mengembalikan nama dari satuan sudut."
-
angular_units
¶
Mengembalikan nilai dari satuan sudut.
-
units
¶
Returns a 2-tuple of the units value and the units name and will automatically determines whether to return the linear or angular units.
-
ellipsoid
¶
Returns a tuple of the ellipsoid parameters for this spatial reference: (semimajor axis, semiminor axis, and inverse flattening).
-
semi_major
¶
Returns the semi major axis of the ellipsoid for this spatial reference.
-
semi_minor
¶
Returns the semi minor axis of the ellipsoid for this spatial reference.
-
inverse_flattening
¶
Returns the inverse flattening of the ellipsoid for this spatial reference.
-
geographic
¶
Returns
True
if this spatial reference is geographic (root node isGEOGCS
).-
local
¶
Returns
True
if this spatial reference is local (root node isLOCAL_CS
).-
projected
¶
Returns
True
if this spatial reference is a projected coordinate system (root node isPROJCS
).-
wkt
¶
Returns the WKT representation of this spatial reference.
-
pretty_wkt
¶
Returns the 'pretty' representation of the WKT.
-
proj
¶
Returns the PROJ.4 representation for this spatial reference.
-
proj4
¶
Nama lain untuk
SpatialReference.proj
.-
xml
¶
Returns the XML representation of this spatial reference.
CoordTransform
¶
-
class
CoordTransform
(source, target)¶
Represents a coordinate system transform. It is initialized with two
SpatialReference
, representing the source and target coordinate
systems, respectively. These objects should be used when performing the same
coordinate transformation repeatedly on different geometries:
>>> ct = CoordTransform(SpatialReference('WGS84'), SpatialReference('NAD83'))
>>> for feat in layer:
... geom = feat.geom # getting clone of feature geometry
... geom.transform(ct) # transforming
Obyek Data Raster¶
GDALRaster
¶
GDALRaster
is a wrapper for the GDAL raster source object that
supports reading data from a variety of GDAL-supported geospatial file
formats and data sources using a simple, consistent interface. Each
data source is represented by a GDALRaster
object which contains
one or more layers of data named bands. Each band, represented by a
GDALBand
object, contains georeferenced image data. For example, an RGB
image is represented as three bands: one for red, one for green, and one for
blue.
Catatan
For raster data there is no difference between a raster instance and its
data source. Unlike for the Geometry objects, GDALRaster
objects are
always a data source. Temporary rasters can be instantiated in memory
using the corresponding driver, but they will be of the same class as file-based
raster sources.
-
class
GDALRaster
(ds_input, write=False)¶ The constructor for
GDALRaster
accepts two parameters. The first parameter defines the raster source, and the second parameter defines if a raster should be opened in write mode. For newly-created rasters, the second parameter is ignored and the new raster is always created in write mode.The first parameter can take three forms: a string representing a file path, a dictionary with values defining a new raster, or a bytes object representing a raster file.
If the input is a file path, the raster is opened from there. If the input is raw data in a dictionary, the parameters
width
,height
, andsrid
are required. If the input is a bytes object, it will be opened using a GDAL virtual filesystem.For a detailed description of how to create rasters using dictionary input, see Membuat raster dari data. For a detailed description of how to create rasters in the virtual filesystem, see Using GDAL's Virtual Filesystem.
The following example shows how rasters can be created from different input sources (using the sample data from the GeoDjango tests; see also the Data Contoh section).
>>> from django.contrib.gis.gdal import GDALRaster >>> rst = GDALRaster('/path/to/your/raster.tif', write=False) >>> rst.name '/path/to/your/raster.tif' >>> rst.width, rst.height # This file has 163 x 174 pixels (163, 174) >>> rst = GDALRaster({ # Creates an in-memory raster ... 'srid': 4326, ... 'width': 4, ... 'height': 4, ... 'datatype': 1, ... 'bands': [{ ... 'data': (2, 3), ... 'offset': (1, 1), ... 'size': (2, 2), ... 'shape': (2, 1), ... 'nodata_value': 5, ... }] ... }) >>> rst.srs.srid 4326 >>> rst.width, rst.height (4, 4) >>> rst.bands[0].data() array([[5, 5, 5, 5], [5, 2, 3, 5], [5, 2, 3, 5], [5, 5, 5, 5]], dtype=uint8) >>> rst_file = open('/path/to/your/raster.tif', 'rb') >>> rst_bytes = rst_file.read() >>> rst = GDALRaster(rst_bytes) >>> rst.is_vsi_based True >>> rst.name # Stored in a random path in the vsimem filesystem. '/vsimem/da300bdb-129d-49a8-b336-e410a9428dad'
Changed in Django 1.11:Added the ability to pass the
size
,shape
, andoffset
parameters when creatingGDALRaster
objects. The parameters can be passed through theds_input
dictionary. This allows to finely control initial pixel values. The functionality is similar to theGDALBand.data()
method.Changed in Django 2.0:Added the ability to read and write rasters in GDAL's memory-based virtual filesystem.
GDALRaster
objects can now be converted to and from binary data in-memory.-
name
¶ The name of the source which is equivalent to the input file path or the name provided upon instantiation.
>>> GDALRaster({'width': 10, 'height': 10, 'name': 'myraster', 'srid': 4326}).name 'myraster'
-
driver
¶ The name of the GDAL driver used to handle the input file. For
GDALRaster
s created from a file, the driver type is detected automatically. The creation of rasters from scratch is a in-memory raster by default ('MEM'
), but can be altered as needed. For instance, useGTiff
for aGeoTiff
file. For a list of file types, see also the GDAL Raster Formats list.An in-memory raster is created through the following example:
>>> GDALRaster({'width': 10, 'height': 10, 'srid': 4326}).driver.name 'MEM'
A file based GeoTiff raster is created through the following example:
>>> import tempfile >>> rstfile = tempfile.NamedTemporaryFile(suffix='.tif') >>> rst = GDALRaster({'driver': 'GTiff', 'name': rstfile.name, 'srid': 4326, ... 'width': 255, 'height': 255, 'nr_of_bands': 1}) >>> rst.name '/tmp/tmp7x9H4J.tif' # The exact filename will be different on your computer >>> rst.driver.name 'GTiff'
-
width
¶ The width of the source in pixels (X-axis).
>>> GDALRaster({'width': 10, 'height': 20, 'srid': 4326}).width 10
-
height
¶ The height of the source in pixels (Y-axis).
>>> GDALRaster({'width': 10, 'height': 20, 'srid': 4326}).height 20
-
srs
¶ The spatial reference system of the raster, as a
SpatialReference
instance. The SRS can be changed by setting it to an otherSpatialReference
or providing any input that is accepted by theSpatialReference
constructor.>>> rst = GDALRaster({'width': 10, 'height': 20, 'srid': 4326}) >>> rst.srs.srid 4326 >>> rst.srs = 3086 >>> rst.srs.srid 3086
-
srid
¶ The Spatial Reference System Identifier (SRID) of the raster. This property is a shortcut to getting or setting the SRID through the
srs
attribute.>>> rst = GDALRaster({'width': 10, 'height': 20, 'srid': 4326}) >>> rst.srid 4326 >>> rst.srid = 3086 >>> rst.srid 3086 >>> rst.srs.srid # This is equivalent 3086
-
geotransform
¶ The affine transformation matrix used to georeference the source, as a tuple of six coefficients which map pixel/line coordinates into georeferenced space using the following relationship:
Xgeo = GT(0) + Xpixel*GT(1) + Yline*GT(2) Ygeo = GT(3) + Xpixel*GT(4) + Yline*GT(5)
The same values can be retrieved by accessing the
origin
(indices 0 and 3),scale
(indices 1 and 5) andskew
(indices 2 and 4) properties.Awalnya adalah
[0.0, 1.0, 0.0, 0.0, 0.0, -1.0]
.>>> rst = GDALRaster({'width': 10, 'height': 20, 'srid': 4326}) >>> rst.geotransform [0.0, 1.0, 0.0, 0.0, 0.0, -1.0]
-
origin
¶ Coordinates of the top left origin of the raster in the spatial reference system of the source, as a point object with
x
andy
members.>>> rst = GDALRaster({'width': 10, 'height': 20, 'srid': 4326}) >>> rst.origin [0.0, 0.0] >>> rst.origin.x = 1 >>> rst.origin [1.0, 0.0]
-
scale
¶ Pixel width and height used for georeferencing the raster, as a as a point object with
x
andy
members. Seegeotransform
for more information.>>> rst = GDALRaster({'width': 10, 'height': 20, 'srid': 4326}) >>> rst.scale [1.0, -1.0] >>> rst.scale.x = 2 >>> rst.scale [2.0, -1.0]
-
skew
¶ Skew coefficients used to georeference the raster, as a point object with
x
andy
members. In case of north up images, these coefficients are both0
.>>> rst = GDALRaster({'width': 10, 'height': 20, 'srid': 4326}) >>> rst.skew [0.0, 0.0] >>> rst.skew.x = 3 >>> rst.skew [3.0, 0.0]
-
extent
¶ Extent (boundary values) of the raster source, as a 4-tuple
(xmin, ymin, xmax, ymax)
in the spatial reference system of the source.>>> rst = GDALRaster({'width': 10, 'height': 20, 'srid': 4326}) >>> rst.extent (0.0, -20.0, 10.0, 0.0) >>> rst.origin.x = 100 >>> rst.extent (100.0, -20.0, 110.0, 0.0)
-
bands
¶ List of all bands of the source, as
GDALBand
instances.>>> rst = GDALRaster({"width": 1, "height": 2, 'srid': 4326, ... "bands": [{"data": [0, 1]}, {"data": [2, 3]}]}) >>> len(rst.bands) 2 >>> rst.bands[1].data() array([[ 2., 3.]], dtype=float32)
-
warp
(ds_input, resampling='NearestNeighbour', max_error=0.0)¶ Returns a warped version of this raster.
The warping parameters can be specified through the
ds_input
argument. The use ofds_input
is analogous to the corresponding argument of the class constructor. It is a dictionary with the characteristics of the target raster. Allowed dictionary key values are width, height, SRID, origin, scale, skew, datatype, driver, and name (filename).By default, the warp functions keeps most parameters equal to the values of the original source raster, so only parameters that should be changed need to be specified. Note that this includes the driver, so for file-based rasters the warp function will create a new raster on disk.
The only parameter that is set differently from the source raster is the name. The default value of the the raster name is the name of the source raster appended with
'_copy' + source_driver_name
. For file-based rasters it is recommended to provide the file path of the target raster.The resampling algorithm used for warping can be specified with the
resampling
argument. The default isNearestNeighbor
, and the other allowed values areBilinear
,Cubic
,CubicSpline
,Lanczos
,Average
, andMode
.The
max_error
argument can be used to specify the maximum error measured in input pixels that is allowed in approximating the transformation. The default is 0.0 for exact calculations.Untuk pengguna akrab dengan
GDAL
, fungsi ini mempunyai fungsionalitas mirip pada kegunaan baris-perintahgdalwarp
.For example, the warp function can be used for aggregating a raster to the double of its original pixel scale:
>>> rst = GDALRaster({ ... "width": 6, "height": 6, "srid": 3086, ... "origin": [500000, 400000], ... "scale": [100, -100], ... "bands": [{"data": range(36), "nodata_value": 99}] ... }) >>> target = rst.warp({"scale": [200, -200], "width": 3, "height": 3}) >>> target.bands[0].data() array([[ 7., 9., 11.], [ 19., 21., 23.], [ 31., 33., 35.]], dtype=float32)
-
transform
(srid, driver=None, name=None, resampling='NearestNeighbour', max_error=0.0)¶ Returns a transformed version of this raster with the specified SRID.
This function transforms the current raster into a new spatial reference system that can be specified with an
srid
. It calculates the bounds and scale of the current raster in the new spatial reference system and warps the raster using thewarp
function.By default, the driver of the source raster is used and the name of the raster is the original name appended with
'_copy' + source_driver_name
. A different driver or name can be specified with thedriver
andname
arguments.The default resampling algorithm is
NearestNeighbour
but can be changed using theresampling
argument. The default maximum allowed error for resampling is 0.0 and can be changed using themax_error
argument. Consult thewarp
documentation for detail on those arguments.>>> rst = GDALRaster({ ... "width": 6, "height": 6, "srid": 3086, ... "origin": [500000, 400000], ... "scale": [100, -100], ... "bands": [{"data": range(36), "nodata_value": 99}] ... }) >>> target = rst.transform(4326) >>> target.origin [-82.98492744885776, 27.601924753080144]
-
info
¶ - New in Django 2.0.
Returns a string with a summary of the raster. This is equivalent to the gdalinfo command line utility.
-
metadata
¶ - New in Django 2.0.
The metadata of this raster, represented as a nested dictionary. The first-level key is the metadata domain. The second-level contains the metadata item names and values from each domain.
To set or update a metadata item, pass the corresponding metadata item to the method using the nested structure described above. Only keys that are in the specified dictionary are updated; the rest of the metadata remains unchanged.
To remove a metadata item, use
None
as the metadata value.>>> rst = GDALRaster({'width': 10, 'height': 20, 'srid': 4326}) >>> rst.metadata {} >>> rst.metadata = {'DEFAULT': {'OWNER': 'Django', 'VERSION': '1.0'}} >>> rst.metadata {'DEFAULT': {'OWNER': 'Django', 'VERSION': '1.0'}} >>> rst.metadata = {'DEFAULT': {'OWNER': None, 'VERSION': '2.0'}} >>> rst.metadata {'DEFAULT': {'VERSION': '2.0'}}
-
vsi_buffer
¶ - New in Django 2.0:
A
bytes
representation of this raster. ReturnsNone
for rasters that are not stored in GDAL's virtual filesystem.
-
is_vsi_based
¶ - New in Django 2.0:
A boolean indicating if this raster is stored in GDAL's virtual filesystem.
-
GDALBand
¶
-
class
GDALBand
¶ GDALBand
instances are not created explicitly, but rather obtained from aGDALRaster
object, through itsbands
attribute. The GDALBands contain the actual pixel values of the raster.-
description
¶ Nama dari gambaran dari pita, jika ada.
-
width
¶ The width of the band in pixels (X-axis).
-
height
¶ The height of the band in pixels (Y-axis).
-
pixel_count
¶ The total number of pixels in this band. Is equal to
width * height
.
-
statistics
(refresh=False, approximate=False)¶ Compute statistics on the pixel values of this band. The return value is a tuple with the following structure:
(minimum, maximum, mean, standard deviation)
.If the
approximate
argument is set toTrue
, the statistics may be computed based on overviews or a subset of image tiles.If the
refresh
argument is set toTrue
, the statistics will be computed from the data directly, and the cache will be updated with the result.If a persistent cache value is found, that value is returned. For raster formats using Persistent Auxiliary Metadata (PAM) services, the statistics might be cached in an auxiliary file. In some cases this metadata might be out of sync with the pixel values or cause values from a previous call to be returned which don't reflect the value of the
approximate
argument. In such cases, use therefresh
argument to get updated values and store them in the cache.For empty bands (where all pixel values are "no data"), all statistics are returned as
None
.The statistics can also be retrieved directly by accessing the
min
,max
,mean
, andstd
properties.
-
min
¶ The minimum pixel value of the band (excluding the "no data" value).
-
max
¶ The maximum pixel value of the band (excluding the "no data" value).
-
mean
¶ The mean of all pixel values of the band (excluding the "no data" value).
-
std
¶ The standard deviation of all pixel values of the band (excluding the "no data" value).
-
nodata_value
¶ The "no data" value for a band is generally a special marker value used to mark pixels that are not valid data. Such pixels should generally not be displayed, nor contribute to analysis operations.
To delete an existing "no data" value, set this property to
None
(requires GDAL ≥ 2.1).
-
datatype
(as_string=False)¶ The data type contained in the band, as an integer constant between 0 (Unknown) and 11. If
as_string
isTrue
, the data type is returned as a string with the following possible values:GDT_Unknown
,GDT_Byte
,GDT_UInt16
,GDT_Int16
,GDT_UInt32
,GDT_Int32
,GDT_Float32
,GDT_Float64
,GDT_CInt16
,GDT_CInt32
,GDT_CFloat32
, andGDT_CFloat64
.
-
color_interp
(as_string=False)¶ - New in Django 2.0.
The color interpretation for the band, as an integer between 0and 16. If
as_string
isTrue
, the data type is returned as a string with the following possible values:GCI_Undefined
,GCI_GrayIndex
,GCI_PaletteIndex
,GCI_RedBand
,GCI_GreenBand
,GCI_BlueBand
,GCI_AlphaBand
,GCI_HueBand
,GCI_SaturationBand
,GCI_LightnessBand
,GCI_CyanBand
,GCI_MagentaBand
,GCI_YellowBand
,GCI_BlackBand
,GCI_YCbCr_YBand
,GCI_YCbCr_CbBand
, andGCI_YCbCr_CrBand
.GCI_YCbCr_CrBand
also representsGCI_Max
because both correspond to the integer 16, but onlyGCI_YCbCr_CrBand
is returned as a string.
-
data
(data=None, offset=None, size=None, shape=None)¶ The accessor to the pixel values of the
GDALBand
. Returns the complete data array if no parameters are provided. A subset of the pixel array can be requested by specifying an offset and block size as tuples.If NumPy is available, the data is returned as NumPy array. For performance reasons, it is highly recommended to use NumPy.
Data is written to the
GDALBand
if thedata
parameter is provided. The input can be of one of the following types - packed string, buffer, list, array, and NumPy array. The number of items in the input should normally correspond to the total number of pixels in the band, or to the number of pixels for a specific block of pixel values if theoffset
andsize
parameters are provided.If the number of items in the input is different from the target pixel block, the
shape
parameter must be specified. The shape is a tuple that specifies the width and height of the input data in pixels. The data is then replicated to update the pixel values of the selected block. This is useful to fill an entire band with a single value, for instance.Sebagai contoh:
>>> rst = GDALRaster({'width': 4, 'height': 4, 'srid': 4326, 'datatype': 1, 'nr_of_bands': 1}) >>> bnd = rst.bands[0] >>> bnd.data(range(16)) >>> bnd.data() array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15]], dtype=int8) >>> bnd.data(offset=(1, 1), size=(2, 2)) array([[ 5, 6], [ 9, 10]], dtype=int8) >>> bnd.data(data=[-1, -2, -3, -4], offset=(1, 1), size=(2, 2)) >>> bnd.data() array([[ 0, 1, 2, 3], [ 4, -1, -2, 7], [ 8, -3, -4, 11], [12, 13, 14, 15]], dtype=int8) >>> bnd.data(data='\x9d\xa8\xb3\xbe', offset=(1, 1), size=(2, 2)) >>> bnd.data() array([[ 0, 1, 2, 3], [ 4, -99, -88, 7], [ 8, -77, -66, 11], [ 12, 13, 14, 15]], dtype=int8) >>> bnd.data([1], shape=(1, 1)) >>> bnd.data() array([[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]], dtype=uint8) >>> bnd.data(range(4), shape=(1, 4)) array([[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]], dtype=uint8)
-
metadata
¶ - New in Django 2.0.
The metadata of this band. The functionality is identical to
GDALRaster.metadata
.
-
Membuat raster dari data¶
This section describes how to create rasters from scratch using the
ds_input
parameter.
A new raster is created when a dict
is passed to the GDALRaster
constructor. The dictionary contains defining parameters of the new raster,
such as the origin, size, or spatial reference system. The dictionary can also
contain pixel data and information about the format of the new raster. The
resulting raster can therefore be file-based or memory-based, depending on the
driver specified.
There's no standard for describing raster data in a dictionary or JSON flavor.
The definition of the dictionary input to the GDALRaster
class is
therefore specific to Django. It's inspired by the geojson format, but the
geojson
standard is currently limited to vector formats.
Examples of using the different keys when creating rasters can be found in the
documentation of the corresponding attributes and methods of the
GDALRaster
and GDALBand
classes.
Kamus ds_input
¶
Only a few keys are required in the ds_input
dictionary to create a raster:
width
, height
, and srid
. All other parameters have default values
(see the table below). The list of keys that can be passed in the ds_input
dictionary is closely related but not identical to the GDALRaster
properties. Many of the parameters are mapped directly to those properties;
the others are described below.
The following table describes all keys that can be set in the ds_input
dictionary.
Kunci | Awalan | Penggunaan |
---|---|---|
srid |
diwajibkan | Dipetakan ke atribut srid |
width |
diwajibkan | Dipetakan ke atribut width |
height |
diwajibkan | Dipetakan ke atribut height |
driver |
MEM |
Dipetakan ke atribut driver |
name |
'' |
Lihat dibawah |
origin |
0 |
Dipetakan ke atribut origin |
scale |
0 |
Dipetakan ke atribut scale |
skew |
0 |
Dipetakan ke atribut width |
bands |
[] |
Lihat dibawah |
nr_of_bands |
0 |
Lihat dibawah |
datatype |
6 |
Lihat dibawah |
papsz_options |
{} |
Lihat dibawah |
-
name
String representing the name of the raster. When creating a file-based raster, this parameter must be the file path for the new raster. If the name starts with
/vsimem/
, the raster is created in GDAL's virtual filesystem.
-
datatype
Integer representing the data type for all the bands. Defaults to
6
(Float32). All bands of a new raster are required to have the same datatype. The value mapping is:Nilai Jenis Piksel GDAL Deskripsi 1 GDT_Byte Delapan bit integer tidak bertanda 2 GDT_UInt16 Enam belas bit integer tidak bertanda 3 GDT_Int16 Enam belas bit integer bertanda 4 GDT_UInt32 Tiga-puluh-dua bit integer tidak bertanda 5 GDT_Int32 Tiga-puluh-dua bit integer bertanda 6 GDT_Float32 Thirty-two bit floating point 7 GDT_Float64 Sixty-four bit floating point
-
nr_of_bands
Integer representing the number of bands of the raster. A raster can be created without passing band data upon creation. If the number of bands isn't specified, it's automatically calculated from the length of the
bands
input. The number of bands can't be changed after creation.
-
bands
A list of
band_input
dictionaries with band input data. The resulting band indices are the same as in the list provided. The definition of the band input dictionary is given below. If band data isn't provided, the raster bands values are instantiated as an array of zeros and the "no data" value is set toNone
.
-
papsz_options
- New in Django 2.0.
A dictionary with raster creation options. The key-value pairs of the input dictionary are passed to the driver on creation of the raster.
The available options are driver-specific and are described in the documentation of each driver.
The values in the dictionary are not case-sensitive and are automatically converted to the correct string format upon creation.
The following example uses some of the options available for the GTiff driver. The result is a compressed signed byte raster with an internal tiling scheme. The internal tiles have a block size of 23 by 23:
>>> GDALRaster({ ... 'driver': 'GTiff', ... 'name': '/path/to/new/file.tif', ... 'srid': 4326, ... 'width': 255, ... 'height': 255, ... 'nr_of_bands': 1, ... 'papsz_options': { ... 'compress': 'packbits', ... 'pixeltype': 'signedbyte', ... 'tiled': 'yes', ... 'blockxsize': 23, ... 'blockysize': 23, ... } ... })
Kamus band_input
¶
The bands
key in the ds_input
dictionary is a list of band_input
dictionaries. Each band_input
dictionary can contain pixel values and the
"no data" value to be set on the bands of the new raster. The data array can
have the full size of the new raster or be smaller. For arrays that are smaller
than the full raster, the size
, shape
, and offset
keys control the
pixel values. The corresponding keys are passed to the data()
method. Their functionality is the same as setting the band data with that
method. The following table describes the keys that can be used.
Kunci | Awalan | Penggunaan |
---|---|---|
nodata_value |
None |
Dipetakan ke atribut nodata_value |
data |
Sama seperti nodata_value atau 0 |
Dilewatkan ke metode data() |
size |
(with, height) dari raster |
Dilewatkan ke metode data() |
shape |
Sama seperti ukuran | Dilewatkan ke metode data() |
offset |
(0, 0) |
Dilewatkan ke metode data() |
Using GDAL's Virtual Filesystem¶
GDAL has an internal memory-based filesystem, which allows treating blocks of
memory as files. It can be used to read and write GDALRaster
objects
to and from binary file buffers.
This is useful in web contexts where rasters might be obtained as a buffer from a remote storage or returned from a view without being written to disk.
GDALRaster
objects are created in the virtual filesystem when a
bytes
object is provided as input, or when the file path starts with
/vsimem/
.
Input provided as bytes
has to be a full binary representation of a file.
For instance:
# Read a raster as a file object from a remote source.
>>> from urllib.request import urlopen
>>> dat = urlopen('http://example.com/raster.tif').read()
# Instantiate a raster from the bytes object.
>>> rst = GDALRaster(dat)
# The name starts with /vsimem/, indicating that the raster lives in the
# virtual filesystem.
>>> rst.name
'/vsimem/da300bdb-129d-49a8-b336-e410a9428dad'
To create a new virtual file-based raster from scratch, use the ds_input
dictionary representation and provide a name
argument that starts with
/vsimem/
(for detail of the dictionary representation, see
Membuat raster dari data). For virtual file-based rasters, the
vsi_buffer
attribute returns the bytes
representation
of the raster.
Here's how to create a raster and return it as a file in an
HttpResponse
:
>>> from django.http import HttpResponse
>>> rst = GDALRaster({
... 'name': '/vsimem/temporarymemfile',
... 'driver': 'tif',
... 'width': 6, 'height': 6, 'srid': 3086,
... 'origin': [500000, 400000],
... 'scale': [100, -100],
... 'bands': [{'data': range(36), 'nodata_value': 99}]
... })
>>> HttpResponse(rast.vsi_buffer, 'image/tiff')