从头创建要素图层

huangapple go评论70阅读模式
英文:

Create FeatureLayer from scratch

问题

我正在尝试使用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)

令我惊讶的是,我甚至不能发布该项目,我收到了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调用的方法尝试获取空图层...)

请帮忙解决这个问题...

英文:

I'm trying to create new instance of arcgis.features.FeatureLayer using python arcgis package.

I find it impossible, I'm traversing through documentation and examples and more examples, trying different things and I find myself going round in circles and slowly I start pulling my hear out...

This is what I have:

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)

At this point - I can't understand why it.layers yields empty results. I believe item_properties is malformed resulting in arcgis ignoring my layers definition... but I have nowhere to check what it should look like. I figured I'd like to use something from arcgis to generate layer definition for me rather than to handcraft JSON myself so it's future-proof.

What I want to do with that item later is this:

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

This fails with TypeError: item must be a type of service, not Feature Collection

So I figured I could publish item and use it here.

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

I need FeatureLayer to be able to call append (so I can just throw extra data each time I have anything new I want to push)

But to my surprise I cannot even publish the item and I'm getting Exception: Job failed. (to my bigger surprise item actually gets published as I can see it through content manager website)

I also tried creating "CSV" item type:

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)

However this results in layer-less item, which results in unhandled exception IndexError: list index out of range (because method arcgis calls tries to get to layers which is empty...)

Please help...

答案1

得分: 1

我成功获得了某种程度的我需要的东西...但是我仍然在努力理解如何让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)

# 现在我们想添加一些额外的数据,我们是否必须创建另一个项目来做到这一点?

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')

# 似乎从`it_2`中的数据附加到`table`容器中(这是已发布的项目,而不是项目本身)

table.append(item_id=it_2.id, upload_format='csv', source_info=source_info["publishParameters"], upsert=False)

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


编辑:

要上传数据而不必创建中间项目,请改用edit_features而不是append

# 创建如上示例中所示的表格

from arcgis import features
f = features.Feature.from_dict({"attributes": {"some_attribute": "value"}})
fs = features.FeatureSet(features=[f])
table.edit_features(adds=fs)
英文:

I managed to get sort-of what I need... however I'm still trying to understand how to make arcgis to upload some data directly from my hard drive rather than from another item. Anyways here is the code:

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)

So to append some data to existing item I have to create new item...


Edit:

To upload data without having to create interim item use edit_features instead of 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)

huangapple
  • 本文由 发表于 2020年1月3日 19:59:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578252.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定