1

I'm new to python scripting in GIS and I have a list with species names, for every species in the list I want to create an empty polygon shapefile with the same fields inside the attribute. I read How to create a new Shapefile with the same attributes as an existing one? but for about 650 polygons its very difficult to get done by hand.

Ideally I would like to done this by using python.

Can anyone help me?

  • So based on the list, you want a new shapefile created, and all of the new .shp containing the same fields? Is this correct? – TsvGis Jun 11 '15 at 2:10
  • What GIS software do you have access to - QGIS, ArcGIS? – TsvGis Jun 11 '15 at 2:25
  • Why not just copy it with a WHERE clause of "FID = -9" – Vince Jun 11 '15 at 3:01
  • This sounds less like polygon creation and more like file creation. Basically you want 650 copies of the same file, all named according to your list? Or do you have one file with 650 polygons, each of a species from a list, and you want a separate file for each species (which may or may not total 650 files)? I know you said empty, but '650 polygons' is irrelevant if so. Or did you mean 650 species? – Chris W Jun 11 '15 at 4:23
  • @TsvGis Yes based on the list I want all the new shapefiles have the name according the list and the fields inside to be the same, I have access both to QGIS and ArcGis. Chris W Bassically yes I want 650 of the same file and all named according to the list. I do not have one polygon, I want to create new shapefiles one for every species. – Dimitris Kasabalis Jun 11 '15 at 7:52
2

Ok, this is do able (If I understand correctly). Here is what I am thinking. Make 1 master shapefile with all the attributes. Create a csv with your list. Use python to read each line (species name) in the csv (use a for loop), use a copy function from ArcGIS or QGIS to copy the master shapefile and use the species name as the ouput name.

The following code uses Arcgis 10+

import os
import arcpy
import csv

# Set environment settings
arcpy.env.workspace = r"D:\Working"

# Variable for the species list
SPECIES_LIST = r"D:\Working\SpeciesList.csv"

# Variable for the Master Shapefile
MASTER_SHP = r"D:\Working\Master_Shapefile.shp"

# Output location for the shapefiles
OUTPUT_DIR = r"D:\Working\Species_Shapefiles"

# Opens and reads csv list
with open(SPECIES_LIST) as speciesLst:

    reader = csv.DictReader(speciesLst)

        # Loops through species from list
        for row in reader:

        # Variable to store the species name from the name column in the csv
        species_name = row['name']

        # Output file path for the new shapefile
        out_feature_class = os.path.join(OUTPUT_DIR, "{}.shp".format(species_name))

        # Prints message to screen
        arcpy.AddMessage("....Creating {} Shapefile...".format(species_name))

        # Function to copy the master .shp
        arcpy.CopyFeatures_management(MASTER_SHP,out_feature_class)

# Prints message to screen
arcpy.AddMessage("....Finished Creating Shapefiles...")

The following images are the various stages of the process.

Set-up of working Directory Set-up of working Directory

Populating csv file Populating csv file

Script being run in the python shell Script being run in the python shell

New shapefile based on the csv list New shapefile based on the csv list

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.