-
Notifications
You must be signed in to change notification settings - Fork 11
/
loadphotopoints.py
401 lines (300 loc) · 16.3 KB
/
loadphotopoints.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
"""
@author: Esri
@contact: cbuscaglia@esri.com
@company: Esri
@version: 2.0
@description: Photo Survey Tool to load photos
@requirements: Python 2.7.x or higher, ArcGIS ArcMap 10.2, 10.3.x, 10.4, ArcGIS Pro 2.0 and above (also ArcGIS Pro)
@copyright: Esri, 2015
"""
# Import modules
import arcpy
import math
import sys, os
from os.path import join
arcpy.env.overwriteOutput = True
# Script arguments (set in the GP tool)
CameraInput = arcpy.GetParameterAsText(0)
SinglePhotos = arcpy.GetParameterAsText(1)
Location = arcpy.GetParameterAsText(2)
PassengerPhotos = arcpy.GetParameterAsText(3)
DriverPhotos = arcpy.GetParameterAsText(4)
AngleField = arcpy.GetParameterAsText(5)
Geodatabase = arcpy.GetParameterAsText(6)
Parcels = arcpy.GetParameterAsText(7)
ParcelPIN = arcpy.GetParameterAsText(8)
TemplateGDB = arcpy.GetParameterAsText(9)
# Retrieve Template Feature Class and Template Questions Table from Template Geodatabase
arcpy.env.workspace = TemplateGDB
TemplateFC = arcpy.ListFeatureClasses()
TemplateQTable = arcpy.ListTables()
TemplateFC = TemplateGDB + "\\" + TemplateFC[0]
TemplateQTable = TemplateGDB +"\\" + TemplateQTable[0]
#Invalid Photo IDs
invalidPhotosTablePassenger = Geodatabase + "\\InvalidPhotosPassenger"
invalidPhotosTableDriver = Geodatabase + "\\InvalidPhotosDriver"
invalidPhotosTable = Geodatabase + "\\InvalidPhotos"
arcpy.AddMessage("Step 1: Loading input parameters")
if str (AngleField) == 'true':
AngleField = 'Direction'
else:
AngleField = ''
if CameraInput == 'Associate Photo with Parcel':
# ______________________________________________________________________________#
#
# Convert Passenger Photos to Points
#_______________________________________________________________________________#
PhotoFeatureClass = Geodatabase + "\\PointAttachmentsTemp" #"""{}\\PointAttachmentsTemp""".format(Geodatabase)
arcpy.GeoTaggedPhotosToPoints_management(PassengerPhotos, PhotoFeatureClass, invalidPhotosTablePassenger, "ONLY_GEOTAGGED", "NO_ATTACHMENTS")
#______________________________________________________________________________#
#
# If Name is used for ParcelPIN make adjustments
#______________________________________________________________________________#
if ParcelPIN is "Name":
arcpy.AlterField_management(PhotoFeatureClass, ParcelPIN, "Image_Name")
else:
pass
SR = arcpy.Describe(Parcels)
SRHelper = SR.spatialReference
PhotoFeatureClass2 = Geodatabase + "\\PointAttachments"
arcpy.Project_management(PhotoFeatureClass, PhotoFeatureClass2, SRHelper)
arcpy.DeleteIdentical_management(PhotoFeatureClass2, "Shape")
arcpy.Delete_management(PhotoFeatureClass)
EntGDB = arcpy.Describe(Geodatabase)
EntGDB.workspaceType
if EntGDB is 'RemoteDatabase':
arcpy.RegisterAsVersioned_management(PhotoFeatureClass2)
else:
pass
arcpy.AddMessage("Step 2: Converting Photos to points")
# Load up the parcel dataset for the property association (and make a copy)
ParcelsFeatureClass = Geodatabase + "\\Parcels"""
arcpy.CopyFeatures_management(Parcels, ParcelsFeatureClass)
arcpy.AddMessage("Step 3: Copying Parcels to staging geodatabase")
# Snap Passenger Photos to nearest parcel edge (30ft. default)
shape = arcpy.Describe(PhotoFeatureClass2).ShapeFieldName
fields = ['SHAPE@XY', AngleField]
def shift_photopoints(in_features, x_shift=None, y_shift=None):
with arcpy.da.UpdateCursor(in_features, fields) as cursor:
for row in cursor:
x = row[0][0] + x_shift * math.cos(math.degrees(int(row[1])))
y = row[0][1] + y_shift * math.sin(math.degrees(int(row[1])))
row[0] = (x, y)
cursor.updateRow(row)
return
if AngleField:
shift_photopoints(PhotoFeatureClass2, 15, 15)
else:
pass
snapenv = [ParcelsFeatureClass, "EDGE", "30 Feet"]
arcpy.Snap_edit(PhotoFeatureClass2, [snapenv])
parcelsOID = arcpy.Describe(ParcelsFeatureClass).OIDFieldName
Nearhelper = Geodatabase + "\\NEAR"
NEAR = Nearhelper
arcpy.GenerateNearTable_analysis(PhotoFeatureClass2, ParcelsFeatureClass, NEAR,
"5 Feet", "NO_LOCATION", "NO_ANGLE", "CLOSEST", "0", "GEODESIC")
arcpy.AddMessage("Step 4: Associating passenger photo points to nearest parcel")
arcpy.JoinField_management(NEAR, "NEAR_FID", ParcelsFeatureClass, parcelsOID, ParcelPIN)
# Export non-matched Photos to table (no GPS, wrong attributes, etc.)
arcpy.JoinField_management(PhotoFeatureClass2, "OBJECTID", NEAR, "IN_FID")
arcpy.TableToTable_conversion(PhotoFeatureClass2, Geodatabase,
"NonMatchedPassengerPhotos", "{0} is Null".format(ParcelPIN), "")
arcpy.AddMessage("Step 5: Reporting non-matched passenger photos to table")
# Cleanup matched Photos (intermediate data)
arcpy.DeleteField_management(PhotoFeatureClass2, "IN_FID;NEAR_FID;NEAR_DIST")
arcpy.AddField_management(PhotoFeatureClass2, "REVERSE", "TEXT", "", "", "5", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.CalculateField_management(PhotoFeatureClass2, "REVERSE", "\"YES\"", "PYTHON", "")
arcpy.Delete_management(NEAR)
#______________________________________________________________________________#
#
# Convert Driver Photos to Points
#______________________________________________________________________________#
PhotoFeatureClass = Geodatabase + "\\PointAttachmentsTemp"
arcpy.GeoTaggedPhotosToPoints_management(DriverPhotos, PhotoFeatureClass, invalidPhotosTableDriver, "ONLY_GEOTAGGED", "NO_ATTACHMENTS")
#______________________________________________________________________________#
#
# If Name is used for ParcelPIN make adjustments
#______________________________________________________________________________#
if ParcelPIN is "Name":
arcpy.AlterField_management(PhotoFeatureClass, ParcelPIN, "Image_Name")
else:
pass
SR = arcpy.Describe(Parcels)
SRHelper = SR.spatialReference
PhotoFeatureClass3 = Geodatabase + "\\PointAttachments2"
arcpy.Project_management(PhotoFeatureClass, PhotoFeatureClass3, SRHelper)
arcpy.DeleteIdentical_management(PhotoFeatureClass3, "Shape")
arcpy.Delete_management(PhotoFeatureClass)
arcpy.MakeFeatureLayer_management(ParcelsFeatureClass, "PARCELSFL")
arcpy.SelectLayerByLocation_management("PARCELSFL", "INTERSECT", PhotoFeatureClass2, "", "NEW_SELECTION", "INVERT")
arcpy.MakeFeatureLayer_management("PARCELSFL", "PARCELSFL2")
# Snap Driver Photos to nearest parcel edge (100 ft. default)
shape = arcpy.Describe(PhotoFeatureClass3).ShapeFieldName
fields = ['SHAPE@XY', AngleField]
def shift_photopoints(in_features, x_shift=None, y_shift=None):
with arcpy.da.UpdateCursor(in_features, fields) as cursor:
for row in cursor:
x = row[0][0] + x_shift * math.cos(math.degrees(int(row[1])))
y = row[0][1] + y_shift * math.sin(math.degrees(int(row[1])))
row[0] = (x, y)
cursor.updateRow(row)
return
if AngleField:
shift_photopoints(PhotoFeatureClass3, 15, 15)
else:
pass
snapenv = ["PARCELSFL2", "EDGE", "100 Feet"]
arcpy.Snap_edit(PhotoFeatureClass3, [snapenv])
Nearhelper = Geodatabase + "\\NEAR"
NEAR = Nearhelper
arcpy.GenerateNearTable_analysis(PhotoFeatureClass3, ParcelsFeatureClass, NEAR,
"5 Feet", "NO_LOCATION", "NO_ANGLE", "CLOSEST", "0", "GEODESIC")
arcpy.AddMessage("Step 6: Associating driver photo points to nearest parcel")
arcpy.JoinField_management(NEAR, "NEAR_FID", ParcelsFeatureClass, parcelsOID, ParcelPIN)
# Export non-matched Photos to table (no GPS, wrong attributes, etc.)
arcpy.JoinField_management(PhotoFeatureClass3, "OBJECTID", NEAR, "IN_FID")
arcpy.TableToTable_conversion(PhotoFeatureClass3, Geodatabase, "NonMatchedDriverPhotos",
"{0} is Null".format(ParcelPIN), "")
arcpy.AddMessage("Step 7: Reporting non-matched driver photos to table")
# Cleanup matched Photos (intermediate data)
arcpy.DeleteField_management(PhotoFeatureClass3, "IN_FID;NEAR_FID;NEAR_DIST")
arcpy.Delete_management(NEAR)
arcpy.AddField_management(PhotoFeatureClass2, "Path2", "TEXT", "", "", "150", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.CalculateField_management(PhotoFeatureClass2, "Path2", "!Path!", "PYTHON", "")
arcpy.AddField_management(PhotoFeatureClass3, "Path2", "TEXT", "", "", "150", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.CalculateField_management(PhotoFeatureClass3, "Path2", "!Path!", "PYTHON", "")
arcpy.DeleteField_management(PhotoFeatureClass2, "Path")
arcpy.DeleteField_management(PhotoFeatureClass3, "Path")
arcpy.AddField_management(PhotoFeatureClass3, "REVERSE", "TEXT", "", "", "5", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.CalculateField_management(PhotoFeatureClass3, "REVERSE", "\"NO\"", "PYTHON", "")
arcpy.Append_management(PhotoFeatureClass2, PhotoFeatureClass3, "NO_TEST", "", "")
#Create Photo Attachments
ParcelPointClassHelper = Geodatabase + "\\PointsTemp"
ParcelPointHelper = Geodatabase + "\\PhotoPoints"
arcpy.FeatureClassToFeatureClass_conversion(TemplateFC,Geodatabase,"PhotoPoints")
arcpy.DefineProjection_management(ParcelPointHelper, SRHelper)
arcpy.AddField_management(ParcelPointHelper, ParcelPIN, "TEXT", "", "", "50", ParcelPIN, "NULLABLE", "NON_REQUIRED")
arcpy.FeatureToPoint_management(ParcelsFeatureClass, ParcelPointClassHelper, "INSIDE")
else:
pass
if CameraInput == 'Associate Geotagged Photo with Point (photo has location)':
# ______________________________________________________________________________#
#
# Convert Photos to Points
#_______________________________________________________________________________#
ParcelPointHelper = Geodatabase + "\\PhotoPoints"
arcpy.FeatureClassToFeatureClass_conversion(TemplateFC,Geodatabase,"PhotoPoints")
else:
pass
if CameraInput == 'Associate Non-Geotagged Photo with specified Point (no location)':
# ______________________________________________________________________________#
#
# Convert Photos to Pointsw/ no coordinates
#_______________________________________________________________________________#
ParcelPointHelper = Geodatabase + "\\PhotoPoints"
arcpy.FeatureClassToFeatureClass_conversion(TemplateFC,Geodatabase,"PhotoPoints")
else:
pass
if CameraInput == "Associate Photo with Parcel":
arcpy.AddMessage("Step 8: Adding survey question fields")
else:
arcpy.AddMessage("Step 2: Adding Survey question fields")
if CameraInput == 'Associate Photo with Parcel':
arcpy.Append_management(ParcelPointClassHelper, ParcelPointHelper, "NO_TEST")
arcpy.AddField_management(ParcelPointHelper, "REVERSE", "TEXT", "", "", "5", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.JoinField_management(ParcelPointHelper, ParcelPIN, PhotoFeatureClass3, ParcelPIN)
arcpy.CalculateField_management(ParcelPointHelper, "REVERSE", "!REVERSE_1!", "PYTHON", "")
arcpy.EnableAttachments_management(ParcelPointHelper)
arcpy.AddAttachments_management(ParcelPointHelper, ParcelPIN, PhotoFeatureClass3, ParcelPIN, "Path2", "")
arcpy.AddMessage("Step 9: Creating photo attachments")
else:
pass
if CameraInput == 'Associate Geotagged Photo with Point (photo has location)':
arcpy.AddMessage("Step 3: Adding application required fields")
arcpy.AddField_management(ParcelPointHelper, "BSTPHOTOID", "TEXT", "", "", "25", "Best Photo Identifier", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(ParcelPointHelper, "SRVNAME", "TEXT", "", "", "25", "Surveyor Name", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(ParcelPointHelper, "Path", "TEXT","", "", "254", "Path", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(ParcelPointHelper, "Name", "TEXT","", "", "150", "Name", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(ParcelPointHelper, "DateTime", "TEXT","", "", "100", "DateTime", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(ParcelPointHelper, "Direction", "Double","", "", "", "Direction", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddMessage("Step 4: Finalizing photo survey feature class")
arcpy.AddMessage("Step 5: Creating Photo Attachments")
ParcelPointHelperTemp = Geodatabase + "\\ParcelPointsTemp"
arcpy.GeoTaggedPhotosToPoints_management(SinglePhotos, ParcelPointHelperTemp, invalidPhotosTable, "ONLY_GEOTAGGED", "NO_ATTACHMENTS")
arcpy.Append_management(ParcelPointHelperTemp, ParcelPointHelper, "NO_TEST")
arcpy.EnableAttachments_management(ParcelPointHelper)
arcpy.AddAttachments_management(ParcelPointHelper, "OBJECTID", ParcelPointHelper, "OBJECTID", "Path", "")
arcpy.Delete_management(ParcelPointHelperTemp)
arcpy.DeleteField_management(ParcelPointHelper, "Path")
else:
pass
if CameraInput == 'Associate Non-Geotagged Photo with specified Point (no location)':
arcpy.AddMessage("Step 3: Adding application required fields")
arcpy.AddField_management(ParcelPointHelper, "BSTPHOTOID", "TEXT", "", "", "25", "Best Photo Identifier", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(ParcelPointHelper, "SRVNAME", "TEXT", "", "", "25", "Surveyor Name", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(ParcelPointHelper, "Path", "TEXT","", "", "254", "Path", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(ParcelPointHelper, "Name", "TEXT","", "", "150", "Name", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(ParcelPointHelper, "DateTime", "TEXT","", "", "100", "DateTime", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(ParcelPointHelper, "Direction", "Double","", "", "", "Direction", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddMessage("Step 4: Finalizing photo survey feature class")
arcpy.AddMessage("Step 5: Creating Photo Attachments")
PointHelperTemp = Geodatabase + "\\PointsTemp"
arcpy.GeoTaggedPhotosToPoints_management(SinglePhotos, PointHelperTemp, invalidPhotosTable, "ALL_PHOTOS", "NO_ATTACHMENTS")
arcpy.Append_management(PointHelperTemp, ParcelPointHelper, "NO_TEST")
arcpy.EnableAttachments_management(ParcelPointHelper)
arcpy.AddAttachments_management(ParcelPointHelper, "OBJECTID", ParcelPointHelper, "OBJECTID", "Path", "")
shape = arcpy.Describe(ParcelPointHelper).ShapeFieldName
fields = ['SHAPE@XY']
edit = arcpy.da.Editor(Geodatabase)
edit.startEditing(False, True)
Coord = Location.split(' ')
Coord2 = ",".join(Coord)
with arcpy.da.UpdateCursor(ParcelPointHelper, "SHAPE@XY") as cur:
for row in cur:
row[0] = eval (Coord2)
cur.updateRow(row)
edit.stopEditing(True)
arcpy.Delete_management(PointHelperTemp)
arcpy.DeleteField_management(ParcelPointHelper, "Path")
else:
pass
#______________________________________________________________________________#
#
# Cleanup Staging GeoDatabase
#______________________________________________________________________________#
if CameraInput == 'Associate Photo with Parcel':
arcpy.Delete_management(PhotoFeatureClass2)
arcpy.Delete_management(PhotoFeatureClass3)
arcpy.Delete_management(ParcelsFeatureClass)
arcpy.Delete_management(ParcelPointClassHelper)
arcpy.DeleteField_management(ParcelPointHelper, "ORIG_FID")
arcpy.DeleteField_management(ParcelPointHelper, "REVERSE_1")
arcpy.DeleteField_management(ParcelPointHelper, "PATH2")
arcpy.DeleteField_management(ParcelPointHelper, ParcelPIN + "_1")
if ParcelPIN is "Name":
arcpy.MakeFeatureLayer_management(ParcelPointHelper, "PARCELSFORSELECTION", "Image_Name is NULL")
else:
arcpy.MakeFeatureLayer_management(ParcelPointHelper, "PARCELSFORSELECTION", "Name is NULL")
arcpy.DeleteRows_management("PARCELSFORSELECTION")
arcpy.DeleteField_management(ParcelPointHelper, "Name")
arcpy.AddMessage("Step 10: Cleaning up staging geodatabase")
arcpy.AddMessage("Step 11: Adding application required fields")
arcpy.AddField_management(ParcelPointHelper, "BSTPHOTOID", "TEXT", "", "", "25", "Best Photo Identifier", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(ParcelPointHelper, "SRVNAME", "TEXT", "", "", "25", "Surveyor Name", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddMessage("Step 12: Finalizing photo survey feature class")
else:
pass
#______________________________________________________________________________#
#
# Add Template Table to Staging GeoDatabase
#______________________________________________________________________________#
arcpy.Copy_management(TemplateQTable, Geodatabase + "//SurveyQuestions")
if CameraInput == "Associate Photo with Parcel":
arcpy.AddMessage("Step 13: Adding survey questions template table to staging geodatabase")
else:
arcpy.AddMessage("Step 6: Adding survey questions template table to staging geodatabase")
try:
arcpy.SetParameter(10, Geodatabase + "\\PhotoPoints")
arcpy.SetParameter(11, Geodatabase + "\\SurveyQuestions")
except:
arcpy.AddWarning("Unable to add output photopoints and survey questions table to map")