Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

95
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!