[GIS] How to create empty polygon shapefiles with the same field names using python

polygon-creationpyqgispython

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?

Best Answer

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.