[1056] Create a GeoDataFrame in GeoPandas with a list of data and geometry

alex_bn_lee / 2024-09-05 / 原文

To create a GeoDataFrame in GeoPandas with a list of data and geometry, you can follow these steps:

  1. Install GeoPandas (if you haven’t already):

    pip install geopandas
  2. Import the necessary libraries:

    import geopandas as gpd
    from shapely.geometry import Point
  3. Prepare your data and geometry:

    # Example data
    data = {
        'name': ['Location1', 'Location2', 'Location3'],
        'value': [10, 20, 30]
    }
    
    # Example geometry (list of Point objects)
    geometry = [Point(1, 1), Point(2, 2), Point(3, 3)]
  4. Create the GeoDataFrame:

    # Create a GeoDataFrame
    gdf = gpd.GeoDataFrame(data, geometry=geometry)
    
    # Display the GeoDataFrame
    print(gdf)

Here’s the complete code snippet:

import geopandas as gpd
from shapely.geometry import Point

# Example data
data = {
    'name': ['Location1', 'Location2', 'Location3'],
    'value': [10, 20, 30]
}

# Example geometry (list of Point objects)
geometry = [Point(1, 1), Point(2, 2), Point(3, 3)]

# Create a GeoDataFrame
gdf = gpd.GeoDataFrame(data, geometry=geometry)

# Display the GeoDataFrame
print(gdf)

Explanation:

  1. Import Libraries: Import geopandas for creating GeoDataFrames and shapely.geometry.Point for creating point geometries.
  2. Prepare Data: Create a dictionary with your data. In this example, we have a list of names and values.
  3. Prepare Geometry: Create a list of Point objects representing the geometries.
  4. Create GeoDataFrame: Use gpd.GeoDataFrame to create the GeoDataFrame, passing the data and geometry.

This will create a GeoDataFrame with your data and corresponding geometries.

Would you like to know more about working with GeoDataFrames or any other specific functionality?