从头开始创建 FeatureLayer
Create FeatureLayer from scratch
我正在尝试使用 python arcgis 包创建 arcgis.features.FeatureLayer 的新实例。
我发现这是不可能的,我正在遍历 documentation and examples and more examples,尝试不同的事情,我发现自己在原地打转,慢慢地我开始拉出我的耳朵...
这是我的:
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)
我正在尝试使用 python arcgis 包创建 arcgis.features.FeatureLayer 的新实例。
我发现这是不可能的,我正在遍历 documentation and examples and more examples,尝试不同的事情,我发现自己在原地打转,慢慢地我开始拉出我的耳朵...
这是我的:
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)