2015-11-12 38 views
0

如何正確將shapefile轉換爲MultiPolygon?我得到試圖把shape文件成的MultiPolygon時生成以下錯誤...Python在參數中遇到的無效類型

.401 395951.4, 238087.5 395958.5, 238086.5 395963, 238086.3 395968.2, 238087.3 395973, 238090.7 395980.6, 238094.2 395989.2, 238101 395993.2, 238107.7 395994.1, 238114 395993.8, 238121.3 395992.6, 238131.6 395988.6, 238146.9 395978.2, 238184.401 395960.9, 238196.6 395958.4, 238203.1 395956.801, 238214.599 395953.4))) 
Traceback (most recent call last): 
    File "geo.py", line 25, in <module> 
    Multi = MultiPolygon([pol['geometry'] for pol in fiona.open(file)]) 
    File "/Users/user/Documents/workspace/env/lib/python3.4/site-packages/django/contrib/gis/geos/collections.py", line 38, in __init__ 
    self._check_allowed(init_geoms) 
    File "/Users/user/Documents/workspace/env/lib/python3.4/site-packages/django/contrib/gis/geos/mutable_list.py", line 261, in _check_allowed 
    raise TypeError('Invalid type encountered in the arguments.') 
file = 'NUTS1_DEC_2008_EW_BFE.shp' 

代碼錯誤:

from shapely.geometry import shape 
c = fiona.open(file) 
pol = c.next() 
geom = shape(pol['geometry']) 

Multi = MultiPolygon([pol['geometry'] for pol in fiona.open(file)]) 

回答

1

MultiPolygon對象從GeometryCollection繼承。
展望GeometryCollection.__ini__()方法你可以看到有一個調用self._check_allowed()方法。 self._check_allowed()來自GEOSGeometryListMixin類,這正是_check_allowed() ID defined

... 
def _check_allowed(self, items): 
    if hasattr(self, '_allowed'): 
     if False in [isinstance(val, self._allowed) for val in items]: 
      raise TypeError('Invalid type encountered in the arguments.') 
... 

你怎麼能注意到,上述方法檢查,如果每個項目是下列任何類型的實例:

Point, LineString, LinearRing, Polygon, MultiPoint, MultiLineString, MultiPolygon 

它可以在這裏找到,django/django/contrib/gis/geos/collections.py,行#142

# Setting the allowed types here since GeometryCollection is defined before 
# its subclasses. 
GeometryCollection._allowed = (Point, LineString, LinearRing, Polygon, 
           MultiPoint, MultiLineString, MultiPolygon) 

也就是說,可能fiona.open()不會返回任何有效的允許類型。 我不是GeoDjango的專家,但使用django.contrib.gis.utils.LayerMapping打擊了我。 閱讀從GeoDjango內置文檔的tutoria似乎相當簡單:GeoDjango Tutorial

+0

那就是我剛剛嘗試的(參見上面的更新) – Prometheus

1

我認爲你需要.read()上的文件,對不對?

Multi = MultiPolygon([pol['geometry'] for pol in fiona.open(file).read()]) 
+0

我不這麼認爲,這個工程到一個點,然後朝着結束時產生的錯誤:''用於POL在fiona.open(文件):'' – Prometheus

+0

也許有一個空行或空字符? –

相關問題