从头开始创建要素图层



我正在尝试使用 python arcgis 包创建 arcgis.features.FeatureLayer 的新实例。

我发现这是不可能的,我正在浏览文档和示例以及更多示例,尝试不同的东西,我发现自己在兜圈子,慢慢地我开始拉出我的听力......

这是我所拥有的:

import arcgis
import json
from arcgis.gis import GIS
g = GIS(username="my_uname", password="my_pwd")
prop = arcgis.features.FeatureCollection(dictdata={'attributes': {'foo': 'bar', 'lorem': 'ipsum'}})
prop_json=json.dumps({"featureCollection": {"layers": [dict(prop.properties)]}})
item_properties={"type": "Feature Collection", "title": "test_feature_collection_01", "text": prop_json}
it = g.content.add(item_properties=item_properties)

在这一点上 - 我不明白为什么it.layers会产生空洞的结果。我相信item_properties格式不正确,导致arcgis忽略我的图层定义......但我无处可检查它应该是什么样子。我想我想使用arcgis的东西来为我生成层定义,而不是自己手工制作 JSON,这样它就可以面向未来。

我稍后想对该项目做的是:

lr = arcgis.features.FeatureLayer.fromitem(item=it)

此操作失败,并显示TypeError: item must be a type of service, not Feature Collection

所以我想我可以发布项目并在这里使用它。

pit = it.publish()
lr = arcgis.features.FeatureLayer.fromitem(item=pit)

我需要FeatureLayer才能调用append(这样我每次有新东西想要推送时都可以抛出额外的数据(

但令我惊讶的是,我什至无法发布该项目,而且我正在Exception: Job failed.(我更大的惊喜项目实际上被发布,因为我可以通过内容管理器网站看到它(

我还尝试创建"CSV"项目类型:

import arcgis
import json
from arcgis.gis import GIS

g = GIS(username="my_uname", password="my_pwd")
item_properties={"type": "CSV", "title": "test_feature_collection_01"}
it = g.content.add(item_properties=item_properties, data="/tmp/foo.csv")
pit = it.publish()
lr = arcgis.features.FeatureLayer.fromitem(item=pit)

但是,这会导致无层项目,从而导致未处理的异常IndexError: list index out of range(因为方法arcgis调用尝试到达空的层......

请帮忙...

我设法得到了我需要的东西......但是,我仍在尝试了解如何arcgis直接从我的硬盘驱动器上传一些数据,而不是从其他项目上传一些数据。无论如何,这是代码:

import arcgis
import json
from arcgis.gis import GIS
g = GIS(username="my_uname", password="my_pwd")
item_properties_1={"type": "CSV", "title": "test_feature_collection_01"}
it_1 = g.content.add(item_properties=item_properties_1, data="/tmp/my_data.csv")
pit_1 = it_1.publish()
table = arcgis.features.Table.fromitem(item=pit_1)
# Now we want to throw some extra data, do we have to create another item to do that??
item_properties_2={"type": "CSV", "title": "test_feature_collection_02"}
it_2 = g.content.add(item_properties=item_properties_2, data="/tmp/more_data.csv")
source_info = g.content.analyze(file_path='/tmp/more_data.csv', file_type='csv', location_type='none')
# Data from `it_2` appears to be appended to `table` container (which is published item, not item itself)
table.append(item_id=it_2.id, upload_format='csv', source_info=source_info["publishParameters"], upsert=False)

因此,要将一些数据附加到现有项目,我必须创建新项目...


编辑:

要上传数据而无需创建临时项目,请使用edit_features而不是append

# create table like shown in sample above
from arcgis import features
f = features.Feature.from_dict({"attributes": {"some_attribute": "value"}})
fs = features.FeatureSet(features=[f])
table.edit_features(adds=fs)

相关内容

最新更新