Search code examples
pythonjsonlistrevit-apisublist

getting the x values of points from a sublist in python


I have a JSON file and the file is a nested list like A. (Sorry- it was a dictionary)

In coordinates where the first item (1.1) holds the the x and second item (2.2) holds the the y and the third item (3.3) holds the z coordinates for a point. And the other 3 point following first point is the points of a building footprint list. So, the 4 point in the list gives the building footprint curve when the lines created.

In the second data you can see in bold ,there are 2 sublists. That means building footprint data have 2 the polygon.

A = 

{
"type": "FeatureCollection",
"name": "PROJECT CRS TM 30",
"features": [

{ "type": "Feature", "properties": { "OBJECTID": 0, "ROOF_NUMBER": null, "STOREY": null }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 1.1, 2.2, 3.3 ], [ 4.4, 5.5,6.6 ], [ 7.7, 8.8, 9.9 ], [ 10.10, 11.11, 12.12 ] ] ] ] } },

{ "type": "Feature", "properties": { "OBJECTID": 2215889.0, "ROOF_NUMBER": null, "STOREY": 4 }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 13.13, 14.14, 15.15], [ 16.16, 17.17, 18.18 ], [ 19.19, 20.20, 21.21 ], **[ [ [ 22.22, 23.23, 24.24 ], [ 25.25, 26.26, 27.27 ], [ 28.28, 29.29, 30.30 ], [ 31.31, 32.32, 33.33 ] ] ] ] } }**,

]
}

I'd like to obtain a list that consist x values of the points and I'd like to get them while not changing the buildings sublist. Such as B:

B = [[[[1.1], [4.4], [7.7], [10.10]]],[[[13.13], [16.16], [19.19]],[[22.22], [25.25], [28.28], [31.31]]]]    

where each list represents the x value of the points. and each feature has their own sublists.

I get a list like C when I write this code in BIM visual programming software.

get_data_values["features"]["geometry"]["coordinates"][0];

C= [ [ [ 13.13, 14.14, 15.15], [ 16.16, 17.17, 18.18 ], [ 19.19, 20.20, 21.21 ] ] , **[ [ 22.22, 23.23, 24.24 ], [ 25.25, 26.26, 27.27 ], [ 28.28, 29.29, 30.30 ], [ 31.31, 32.32, 33.33 ] ] ** ]

After getting C I used the code below to get B List.


    def Extract(lst):
        second_items = []
        for sublst in lst:
            for secsublst in sublst:     
                for thirdsublst in secsublst:
                    second_item = []
                    for item in thirdsublst:
                        second_items.append(second_item)
                        if item:
                            second_item.append(item[1])
                                      
        return second_items

So, when I try this, I get x values of each feature but I realized there are 9835 feature in the data but in the end I only get 84296 feature

The JSON file has 9835 feature which means there are 9835 buildings info is located. And in some features there are 2 different footprint polygons. [actual total feature number 9835]

The number I get is 84296 when I use extract def. That means I get less number of building.

[the whole number is 9835] [the number I get 84296]

How can I get 9835 feature like B? I have searched the forum, but couldn't solve my problem since I am a beginner.

Thanks for your time and guidence.

The whole process seem like this. in a BIM software dictionary elements can be taken like this


Solution

  • You can use a recursive function to walk through and replicate the nested list structure and only populate the new structure with the first value.

    def flatten_coords(el):
        if el == []:
            return el
        if isinstance(el[0], list):
            return [flatten_coords(el[0])] + flatten_coords(el[1:])
        return el[:1]
    
    
    x_coords = []
    
    for feature in A["features"]:
        x_coords.append(flatten_coords(feature["geometry"]["coordinates"]))
    
    print(x_coords)
    

    Output:

    [[[[[1.1], [4.4], [7.7], [10.1]]]], [[[[13.13], [16.16], [19.19], [[[22.22], [25.25], [28.28], [31.31]]]]]]]