I have Python script that needs to pull the ESPG code from a shapefile, when I run this in Intellij IDEA 2021.1.2 (using a Anaconda Environment for Python) it works correctly, but when it is run as a batch file it is missing significant bits of the SRS Info.
Python Script
import os
import sys
from osgeo import ogr
from Crypto.Hash import MD5
from osgeo import gdal
import pkg_resources
daShapefile = r"C:\Users\bradl\Dropbox\Projects\SpatialDays\PythonFunctions\Inputs\OSM_Download_TEST\Fiji_3460.shp"
def get_md5(file_path):
"""
This function generates a MD5 value for the provide file,
:param file_path: Path to File
:return:
"""
print("\nCalulating MD5")
chunk_size = 65536
h = MD5.new()
with open(file_path, 'rb') as f:
while True:
chunk = f.read(chunk_size)
if len(chunk):
h.update(chunk)
else:
break
return h.hexdigest()
def md5_checker(source_md5, download_md5):
"""
This function compares to the stated MD5 value of a file to that generate from the actually downloaded file.
:param source_md5: MD5 Value from Authoritative Source
:param download_md5: Local generated MD5 Value
:return:
"""
# Finally compare original MD5 with freshly calculated
if source_md5 == download_md5:
print("MD5 verified:\n\tSource MD5: {}\n\tDownload MD5: {}".format(source_md5, download_md5))
return True
else:
print("MD5 verification failed!\n\tSource MD5 : {}\n\tDownload MD5: {}".format(source_md5, download_md5))
return False
print("Python: {}".format(sys.version))
try:
user_paths = os.environ['PYTHONPATH'].split(os.pathsep)
print("\tPython Path: {}".format(user_paths))
except KeyError:
user_paths = []
print("\tPython Path: NOT FOUND")
print("GDAL Version Number: {}".format(pkg_resources.get_distribution('gdal').version))
shp_md5 = get_md5(daShapefile)
check_shapefile_md5 = md5_checker(source_md5='bcf1de64030183d1b70e562786cc7a57',
download_md5=shp_md5)
if check_shapefile_md5 is True:
print("\tShapefile is Correct")
else:
print("\tShapefile is Incorrect. \n\t\t MD5: {}".format(shp_md5))
driver = ogr.GetDriverByName('ESRI Shapefile')
dataset = driver.Open(daShapefile)
driver = ogr.GetDriverByName('ESRI Shapefile')
dataSource = driver.Open(daShapefile, 0) # 0 means read-only. 1 means writeable.
# Check to see if shapefile is found.
if dataSource is None:
print('Could not open %s' % (daShapefile))
else:
print('Opened %s' % (daShapefile))
layer = dataSource.GetLayer()
featureCount = layer.GetFeatureCount()
print("Number of features in %s: %d" % (os.path.basename(daShapefile),featureCount))
spatial_ref = layer.GetSpatialRef()
print("SRS: {}".format(spatial_ref))
epsg = spatial_ref.GetAttrValue("AUTHORITY", 1)
Batch File
@echo off
"C:\Users\bradl\.conda\envs\SD_Local_Dev\python.exe" "C:/Users/bradl/Dropbox/Projects/SpatialDays/PythonFunctions/Scratchpad.py"
pause
Intellij Terminal Output
C:\Users\bradl\.conda\envs\SD_Local_Dev\python.exe C:/Users/bradl/Dropbox/Projects/SpatialDays/PythonFunctions/Scratchpad.py
Python: 3.7.6 (default, Jan 8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]
Python Path: ['C:\\Users\\bradl\\Dropbox\\Projects\\SpatialDays\\PythonFunctions']
GDAL Version Number: 2.3.3
Calulating MD5
MD5 verified:
Source MD5: bcf1de64030183d1b70e562786cc7a57
Download MD5: bcf1de64030183d1b70e562786cc7a57
Shapefile is Correct
Opened C:\Users\bradl\Dropbox\Projects\SpatialDays\PythonFunctions\Inputs\OSM_Download_TEST\Fiji_3460.shp
Number of features in Fiji_3460.shp: 1
SRS: PROJCS["Fiji 1986 / Fiji Map Grid",
GEOGCS["Fiji 1986",
DATUM["Fiji_Geodetic_Datum_1986",
SPHEROID["WGS 72",6378135,298.26,
AUTHORITY["EPSG","7043"]],
TOWGS84[0,0,4.5,0,0,0.554,0.2263],
AUTHORITY["EPSG","6720"]],
PRIMEM["Greenwich",0,
AUTHORITY["EPSG","8901"]],
UNIT["degree",0.0174532925199433,
AUTHORITY["EPSG","9122"]],
AUTHORITY["EPSG","4720"]],
PROJECTION["Transverse_Mercator"],
PARAMETER["latitude_of_origin",-17],
PARAMETER["central_meridian",178.75],
PARAMETER["scale_factor",0.99985],
PARAMETER["false_easting",2000000],
PARAMETER["false_northing",4000000],
UNIT["metre",1,
AUTHORITY["EPSG","9001"]],
AXIS["Easting",EAST],
AXIS["Northing",NORTH],
AUTHORITY["EPSG","3460"]]
Batch file (C:\windows\system32\cmd.exe) Terminal Output
Python: 3.7.6 (default, Jan 8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]
Python Path: NOT FOUND
GDAL Version Number: 2.3.3
Calulating MD5
MD5 verified:
Source MD5: bcf1de64030183d1b70e562786cc7a57
Download MD5: bcf1de64030183d1b70e562786cc7a57
Shapefile is Correct
Opened C:\Users\bradl\Dropbox\Projects\SpatialDays\PythonFunctions\Inputs\OSM_Download_TEST\Fiji_3460.shp
Number of features in Fiji_3460.shp: 1
SRS: PROJCS["Fiji_1986_Fiji_Map_Grid",
GEOGCS["GCS_Fiji_1986",
DATUM["Fiji_1986",
SPHEROID["WGS_72",6378135.0,298.26]],
PRIMEM["Greenwich",0.0],
UNIT["Degree",0.0174532925199433]],
PROJECTION["Transverse_Mercator"],
PARAMETER["False_Easting",2000000.0],
PARAMETER["False_Northing",4000000.0],
PARAMETER["Central_Meridian",178.75],
PARAMETER["Scale_Factor",0.99985],
PARAMETER["Latitude_Of_Origin",-17.0],
UNIT["Meter",1.0]]
Press any key to continue . . .
.