0

Aim

For an assignment I need to put some building information (geometries and their properties) into a GeoJSON data structure.

Approach

My general approach is as follows: create an empty dictionary with the necessary GeoJSON data structure and .append the data to this structure (see: below)

output_buildings = {
    'type': "FeatureCollection",
    'features': [
    {
        'type': "Feature",
        'geometry': {
            'type': ,
            'coordinates': 
        },
        'properties': {
            'building_id': ,
            'building_year': ,
            'building_status': ,
            'building_occurance': 
        }
        }
    ]
}

Issue

I know how to create a simple empty dictionary, but my problem is that I don't know how to create a key structure without values (as I want to append those values later on). I receive an error at type (the second one within features) at the ,.

Previous efforts

  • I found this topic on StackOverflow: Method for Creating a Nested Dictionary from a List of Keys; but it doesn't fit my purpose.
  • I searched in the python docs with terms like "create empty nested dictionary", "create dictionary with empty values", but didn't find what I was looking for.
  • I tried to use placeholders (%s / %d), but they are not suitable for this purpose
  • Finally I tried to use pass (didn't work).

Question

I haven't found a solution yet. Can you please provide me with some suggestions?

Thanks in advance!

Graven
  • 63
  • 7
  • 3
    Have you considered using `None` as placeholders? – nisemonoxide Feb 25 '18 at 10:43
  • No such thing as having a key without a value in a dictionary, nor an empty variable. They're matched pairs. You can use placeholders, such as `None`, `float('NaN')` or a new object. The closest thing we have is having an uninitialized object attribute slot. – Yann Vernier Feb 25 '18 at 10:57
  • No, didn't consider `None`. I was looking for something like that. Can `None` be overwritten? – Graven Feb 25 '18 at 12:09

1 Answers1

1

Your current dictionary structure is invalid, since you must have key-value pairs existent.

You can get around this by filling in None placeholders for keys that don't have a designated value. The key coordinates can be set to an empty list however, since you can have multiple coordinates.

Valid GeoJSON structure:

output_buildings = {
    'type': "FeatureCollection",
    'features': [
        {
            'type': "Feature",
            'geometry': {
                'type': None,
                'coordinates': []
            },
            'properties': {
                'building_id': None,
                'building_year': None,
                'building_status': None,
                'building_occurance': None
            }
        }
    ]
}
RoadRunner
  • 25,803
  • 6
  • 42
  • 75