visgl / deck.gl

WebGL2 powered visualization framework
https://deck.gl
MIT License
12.13k stars 2.08k forks source link

[Doc] Documentation for accessing properties in a GeoJsonLayer lacking important information. #8385

Open KS-HTK opened 8 months ago

KS-HTK commented 8 months ago

Original Title: [Feat] pydeck.Layer support for geojson.FeatureCollection in 'GeoJsonLayer'

This issue was updated as I figured out why I was having issues with this. See the comment below.

Old Text:

Target Use Case

Passing a python-geojson geojson.FeatureCollection to pydeck directly.

gj = geojson.FeatureCollection([geojson.Feature(geojson.Point((0, 0)))])

layer = pydeck.Layer(
  type="GeoJsonLayer",
  data=gj,
)

Proposal

I think this should be supported as it is a GeoJsonLayer it should accept any valid geojson. It works if the object is a python dict that could be loaded to the geojson with geojson.loads(geojson.dumps(data)) but it does not work with the geojson object of that data.

It does however works if you do not pass the full FeatureCollection but only its features list.

gj = geojson.FeatureCollection([geojson.Feature(geojson.Point((0, 0)))])

layer = pydeck.Layer(
  type="GeoJsonLayer",
  data=gj.features,
)

Which is why I think it should also be supported to just pass the FeatureCollection.

It actually works to pass the FeatureCollection to pydeck. But it leads to errors if any of the keyword args are trying to access any data of a feature. So setting line color with get_line_color="[255, 0, properties.growth*255]" for example will break the output.

EDIT: This is more of an Issue than I realized at the time. When passing only the features array access to the properties from the styling functions works. So get_line_color and so on. But the DeckGL object cannot access the properties for use in the Tooltip function. When passing the geojson.FeatureCollection access from the tooltip function works. But you can not use the properties in get_line_color and similar keywords.

KS-HTK commented 8 months ago

Ok, so experimenting around alot lead me to figure out some stuff.

The Documentation of pydeck clearly needs updating.

On a GeoJsonLayer passing a FeatureCollection is the way to go. Where the Docs are quite unclear is on the styling part. get_line_color and get_fill_color cant access properties because pydeck seems to flatten them into the top layer of a feature. So you can not access them via properties.color even when all features have a properties.color attribute. In the Vancouver property values example get_fill_color='[255, 255, properties.growth * 255]' is used. This does not work and should be get_fill_color='[255, 255, growth * 255]'. And this is where the Documentation should be more clear. I ultimatly figured this out as I was playing around with the tooltip keyword and used the events example to get show the objects as they are passed back through the event.

I am a bit confused to why the objects are flattend in this way as it is unclear what happens if the feature has a arg called id, (which by geojson specification must be unique) and the properties contain a different id that might not be unique. Will the id of the feature be overwritten by the properties.id or not?