Hi all,
I am trying to display a FeatureSet (arcgis.features.FeatureSet) object on the ArcGIS API for Python map widget (arcgis.gis.GIS.map.add_layer) in Jupyter Notebook.
I have two FeatureSet objects:
This is the code I am using to create the feature_set object:
from arcgis.features import Feature, FeatureSet, FeatureCollection
attributes = [{'FID':1, 'some_field':'some_val', ...}, {'FID':2, 'some_field':'some_val', ...}, ...]
features_from_geom = [{"paths": [[...]]}, {"paths": [[...]]}, ...]
# len(attributes) = len(features_from_geom) = 5
features = list()
counter = len(attributes)
for idx in range(counter):
features.append(Feature(geometry=features_from_geom[idx], attributes=attributes[idx]))
# here, query_resp_from_x is just a query response from layer X, which in itself is a FeatureSet
feature_set = FeatureSet(
fields=query_resp_from_x.fields,
geometry_type=query_resp_from_x.geometry_type,
spatial_reference=query_resp_from_x.spatial_reference,
object_id_field_name=query_resp_from_x.object_id_field_name,
global_id_field_name=query_resp_from_x.global_id_field_name
)
Then, I created a map widget in the same notebook and added both the FeatureSet objects to it:
from arcgis.gis import GIS
gis_obj = GIS('http://my_portal', 'my_user')
map_obj = gis_obj.map()
map_obj.add_layer(feature_set)
map_obj.add_layer(query_resp_from_x)
map_obj
The map displayed only the query response FeatureSet (query_resp_from_x). Neither the FeatureSet I constructed (feature_set) was displayed, not an exception was raised.
I tried adding some rendering information to the FeatureSet and construct a FeatureCollection from it and display it on map, to no avail.
Can someone tell me what is wrong with this approach?
(P.S.: I converted both the FeatureSet objects (feature_set and query_resp_from_x) and compared them. They were exactly the same except for changes in values. I am attaching both the dict objects as JSON files for your reference.)
Solved! Go to Solution.
I can't identify any issue in your code, but maybe you can try mapview.draw function:
arcgis.widgets module — arcgis 1.5.0 documentation
I tried to construct a FeatureSet out of the buffer result, then successfully displayed it with the draw function. It seems you need to use WGS84 (EPSG4326) spatial reference. and here is the code snippet:
#important, project the location before buffering
_p=project([p],in_sr=sr2,out_sr=sr)[0]
fill={"type": "esriSFS", "style": "esriSFSSolid", "color": [255,0,0,255]}
buf=buffer(geometries=[_p],in_sr=sr,distances=[500],unit='Meters')
#this is the key
buf[0]['spatialReference']=sr
f=Feature(buf[0])
fset=FeatureSet([f],spatial_reference=sr)
map1.clear_graphics()
map1.draw(fset,symbol=fill)
map1.draw(p,symbol=symbol)
If you want to see the full post, here is the link.
how to draw a buffer on the map??
Please note there are better ways to do buffer and visualize the result, I was doing it just for some test at that point of time
I can't identify any issue in your code, but maybe you can try mapview.draw function:
arcgis.widgets module — arcgis 1.5.0 documentation
I tried to construct a FeatureSet out of the buffer result, then successfully displayed it with the draw function. It seems you need to use WGS84 (EPSG4326) spatial reference. and here is the code snippet:
#important, project the location before buffering
_p=project([p],in_sr=sr2,out_sr=sr)[0]
fill={"type": "esriSFS", "style": "esriSFSSolid", "color": [255,0,0,255]}
buf=buffer(geometries=[_p],in_sr=sr,distances=[500],unit='Meters')
#this is the key
buf[0]['spatialReference']=sr
f=Feature(buf[0])
fset=FeatureSet([f],spatial_reference=sr)
map1.clear_graphics()
map1.draw(fset,symbol=fill)
map1.draw(p,symbol=symbol)
If you want to see the full post, here is the link.
how to draw a buffer on the map??
Please note there are better ways to do buffer and visualize the result, I was doing it just for some test at that point of time
Hi Simo,
Thank you for the response! mapview.draw did really solve my issue, although I could do it without projecting it to 4326.
But, I still wonder why the add_layer method would not display the FeatureSet, especially when the documentation qualifies FeatureSet as a valid input.
Thanks again!