dash-docs icon indicating copy to clipboard operation
dash-docs copied to clipboard

Choropleth Map not Working Python

Open tony-zeidan opened this issue 3 years ago • 1 comments

Hi there,

My name is Tony Zeidan and I am currently using the Plotly express API to make a choropleth map of Canada based on a hexagonal grid which is obtained from the Uber H3 (h3-py) library. I have managed to get the right resulting hexagonal grid and made a geo json from it. When I use the Choropleth Mapbox method, I can only do it in Mercator projection and the result is as follows:

    dig = (px.choropleth_mapbox(
                    merged, 
                    geojson=geojson_obj, 
                    locations='h3_cell', 
                    color='count',
                    color_continuous_scale="Viridis",
                    range_color=(0,merged['count'].mean()),mapbox_style='carto-positron',
                    zoom=7,
                    center = {"lat": 65.469211, "lon": -136.713865},
                    opacity=0.7,
                    labels={'count':'# of fire ignitions '}))

The resulting map returns a correct result (a map with color coded hexagons based on count) but I need it in the Orthographic projection so I decided to use the plotly express Choropleth method instead.

mapbox_method

    fig = px.choropleth(merged, geojson=geojson_obj, locations='h3_cell', color='count',
                           color_continuous_scale="Magma",
                           range_color=(0,merged['count'].mean()),
                           scope="north america",
                           center = {"lat": 65.469211, "lon": -136.713865},
                           labels={'count':'# of fire ignitions'}
                          )
    fig.update_geos(lataxis_showgrid=True, lonaxis_showgrid=True, projection_type='orthographic')

This results in an image of the globe being color coded one color, although when I hover over Canada it shows the correct hexagon cell ids (so they are present). Any advice would be helpful.

express_method

tony-zeidan avatar Mar 04 '21 15:03 tony-zeidan

I believe it may be the formatting of the GeoJSON file I am using, if anyone can give me a tip that would be greatly appreciated. Here is a sample GeoJSON from my project, and the code used to generate it.


    geojson_obj = (hexagons_dataframe_to_geojson
                (merged,
                 hex_id_field='h3_cell',
                 value_field='count',
                 geometry_field='geometry'))

  def hexagons_dataframe_to_geojson(df_hex, hex_id_field,geometry_field, value_field,file_output = None):
  
      list_features = []
  
      for i, row in df_hex.iterrows():
          feature = Feature(geometry = row[geometry_field],
                            id = row[hex_id_field],
                            properties = {"value": row[value_field]})
          list_features.append(feature)
  
      feat_collection = FeatureCollection(list_features)
  
      if file_output is not None:
          with open(file_output, "w") as f:
              json.dump(feat_collection, f)
  
      else :
        return feat_collection

map.txt

I have seen some issues like this and it appears to be the standard of the GeoJSON file that is the problem, any help would be appreciated.

tony-zeidan avatar Mar 04 '21 16:03 tony-zeidan