File size: 2,119 Bytes
84e78bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

import geopandas as gpd

def geojson_processor_to_csv(geojson_data, output_file):
    """
    Procces and Convert GeoJSON data to a csv.

    Args:
        geojson_data (str): Parsed GeoJSON data path.
        output_file (str): Name of the output CSV file.
    Returns:
        pd.DataFrame: Pandas DataFrames containing the GeoJSON features.
    """

    # split geometries from the DataFrame to reduce processing time in later steps
    geom_df = gpd.GeoDataFrame(geojson_data["geometry"], crs="EPSG:4326")
    
    # get geometry bounds
    df_bounds = geom_df['geometry'].bounds

    # create bbox column from bounds
    geom_df['bbox'] = list(zip(df_bounds['minx'], df_bounds['miny'], df_bounds['maxx'], df_bounds['maxy']))
    print('bboxes added to df.')

    # calculate geometry centroids
    geom_df['centroid'] = geom_df['geometry'].centroid
    print('centroids added to df.')
    
    # find neighbors
    geom_df['neighbors'] = None

    # Iterate through the GeoDataFrame to find neighbors
    for index, row in geom_df.iterrows():
        neighbors = []
        for other_index, other_row in geom_df.iterrows():
            if index != other_index and row['geometry'].touches(other_row['geometry']):
                neighbors.append(other_row['ID'])
        geom_df.at[index, 'neighbors'] = neighbors
    
    # save df as csv
    geom_df.to_csv(output_file + '_geom', index=False)
    print('geometry file saved')

    # assign unique string identifiers to each row based on its position in the DataFrame
    geojson_data["geometry"] = [output_file + '_geom;' + str(i) + ";0" for i in range(geojson_data.shape[0])]
    geojson_data["bbox"] = [output_file + '_geom;' + str(i) + ";1" for i in range(geojson_data.shape[0])]
    geojson_data["centroid"] = [output_file + '_geom;' + str(i) + ";2" for i in range(geojson_data.shape[0])]
    geojson_data["neighbors"] = [output_file + '_geom;' + str(i) + ";3" for i in range(geojson_data.shape[0])]
    
    # save df as csv
    geojson_data.to_csv(output_file + '_main', index=False)
    print('main file saved.')

    return 'Processing done and saved'