LayerMapping データインポートユーティリティ¶
LayerMapping クラスは、ベクトル空間データファイル (例: shapefile) の内容を GeoDjango モデルにマッピングする方法を提供します。
このユーティリティは、ベクターレイヤーからジオメトリとフィールドを取り出し、別の座標系 (WGS84など) に変換し、GeoDjango モデルに挿入するまでのコードの繰り返しをなくしたいという作者の個人的なニーズから生まれました。
注釈
LayerMapping の使用には GDAL が必要です。
警告
GIS data sources, like shapefiles, may be very large. If you find that
LayerMapping is using too much memory, set DEBUG to
False in your settings. When DEBUG is set to True,
Django automatically logs every SQL
query -- and when SQL statements contain geometries, this may consume more
memory than is typical.
カスタマイズ例¶
シェープファイルのような、GDALがサポートするデータソースが必要です (ここでは、3つのフィーチャを持つ単純なポリゴンシェープファイル、
test_poly.shpを使用しています):
>>> from django.contrib.gis.gdal import DataSource
>>> ds = DataSource("test_poly.shp")
>>> layer = ds[0]
>>> print(layer.fields) # Exploring the fields in the layer, we only want the 'str' field.
['float', 'int', 'str']
>>> print(len(layer)) # getting the number of features in the layer (should be 3)
3
>>> print(layer.geom_type) # Should be 'Polygon'
Polygon
>>> print(layer.srs) # WGS84 in WKT
GEOGCS["GCS_WGS_1984",
DATUM["WGS_1984",
SPHEROID["WGS_1984",6378137,298.257223563]],
PRIMEM["Greenwich",0],
UNIT["Degree",0.017453292519943295]]
ここで、対応する Django モデルを定義します (必ず
migrateを使ってください):from django.contrib.gis.db import models class TestGeo(models.Model): name = models.CharField(max_length=25) # corresponds to the 'str' field poly = models.PolygonField(srid=4269) # we want our model in a different SRID def __str__(self): return "Name: %s" % self.name
LayerMappingを使用して、すべてのフィーチャを抽出し、データベースに配置します。
>>> from django.contrib.gis.utils import LayerMapping
>>> from geoapp.models import TestGeo
>>> mapping = {
... "name": "str", # The 'name' model field maps to the 'str' layer field.
... "poly": "POLYGON", # For geometry fields use OGC name.
... } # The mapping is a dictionary
>>> lm = LayerMapping(TestGeo, "test_poly.shp", mapping)
>>> lm.save(verbose=True) # Save the layermap, imports the data.
Saved: Name: 1
Saved: Name: 2
Saved: Name: 3
Here, LayerMapping transformed the three geometries from the shapefile
in their original spatial reference system (WGS84) to the spatial reference
system of the GeoDjango model (NAD83). If no spatial reference system is
defined for the layer, use the source_srs keyword with a
SpatialReference object to specify one.
LayerMapping API¶
- class LayerMapping(model, data_source, mapping, layer=0, source_srs=None, encoding=None, transaction_mode='commit_on_success', transform=True, unique=True, using='default')[ソース]¶
以下は LayerMapping オブジェクトのインスタンス生成時に使用できる引数とキーワードです。
引数 |
説明 |
|---|---|
|
地理情報モデルで、インスタンスではありません。 |
|
The path to the OGR-supported data source file
(e.g., a shapefile). Also accepts
|
|
辞書: キーはモデルフィールドに対応する文字列で、値はOGRフィーチャ用の文字列フィールド名に対応します。もしくは、モデルフィールドがジオグラフィックの場合は、OGRジオメトリの種類に対応する必要があります。例: |
キーワード引数 |
|
|---|---|
|
データソースから使用するレイヤーのインデックス (デフォルトは 0) |
|
Use this to specify the source SRS manually (for
example, some shapefiles don't come with a |
|
Specifies the character set encoding of the strings
in the OGR data source. For example, |
|
|
|
Setting this to False will disable coordinate transformations. In other words, geometries will be inserted into the database unmodified from their original state in the data source. |
|
Setting this to the name, or a tuple of names,
from the given model will create models unique
only to the given name(s). Geometries from
each feature will be added into the collection
associated with the unique model. Forces
the transaction mode to be |
|
空間データをインポートする際に使用するデータベースを設定します。デフォルトは |
save() のキーワード引数¶
- LayerMapping.save(verbose=False, fid_range=False, step=False, progress=False, silent=False, stream=sys.stdout, strict=False)[ソース]¶
The save() method also accepts keywords. These keywords are
used for controlling output logging, error handling, and for importing
specific feature ranges.
Save のキーワード引数 |
説明 |
|---|---|
|
May be set with a slice or tuple of (begin, end) feature ID's to map from the data source. In other words, this keyword enables the user to selectively import a subset range of features in the geographic data source. |
|
When this keyword is set, status information will be printed giving the number of features processed and successfully saved. By default, progress information will be printed every 1000 features processed, however, this default may be overridden by setting this keyword with an integer for the desired interval. |
|
デフォルトでは、致命的でないエラー通知は |
|
整数で設定すると、トランザクションは各ステップの間隔で発生します。たとえば、 |
|
Status information will be written to this file
handle. Defaults to using |
|
Execution of the model mapping will cease upon
the first error encountered. The default value
( |
|
設定された場合、データベース上で実行される各モデル保存の後に情報が出力されます。 |
トラブルシューティング¶
メモリ不足¶
As noted in the warning at the top of this section, Django stores all SQL
queries when DEBUG=True. Set DEBUG=False in your settings, and this
should stop excessive memory use when running LayerMapping scripts.
MySQL: max_allowed_packet エラー¶
LayerMapping とMySQLを使用する際に以下のエラーが発生した場合:
OperationalError: (1153, "Got a packet bigger than 'max_allowed_packet' bytes")
Then the solution is to increase the value of the max_allowed_packet
setting in your MySQL configuration. For example, the default value may
be something low like one megabyte -- the setting may be modified in MySQL's
configuration file (my.cnf) in the [mysqld] section:
max_allowed_packet = 10M