-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEL_Extract.py
202 lines (169 loc) · 7.67 KB
/
EL_Extract.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#--------------------------------------------------------------------------------------------------------#
# import packages and modules
# import packages
import urllib
import json
import requests
import os
import shutil
import sys
import logging
from datetime import datetime
import time
from zipfile import ZipFile
from io import BytesIO
import arcpy
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import pathlib
from time import strftime
# environment settings
arcpy.env.workspace = "F:/GIS/PARCELUPDATE/Workspace/ParcelStaging.gdb"
arcpy.env.overwriteOutput = True
arcpy.env.outputCoordinateSystem = arcpy.SpatialReference(26910)
# set workspace and sde connections
workspace = "F:/GIS/PARCELUPDATE/Workspace/Staging"
# network path to connection files
filePath = "F:/GIS/PARCELUPDATE/Workspace/"
# portal signin
## TRPA_ADMIN credentials
# portal_user = "TRPA_PORTAL_ADMIN"
# portal_pwd = str(os.environ.get('Password'))
# portal_url = "https://maps.trpa.org/portal/"
# # sign in
# arcpy.SignInToPortal(portal_url, portal_user, portal_pwd)
# Parcel AOI to select parcels to keep (includes TRPA Boundary and Olympic Valley Watershed)
parcelAOI = "Parcel_AOI"
FIRSTstartTimer = datetime.datetime.now()
# Create and open log file.
complete_txt_path = os.path.join(workspace, "CountyParcel_Extract_Log.txt")
print (complete_txt_path)
log = open(complete_txt_path, "w")
# Write results to txt file
log.write("Log: " + str(FIRSTstartTimer) + "\n")
log.write("\n")
log.write("Begin process:\n")
log.write("Process started at: " + str(FIRSTstartTimer) + "\n")
log.write("\n")
#---------------------------------------------------------------------------------------#
## GET DATA
#---------------------------------------------------------------------------------------#
# start timer for the get data requests
startTimer = datetime.datetime.now()
#---------------------------------------------------------------------------------------#
## Define Functions ##
#---------------------------------------------------------------------------------------#
##--------------------------------------------------------------------------------------------------------#
## SEND EMAIL WITH LOG FILE ##
##--------------------------------------------------------------------------------------------------------#
# path to text file
fileToSend = complete_txt_path
# email parameters
subject = "County Parcel Extract Log File"
sender_email = "infosys@trpa.org"
# password = ''
receiver_email = "gis@trpa.gov"
def send_mail(body):
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = sender_email
msg['To'] = receiver_email
msgText = MIMEText('%s<br><br>Cheers,<br>GIS Team' % (body), 'html')
msg.attach(msgText)
attachment = MIMEText(open(fileToSend).read())
attachment.add_header("Content-Disposition", "attachment", filename = os.path.basename(fileToSend))
msg.attach(attachment)
try:
with smtplib.SMTP("mail.smtp2go.com", 25) as smtpObj:
smtpObj.ehlo()
smtpObj.starttls()
# smtpObj.login(sender_email, password)
smtpObj.sendmail(sender_email, receiver_email, msg.as_string())
except Exception as e:
print(e)
try:
#---------------------------------------------------------------------------------------#
# EL DORADO EXTRACT
#---------------------------------------------------------------------------------------#
# Set up Zip path.
zipPath = workspace
# setup output feature class
outfc = "Parcel_EL_Extracted"
# Check if zip from failed attempt still exists
existingZip = pathlib.Path(zipPath + r"\zipfolder")
if existingZip.exists():
shutil.rmtree(zipPath + r"\zipfolder")
log.write('Previous zip folder deleted')
# Setup the params for the extraction GP tool. The boundary is a polygon the grabs the whole county.
payload = {'f': 'json', 'env:outSR': '6418', 'Layers_to_Clip': '["Parcels"]', 'Area_of_Interest': '{"geometryType":"esriGeometryPolygon","features":[{"geometry":{"rings":[[[-13490599.294393552,4646257.881632805],[-13490599.294393552,4735689.204726496],[-13336502.24537058,4735689.204726496],[-13336502.24537058,4646257.881632805],[-13490599.294393552,4646257.881632805]]],"spatialReference":{"wkid":102100}}}],"sr":{"wkid":102100}}', 'Feature_Format': 'File Geodatabase - GDB - .gdb'}
# Make the request to the GP service.
log.write('Requesting parcels from EDC')
job = requests.get(r"https://see-eldorado.edcgov.us/arcgis/rest/services/uGOTNETandEXTRACTS/geoservices/GPServer/Extract%20Data%20Task/submitJob",params=payload)
jobJson = job.json()
# Check to make sure the job was accepted and get the JobID.
if 'jobId' in jobJson:
jobID = jobJson['jobId']
jobStatus = jobJson['jobStatus']
jobURL = r"https://see-eldorado.edcgov.us/arcgis/rest/services/uGOTNETandEXTRACTS/geoservices/GPServer/Extract%20Data%20Task/jobs"
if jobStatus == 'esriJobSubmitted' or jobStatus == 'esriJobExecuting':
log.write('EDC job submitted')
# Check the status of the job, when done grab the resulting ZIP file link.
while jobStatus == 'esriJobSubmitted' or jobStatus == 'esriJobExecuting':
time.sleep(5)
jobCheck = requests.get(jobURL+"/"+jobID+"?f=json")
jobJson = jobCheck.json()
if 'jobStatus' in jobJson:
jobStatus = jobJson['jobStatus']
if jobStatus == "esriJobSucceeded":
if 'results' in jobJson:
logging.info('EDC server job completed')
resultURL = jobJson['results']['Output_Zip_File']['paramUrl']
# Grab the ZIP link.
logging.info('Downloading ZIP from EDC')
jobResult = requests.get(jobURL+"/"+jobID+r"/"+resultURL+r"?f=json&returnType=data")
if jobStatus == "esriJobFailed":
logging.error('EDC server job failure')
if 'messages' in jobJson:
logging.error(jobJson['messages'])
raise ValueError('EDC job failed!')
# Get the ZIP file.
parcelsZip = requests.get(jobResult.json()['value']['url'])
logging.info('Downloaded ZIP from EDC')
# Save the ZIP into memory.
zipFile = ZipFile(BytesIO(parcelsZip.content))
# Unzip the ZIP to the defined path.
for each in zipFile.namelist():
if not each.endswith('/'):
root, name = os.path.split(each)
directory = os.path.normpath(os.path.join(zipPath, root))
if not os.path.isdir(directory):
os.makedirs(directory)
open(os.path.join(directory, name), 'wb').write(zipFile.read(each))
logging.info('Unzipped files in ' + str(zipPath))
# Setup env for parcel FGDB and set overwrite to true.
zipFolder = zipPath + r"\zipfolder"
# arcpy.env.overwriteOutput = True
in_features = os.path.join(workspace, "zipfolder\data.gdb\Parcels")
# Export to staging gdb
arcpy.management.CopyFeatures(in_features, outfc)
print("El Dorado Parcels Extracted")
# catch any arcpy errors
except arcpy.ExecuteError:
print(arcpy.GetMessages())
log.write(arcpy.GetMessages())
log.close()
header = "ERROR - Arcpy Exception - Check Log"
# send email with header based on try/except result
send_mail(header)
print('Sending email...')
# catch system errors
except Exception:
e = sys.exc_info()[1]
print(e.args[0])
log.write(e.args[0])
log.close()
header = "ERROR - System Error - Check Log"
# send email with header based on try/except result
send_mail(header)
print('Sending email...')