0

I am trying to draw borders in folium in python by using a geojson but I have an error. What is wrong?

import folium
from folium import plugins
  .
  .
bordersStyle={
color': 'black',
weight': 2,
fillColor': 'blue',
fillOpacity': 0.2
} 

folium.GeoJson(
           data=(open("all_geojson.json", 'r').read()),
           name="borders",
           style_function=lambda x:bordersStyle).add_to(base_map)

folium.LayerControl().add_to(base_map)

base_map.save('land3.html')

When I run, I have below error

if self.data['type'] == 'FeatureCollection':
TypeError: list indices must be integers or slices, not str

And an example from my geojson file:

geojson = {
      "type": 'FeatureCollection',
      "features": [
          {
              "type": "feature",
              "geometry": {
                  "type": "Polygon",
                  "coordinates": '%s' % coordinates #getting the coordinates from a code
              }
          }
          ],
      "properties": [
          {
              "landno": '%s' % no, #getting the number from a code
              "city": 'Paris',
              "area": '32x32'
          }
       ]
     }

 
  • error shows you line with code - so first you could use `print()` and `print(type())` to see what you have in variables in this line. It is called `"print debuging"` and it can help to see what is the problem – furas Mar 06 '22 at 00:57
  • you try to get `['type']` from `self.data` but it seems it is NOT `dictionary` but `list` and it needs `[0]`, `[1]`, etc. So first check what you have in `self.data` . We can't run your code so we can't check it, and we can't help more. – furas Mar 06 '22 at 00:59
  • I see one line which can be your problem. You run `data=(open("all_geojson.json", 'r').read())` - you read it as text file and it may treats it as one string with text. If it really have to be `JSON` data then maybe you should convert string to Python structure `data = json.loads( open("all_geojson.json", 'r').read() )` – furas Mar 06 '22 at 01:02
  • I also don't like your geojson file. If it is JSON file then it shouldn't have `geojson = ` and `'%s' % coordinates` and `'%s' % no` because JSON is NOT Python - it can't have code to generate values. This looks like Python code. But problem can make `'%s' % coordinates` because it would need to set value in `coordinates` before importing this code. – furas Mar 06 '22 at 01:04
  • You could keep it in text file without `geojson = `, `"coordinates": '%s' % coordinates` and `"landno": '%s' % no` and read it as `json.loads(...)` to get Python structure and later add missing `"coordinates"` and `"landno"`. OR you could treat it as `template` with `%s` or `{}` and use string formatting `"text {} other {}".format(coordinates, no)` – furas Mar 06 '22 at 01:12
  • Thank you for comments. Actually the geojson example is from my code to create json file. Associated area is in the original json file is like that: "coordinates":[[14, 14],[16, 14],[16,16],[14,16],[14,14]]. – bandit_hilmi Mar 06 '22 at 01:32
  • I still don't understand problem. I don't see FULL error message, and I can't run it to see what is wrong. At this moment we don't have idea what is the problem. – furas Mar 06 '22 at 03:47

0 Answers0