Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

97
Vistas
need to export individual features to shapefiles using geopandas

I'm using geopandas to read a geojson file and output shapefiles. The issue is I cannot figure out how to export single features within that shapefile - only the entire shapefile. Just for ref, I'm using google colab.

here's what I have so far

os.makedirs('/content/drive/MyDrive/shapes')
gdf = gpd.read_file('/content/sample_data/countries.geojson')
for num, row in gdf.iterrows():
    key = row.city
    fileName = key+".shp"
    path = '/content/drive/MyDrive/shapes/'+fileName
    os.makedirs('/content/drive/MyDrive/shapes/'+fileName)
    os.chdir('/content/drive/MyDrive/shapes/'+fileName)
    gdf.to_file(fileName) # need to do something like row to file here

this code will export a bunch of shapefiles of the original geojson file & name them by a certain key. I can't figure out how to loop through the individual features and make a shapefile for each.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Since I didn't have your shapefile, I am going to answer your question based on what you have told in your question. First of all, you should not save the gdf dataframe every time because you want to save the row into a file(I think each row represents a city and you want to have this city in a different shapefile that is named after the city name). So, what I suggest is:

os.makedirs('/content/drive/MyDrive/shapes')
gdf = gpd.read_file('/content/sample_data/countries.geojson')
for num, row in gdf.iterrows():
    key = row.city
    fileName = key+".shp"
    path = '/content/drive/MyDrive/shapes/'+fileName
    os.makedirs(path)
    os.chdir('/content/drive/MyDrive/shapes/'+fileName)
    gdf.iloc[num:num+1,:].to_file(path)

Note that, instead of saving in filename I saved the file into path because if it wasn't the case, the last few lines in your code would be for nothing!

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda