Table of Contents
Introduction
A .prj file contains the coordinate system information for a dataset and is required for ‘on the fly’ projection by GIS software. The file itself is a text file containing information in Well Known Text (WKT) format. The code snippet here shows you how to generate a .prj file, using Python, for your data if the .prj file is missing.
You will need to know what coordinate system the data is in and the EPSG code. For example, to find the EPSG code for WGS84 use a search engine and search for EPSG WGS84. The first site returned is usually http://spatialreference.org/. The EPSG code for WGS84 is 4326.
This course is designed to instill the basics of Python Programming by incrementally increasing your knowledge session-upon-session. In each section you will be given new material for a workbook to fill out and by the end of this course you will have your very own Python reference handbook. So how does this course have a GIS focus? Simple, most elements of the course have GIS and geospatial data in mind. Instead of using non-descript variables and values, we will use terms such as population, city, x_coord, y_coord, and so on. This will aid participants with pinpointing how they can relate geospatial data to Python.
Using Python to Convert from Decimal Degrees to Degrees-Minutes-Seconds
The function below grabs information from spatialreference.org and returns the wkt representation for the EPSg code provided.
## function to generate .prj file information using spatialreference.org
def getWKT_PRJ(epsg_code):
## import required
import urllib.request
## access projection information
response = urllib.request.urlopen("http://spatialreference.org/ref/epsg/{0}/prettywkt/".format(epsg_code))
## decode the response from bytes to string
wkt = response.read().decode('utf-8')
## return with no linebreaks or spaces
return wkt.replace("\n", "").replace(" ", "")
The Conversion in Action
## create the .prj file
with open("filename.prj", "w") as prj:
## call the function and supply the epsg code
epsg_info = getWKT_PRJ("4326")
prj.write(epsg_info)
If you open the .prj file in a text editor you will see the text below.
GEOGCS["WGS84",DATUM["WGS_1984",SPHEROID["WGS84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]
GEOGCS at the beginning of the WKT indicates that EPSG: 4326 is a geographic coordinate reference system (it would be PROJCS if it was a projected coordinate system). The information within the square brackets after DATUM provide the information for the parameters of the datum. We see the name of the datum, ‘WGS_1984’, and the SPHEROID information with a semimajor axis of 6,378,137 m with an inverse-flattening ratio of 298.257223563. The PRIMEM data tells us that it uses Greenwich as the prime meridian (where longitude is zero). UNIT specifies the measurement unit of the coordinate system, here it is ‘degree’, and the 0.0174532925199433 value is required to convert from radians into the stated units.
Open your data in a GIS and make sure that it is displaying correctly in relation to other features.
At Final Draft Mapping we provide comprehensive courses for automating tasks within ArcGIS Pro and ArcGIS Online with ArcPy and the ArcGIS API for Python. Courses range from beginner to advanced workflows and all paid courses provide extra support where you can ask questions. Automation within ArcGIS is a highly sought after skill, by adding these skills to your arsenal you are placing yourself at the forefront of that demand.
We appreciate our blog readers, you can get 25% off any (non-sale) course at any time with the code FDMBLOG25