Morocco buildings
In [1]:
                Copied!
                
                
            import leafmap.foliumap as leafmap
import leafmap.foliumap as leafmap
    
        In [2]:
                Copied!
                
                
            def google_buildings_csv_to_vector(filename, output=None, **kwargs):
    """
    Convert a CSV file containing Google Buildings data to a GeoJSON vector file.
    Args:
        filename (str): The path to the input CSV file.
        output (str, optional): The path to the output GeoJSON file. If not provided, the output file will have the same
            name as the input file with the extension changed to '.geojson'.
        **kwargs: Additional keyword arguments that are passed to the `to_file` method of the GeoDataFrame.
    Returns:
        None
    """
    import os
    import pandas as pd
    import geopandas as gpd
    from shapely import wkt
    df = pd.read_csv(filename)
    # Create a geometry column from the "geometry" column in the DataFrame
    df["geometry"] = df["geometry"].apply(wkt.loads)
    # Convert the pandas DataFrame to a GeoDataFrame
    gdf = gpd.GeoDataFrame(df, geometry="geometry")
    gdf.crs = "EPSG:4326"
    if output is None:
        output = os.path.splitext(filename)[0] + ".geojson"
    gdf.to_file(output, **kwargs)
def google_buildings_csv_to_vector(filename, output=None, **kwargs):
    """
    Convert a CSV file containing Google Buildings data to a GeoJSON vector file.
    Args:
        filename (str): The path to the input CSV file.
        output (str, optional): The path to the output GeoJSON file. If not provided, the output file will have the same
            name as the input file with the extension changed to '.geojson'.
        **kwargs: Additional keyword arguments that are passed to the `to_file` method of the GeoDataFrame.
    Returns:
        None
    """
    import os
    import pandas as pd
    import geopandas as gpd
    from shapely import wkt
    df = pd.read_csv(filename)
    # Create a geometry column from the "geometry" column in the DataFrame
    df["geometry"] = df["geometry"].apply(wkt.loads)
    # Convert the pandas DataFrame to a GeoDataFrame
    gdf = gpd.GeoDataFrame(df, geometry="geometry")
    gdf.crs = "EPSG:4326"
    if output is None:
        output = os.path.splitext(filename)[0] + ".geojson"
    gdf.to_file(output, **kwargs)
    
        For example, download the Morocco Buildings Footprint impacted by the earthquake from https://data.humdata.org/dataset/openbuildings_morocco_earthquake_footprint
In [3]:
                Copied!
                
                
            url = 'https://data.humdata.org/dataset/c6059279-4521-4b39-8b18-d43aedc012c3/resource/6af2ee44-1807-4fb7-a647-a42fc7ffbff0/download/open_buildings_v3_morocco_epicenter.csv.gz'
url = 'https://data.humdata.org/dataset/c6059279-4521-4b39-8b18-d43aedc012c3/resource/6af2ee44-1807-4fb7-a647-a42fc7ffbff0/download/open_buildings_v3_morocco_epicenter.csv.gz'
    
        In [4]:
                Copied!
                
                
            filename = 'open_buildings_v3_morocco_epicenter.csv.gz'
filename = 'open_buildings_v3_morocco_epicenter.csv.gz'
    
        In [5]:
                Copied!
                
                
            # leafmap.download_file(url, filename) #1.2 GB
# leafmap.download_file(url, filename) #1.2 GB
    
        The following code cell takes 20-30 minutes to run. Please be patient.
In [6]:
                Copied!
                
                
            # google_buildings_csv_to_vector(filename, output='buildings.gpkg')
# google_buildings_csv_to_vector(filename, output='buildings.gpkg')
    
        
  
    
      Last update:
      2023-09-20