-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_server.py
More file actions
370 lines (288 loc) · 20.4 KB
/
Copy pathpython_server.py
File metadata and controls
370 lines (288 loc) · 20.4 KB
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
import http.server
import json
import arcpy
from arcgis.gis import GIS
from arcgis.geometry import Polyline
import shapefile
import os
from datetime import datetime
import time
class JSONHandler(http.server.BaseHTTPRequestHandler):
def do_POST(self):
# Get the content length from the request headers
content_length = int(self.headers['Content-Length'])
# Read the raw JSON data from the request
raw_data = self.rfile.read(content_length)
try:
# Parse the JSON data
data = json.loads(raw_data.decode('utf-8'))
# print(data)
temp_data = []
combined_path = []
spatial_reference = data['directionline'][0]["geometry"]["spatialReference"]
for i in (data['directionline']):
#print(i['geometry'])
combined_path.extend(i['geometry']['paths'])
#print(temp_data[0])
combined_polyline = Polyline({"paths":combined_path,"spatialReference":spatial_reference})
# Define the name of the shapefile and its geometry type
shapefile_name = "NewShapefile"
geometry_type = "POLYLINE"
# Define the spatial reference of the shapefile (optional)
spatial_reference = arcpy.SpatialReference(3857) # Example: WGS 1984 (4326)
folder_path = "C:\\Users\\jac13439\\Documents\\ArcGISExperienceBuilder\\client\\your-extensions\\f2"
items = os.listdir(folder_path)
# Check if the folder is empty
if len(items) != 0:
# Loop through each item and delete files or remove directories
for item in items:
item_path = os.path.join(folder_path, item)
if os.path.isfile(item_path):
# Delete the file
os.remove(item_path)
# Create the empty shapefile
shapefile_path = arcpy.CreateFeatureclass_management(folder_path, shapefile_name, geometry_type, spatial_reference = spatial_reference)
# Replace 'polyline_array' with your actual array containing the polyline coordinates
polyline_array = combined_path #[[x1, y1], [x2, y2], ...]
# Create a list to store the polyline features
polylines = []
# Convert each set of coordinates to arcpy.Point objects and create polylines
for polyline_coords in polyline_array:
array = arcpy.Array([arcpy.Point(float(coords[0]), float(coords[1])) for coords in polyline_coords])
polylines.append(arcpy.Polyline(array, spatial_reference))
# Open an InsertCursor to insert the polyline features into the shapefile
with arcpy.da.InsertCursor(shapefile_path, ["SHAPE@"]) as cursor:
for polyline in polylines:
cursor.insertRow([polyline])
#earthquake = shapefile.Reader("C:\\Users\\jac13439\\Documents\\ArcGISExperienceBuilder\\client\\your-extensions\\earthquakesRisk\\earthquakes\\earthquakeRisk.shp")
# folder_path = "C:\\Users\\jac13439\\Documents\\ArcGISExperienceBuilder\\client\\your-extensions\\output"
# os.mkdir(folder_path)
#items = os.listdir(folder_path)
# Check if the folder is empty
#try:
# if len(items) != 0:
# Loop through each item and delete files or remove directories
# for item in items:
# item_path = os.path.join(folder_path, item)
# if os.path.isfile(item_path):
# Delete the file
# os.remove(item_path)
#except:
# pass
## Eathquake ###########################################################################
infeature1 = "C:\\Users\\jac13439\\Documents\\ArcGISExperienceBuilder\\client\\your-extensions\\f2\\NewShapefile.shp"
infeature2 = "C:\\Users\\jac13439\\Documents\\ArcGISExperienceBuilder\\client\\your-extensions\\Earthquake_Data\\Earthquake_Data\\EarthquakeRisk.shp"
base_folder = "C:\\Users\\jac13439\\Documents\\ArcGISExperienceBuilder\\client\\your-extensions\\output_earthquake"
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") + "aa"
folder_path = os.path.join(base_folder, timestamp)
os.mkdir(folder_path)
ttemp = (os.path.join(folder_path,timestamp))
# Process: Find all stream crossings (points)
inFeatures = [infeature1, infeature2]
intersectOutput = ttemp #os.path.join(folder_path,timestamp) #r"C:\Users\jac13439\Documents\ArcGISExperienceBuilder\client\your-extensions\output\output"
arcpy.analysis.Intersect(inFeatures, intersectOutput, "", "", "point")
#print(os.path.join(ttemp + timestamp + ".sh"))
print(os.path.join(ttemp + ".shp"))
arcpy.management.AddJoin(
in_layer_or_view= os.path.join(ttemp + ".shp") , #s.path.join(base_folder, timestamp + ".sh"), #r"C:\Users\jac13439\Documents\ArcGISExperienceBuilder\client\your-extensions\output\output.shp",
in_field="Id_1",
join_table= r"C:\Users\jac13439\Documents\ArcGISExperienceBuilder\client\your-extensions\Earthquake_Data\Earthquake_Data\EarthquakeRisk.shp",
join_field="Id",
index_join_fields="NO_INDEX_JOIN_FIELDS",
#rebuild_index="NO_REBUILD_INDEX",
join_type="KEEP_COMMON"
)
# Path to the shapefile you want to read
shapefile_path = os.path.join(ttemp + ".shp")#r"C:\Users\jac13439\Documents\ArcGISExperienceBuilder\client\your-extensions\output\output.shp"
data_points = []
# Open a SearchCursor to read the shapefile
fields = ["SHAPE@", "gridcode","Shape"] # Replace field1, field2, ... with the field names you want to access
with arcpy.da.SearchCursor(shapefile_path, fields) as cursor:
for row in cursor:
# Access geometry and attribute information for each feature
# geometry = row[0] # The geometry of the feature (arcpy.Geometry object)
attribute1 = row[1] # The value of "field1" attribute for the feature
attribute2 = row[2] # The value of "field2" attribute for the feature
print(attribute2)
data_points.append(("Earthquake",attribute2,attribute1))
## Eathquake ###########################################################################
## Hurricane MAY ###########################################################################
infeature1 = "C:\\Users\\jac13439\\Documents\\ArcGISExperienceBuilder\\client\\your-extensions\\f2\\NewShapefile.shp"
infeature2 = "C:\\Users\\jac13439\\Documents\\ArcGISExperienceBuilder\\client\\your-extensions\\Hurricane_Data\\Hurricane_Data\\HurricaneRisk_MAY.shp"
base_folder = "C:\\Users\\jac13439\\Documents\\ArcGISExperienceBuilder\\client\\your-extensions\\output_hurricane"
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") + "ii"
folder_path = os.path.join(base_folder, timestamp)
os.mkdir(folder_path)
ttemp = (os.path.join(folder_path,timestamp))
# Process: Find all stream crossings (points)
inFeatures = [infeature1, infeature2]
intersectOutput = ttemp #os.path.join(folder_path,timestamp) #r"C:\Users\jac13439\Documents\ArcGISExperienceBuilder\client\your-extensions\output\output"
arcpy.analysis.Intersect(inFeatures, intersectOutput, "", "", "point")
#print(os.path.join(ttemp + timestamp + ".sh"))
print(os.path.join(ttemp + ".shp"))
arcpy.management.AddJoin(
in_layer_or_view= os.path.join(ttemp + ".shp") , #s.path.join(base_folder, timestamp + ".sh"), #r"C:\Users\jac13439\Documents\ArcGISExperienceBuilder\client\your-extensions\output\output.shp",
in_field="Id_1",
join_table= r"C:\Users\jac13439\Documents\ArcGISExperienceBuilder\client\your-extensions\\Hurricane_Data\\Hurricane_Data\\HurricaneRisk_MAY.shp",
join_field="Id",
index_join_fields="NO_INDEX_JOIN_FIELDS",
#rebuild_index="NO_REBUILD_INDEX",
join_type="KEEP_COMMON"
)
# Path to the shapefile you want to read
shapefile_path = os.path.join(ttemp + ".shp")#r"C:\Users\jac13439\Documents\ArcGISExperienceBuilder\client\your-extensions\output\output.shp"
# data_points = []
# Open a SearchCursor to read the shapefile
fields = ["SHAPE@", "gridcode","Shape","Explain"] # Replace field1, field2, ... with the field names you want to access
with arcpy.da.SearchCursor(shapefile_path, fields) as cursor:
for row in cursor:
# Access geometry and attribute information for each feature
# geometry = row[0] # The geometry of the feature (arcpy.Geometry object)
attribute1 = row[1] # The value of "field1" attribute for the feature
attribute2 = row[2] # The value of "field2" attribute for the feature
attribute3 = row[3] # The value of "field3" attribute for the feature
print(attribute3)
data_points.append(("Hurricane_May",attribute2,attribute1))
## Hurricane MAY ###########################################################################
## Hurricane JUNE ###########################################################################
infeature1 = "C:\\Users\\jac13439\\Documents\\ArcGISExperienceBuilder\\client\\your-extensions\\f2\\NewShapefile.shp"
infeature2 = "C:\\Users\\jac13439\\Documents\\ArcGISExperienceBuilder\\client\\your-extensions\\Hurricane_Data\\Hurricane_Data\\HurricaneRisk_JUNE.shp"
base_folder = "C:\\Users\\jac13439\\Documents\\ArcGISExperienceBuilder\\client\\your-extensions\\output_hurricane"
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") + "ll"
folder_path = os.path.join(base_folder, timestamp)
os.mkdir(folder_path)
ttemp = (os.path.join(folder_path,timestamp))
# Process: Find all stream crossings (points)
inFeatures = [infeature1, infeature2]
intersectOutput = ttemp #os.path.join(folder_path,timestamp) #r"C:\Users\jac13439\Documents\ArcGISExperienceBuilder\client\your-extensions\output\output"
arcpy.analysis.Intersect(inFeatures, intersectOutput, "", "", "point")
#print(os.path.join(ttemp + timestamp + ".sh"))
print(os.path.join(ttemp + ".shp"))
arcpy.management.AddJoin(
in_layer_or_view= os.path.join(ttemp + ".shp") , #s.path.join(base_folder, timestamp + ".sh"), #r"C:\Users\jac13439\Documents\ArcGISExperienceBuilder\client\your-extensions\output\output.shp",
in_field="Id_1",
join_table= r"C:\Users\jac13439\Documents\ArcGISExperienceBuilder\client\your-extensions\\Hurricane_Data\\Hurricane_Data\\HurricaneRisk_JUNE.shp",
join_field="Id",
index_join_fields="NO_INDEX_JOIN_FIELDS",
#rebuild_index="NO_REBUILD_INDEX",
join_type="KEEP_COMMON"
)
# Path to the shapefile you want to read
shapefile_path = os.path.join(ttemp + ".shp")#r"C:\Users\jac13439\Documents\ArcGISExperienceBuilder\client\your-extensions\output\output.shp"
# data_points = []
# Open a SearchCursor to read the shapefile
fields = ["SHAPE@", "gridcode","Shape","Explain"] # Replace field1, field2, ... with the field names you want to access
with arcpy.da.SearchCursor(shapefile_path, fields) as cursor:
for row in cursor:
# Access geometry and attribute information for each feature
# geometry = row[0] # The geometry of the feature (arcpy.Geometry object)
attribute1 = row[1] # The value of "field1" attribute for the feature
attribute2 = row[2] # The value of "field2" attribute for the feature
attribute3 = row[3] # The value of "field3" attribute for the feature
print(attribute3)
data_points.append(("Hurricane_JUNE",attribute2,attribute1))
## Hurricane JUNE ###########################################################################
## Hurricane JULY1 ###########################################################################
infeature1 = "C:\\Users\\jac13439\\Documents\\ArcGISExperienceBuilder\\client\\your-extensions\\f2\\NewShapefile.shp"
infeature2 = "C:\\Users\\jac13439\\Documents\\ArcGISExperienceBuilder\\client\\your-extensions\\Hurricane_Data\\Hurricane_Data\\HurricaneRisk_JULY1.shp"
base_folder = "C:\\Users\\jac13439\\Documents\\ArcGISExperienceBuilder\\client\\your-extensions\\output_hurricane"
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") + "gg"
folder_path = os.path.join(base_folder, timestamp)
os.mkdir(folder_path)
ttemp = (os.path.join(folder_path,timestamp))
# Process: Find all stream crossings (points)
inFeatures = [infeature1, infeature2]
intersectOutput = ttemp #os.path.join(folder_path,timestamp) #r"C:\Users\jac13439\Documents\ArcGISExperienceBuilder\client\your-extensions\output\output"
arcpy.analysis.Intersect(inFeatures, intersectOutput, "", "", "point")
#print(os.path.join(ttemp + timestamp + ".sh"))
print(os.path.join(ttemp + ".shp"))
arcpy.management.AddJoin(
in_layer_or_view= os.path.join(ttemp + ".shp") , #s.path.join(base_folder, timestamp + ".sh"), #r"C:\Users\jac13439\Documents\ArcGISExperienceBuilder\client\your-extensions\output\output.shp",
in_field="Id_1",
join_table= r"C:\Users\jac13439\Documents\ArcGISExperienceBuilder\client\your-extensions\\Hurricane_Data\\Hurricane_Data\\HurricaneRisk_JULY1.shp",
join_field="Id",
index_join_fields="NO_INDEX_JOIN_FIELDS",
#rebuild_index="NO_REBUILD_INDEX",
join_type="KEEP_COMMON"
)
# Path to the shapefile you want to read
shapefile_path = os.path.join(ttemp + ".shp")#r"C:\Users\jac13439\Documents\ArcGISExperienceBuilder\client\your-extensions\output\output.shp"
# data_points = []
# Open a SearchCursor to read the shapefile
fields = ["SHAPE@", "gridcode","Shape","Explain"] # Replace field1, field2, ... with the field names you want to access
with arcpy.da.SearchCursor(shapefile_path, fields) as cursor:
for row in cursor:
# Access geometry and attribute information for each feature
# geometry = row[0] # The geometry of the feature (arcpy.Geometry object)
attribute1 = row[1] # The value of "field1" attribute for the feature
attribute2 = row[2] # The value of "field2" attribute for the feature
attribute3 = row[3] # The value of "field3" attribute for the feature
print(attribute3)
data_points.append(("Hurricane_JULY",attribute2,attribute1))
## Hurricane JULY1 ###########################################################################
## Hurricane AUG ###########################################################################
infeature1 = "C:\\Users\\jac13439\\Documents\\ArcGISExperienceBuilder\\client\\your-extensions\\f2\\NewShapefile.shp"
infeature2 = "C:\\Users\\jac13439\\Documents\\ArcGISExperienceBuilder\\client\\your-extensions\\Hurricane_Data\\Hurricane_Data\\HurricaneRisk_AUG.shp"
base_folder = "C:\\Users\\jac13439\\Documents\\ArcGISExperienceBuilder\\client\\your-extensions\\output_hurricane"
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") + "ff"
folder_path = os.path.join(base_folder, timestamp)
os.mkdir(folder_path)
ttemp = (os.path.join(folder_path,timestamp))
# Process: Find all stream crossings (points)
inFeatures = [infeature1, infeature2]
intersectOutput = ttemp #os.path.join(folder_path,timestamp) #r"C:\Users\jac13439\Documents\ArcGISExperienceBuilder\client\your-extensions\output\output"
arcpy.analysis.Intersect(inFeatures, intersectOutput, "", "", "point")
#print(os.path.join(ttemp + timestamp + ".sh"))
print(os.path.join(ttemp + ".shp"))
arcpy.management.AddJoin(
in_layer_or_view= os.path.join(ttemp + ".shp") , #s.path.join(base_folder, timestamp + ".sh"), #r"C:\Users\jac13439\Documents\ArcGISExperienceBuilder\client\your-extensions\output\output.shp",
in_field="Id_1",
join_table= r"C:\Users\jac13439\Documents\ArcGISExperienceBuilder\client\your-extensions\\Hurricane_Data\\Hurricane_Data\\HurricaneRisk_AUG.shp",
join_field="Id",
index_join_fields="NO_INDEX_JOIN_FIELDS",
#rebuild_index="NO_REBUILD_INDEX",
join_type="KEEP_COMMON"
)
# Path to the shapefile you want to read
shapefile_path = os.path.join(ttemp + ".shp")#r"C:\Users\jac13439\Documents\ArcGISExperienceBuilder\client\your-extensions\output\output.shp"
# data_points = []
# Open a SearchCursor to read the shapefile
fields = ["SHAPE@", "gridcode","Shape","Explain"] # Replace field1, field2, ... with the field names you want to access
with arcpy.da.SearchCursor(shapefile_path, fields) as cursor:
for row in cursor:
# Access geometry and attribute information for each feature
# geometry = row[0] # The geometry of the feature (arcpy.Geometry object)
attribute1 = row[1] # The value of "field1" attribute for the feature
attribute2 = row[2] # The value of "field2" attribute for the feature
attribute3 = row[3] # The value of "field3" attribute for the feature
print(attribute3)
data_points.append(("Hurricane_AUG",attribute2,attribute1))
## Hurricane JULY1 ###########################################################################
response_data = {"status": "success", "message": "JSON data received successfully", "data": data_points}
response_status = 200
except json.JSONDecodeError:
response_data = {"status": "error", "message": "Invalid JSON data"}
response_status = 400
# Set the response headers
self.send_response(response_status)
self.send_header('Content-type', 'application/json')
# Add the 'Access-Control-Allow-Origin' header to allow requests from all origins
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
# Send the response data
response = json.dumps(response_data).encode('utf-8')
self.wfile.write(response)
if __name__ == '__main__':
host = 'localhost'
port = 8000
server_address = (host, port)
# Create the HTTP server with our custom JSONHandler
httpd = http.server.HTTPServer(server_address, JSONHandler)
print(f"Server started at http://{host}:{port}")
try:
# Start the server and keep it running until interrupted
httpd.serve_forever()
except KeyboardInterrupt:
# If the server is interrupted (e.g., by pressing Ctrl+C), stop it gracefully
print("\nServer stopped")
httpd.server_close()