Skip to content

Instantly share code, notes, and snippets.

@RobertoTjesse
Last active March 11, 2019 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobertoTjesse/8876f112e767e14544175ffdd31c5e1a to your computer and use it in GitHub Desktop.
Save RobertoTjesse/8876f112e767e14544175ffdd31c5e1a to your computer and use it in GitHub Desktop.
Add a projection file to all the shapefiles inside a folder
import os, urllib.request, urllib.parse, urllib.error
def getWKT_PRJ (epsg_code):
# access projection information
wkt = urllib.request.urlopen("http://spatialreference.org/ref/epsg/{0}/prettywkt/".format(epsg_code))
decoded = (wkt.read().decode('utf-8'))
# remove spaces between charachters
remove_spaces = decoded.replace(" ","")
# place all the text on one line
output = remove_spaces.replace("\n","")
return output
def referencer(folder_path, extension):
for path, subdirs, files in os.walk(folder_path):
for name in files:
file_extension = os.path.splitext(name)[-1]
if(extension in file_extension):
#if(file_extension.lower() in name.lower()):
file_path = os.path.join(path,name)
file_name = os.path.splitext(file_path)[0]
print(file_path)
print(file_name)
prj = file_name + ".prj"
print(prj)
projection = open(prj,"w")
projection.write(epsg)
projection.close()
epsg = getWKT_PRJ("25831")
referencer('E:\Testfolder', '.shp')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment