prime90
prime90

Reputation: 959

For loop works fine outside of function but throws error inside of function

I have a for loop that works flawlessly outside of a function and I decided to put it inside a function so I'm not typing it out every time but it keeps throwing an error. The function does is pass a geojson file and an empty list to append.

For loop (outside of function works):

for feature in geojson['features']:
    if feature['geometry']['type'] == 'Polygon':
        points.extend(feature['geometry']['coordinates'][0])    
        points.append([None, None])   
    elif feature['geometry']['type'] == 'MultiPolygon':
        for polyg in feature['geometry']['coordinates']:
            points.extend(polyg[0])
            points.append([None, None])
    elif feature['geometry']['type'] == 'LineString': 
        points.extend(feature['geometry']['coordinates'])
        points.append([None, None])
    elif feature['geometry']['type'] == 'MultiLineString': 
        for line in feature['geometry']['coordinates']:
            points.extend(line)
            points.append([None, None])
    else: pass

Function:

import json

geojson = json.load(open("towns.geojson"))

points= []

def coordinate_extract(geojson_file, empty_list):
    for feature in geojson_file['features']:
        if feature['geometry']['type'] == 'Polygon':
            empty_list.extend(feature['geometry']['coordinates'][0])    
            empty_list.append([None, None]) # mark the end of a polygon   
        elif feature['geometry']['type'] == 'MultiPolygon':
            for polyg in feature['geometry']['coordinates']:
                empty_list.extend(polyg[0])
                empty_list.append([None, None]) #end of polygon
        elif feature['geometry']['type'] == 'LineString': 
            empty_list.extend(feature['geometry']['coordinates'])
            empty_list.append([None, None])
        elif feature['geometry']['type'] == 'MultiLineString': 
            for line in feature['geometry']['coordinates']:
                empty_list.extend(line)
                empty_list.append([None, None])
        else: pass   
    print(empty_list)

coordinate_extract('geojson', 'points')

TypeError: string indices must be integers

I don't understand why I'm getting this error on line 8 inside of the function but not outside. Any suggestions?

Upvotes: 0

Views: 51

Answers (2)

alex067
alex067

Reputation: 3301

coordinate_extract('geojson', 'points')

You're mistakingly passing in a string, instead of the actual object geojson that contains your json data.

So your for loop is trying to access an index in a string, which works but a string only contains numerical indices.

Just remove the quotations and pass in geojson & the points array as an object and list respectively, not as a string.

Upvotes: 1

High-Octane
High-Octane

Reputation: 1112

Don't use quotations on your variables while calling the function.

It is considering the inputs as a string, where you are expecting variables.

coordinate_extract(geojson, points)

You sir, definitely need to start from basics

Upvotes: 1

Related Questions