-
Notifications
You must be signed in to change notification settings - Fork 0
/
facilities.sql
781 lines (754 loc) · 26.9 KB
/
facilities.sql
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
-- //////////////////////////////////////////////////////////////////////
-- Queries to get the CSV files which provide the operational point data for the Facilities Web application
-- Written by Regan Sarwas, 2019-08-20 +/-
--
-- TODO: Consider adding facilities in GIS (that have photos?) even if not in FMSS
-- ISEXTANT = 'True' AND ISOUTPARK <> 'Yes' AND P.FACMAINTAIN IN ('NPS','FEDERAL')
-- //////////////////////////////////////////////////////////////////////
-- Make sure we are using the DEFAULT version in SDE
exec sde.set_default
-------------------------
--
-- Create Temp tables used in queries
--
-------------------------
create TABLE #PhotoId_A (ID nvarchar(50) NOT NULL PRIMARY KEY );
create TABLE #PhotoId_F (ID nvarchar(50) NOT NULL PRIMARY KEY );
create TABLE #PhotoId_G (ID nvarchar(50) NOT NULL PRIMARY KEY );
create TABLE #PhotoId_L (ID nvarchar(50) NOT NULL PRIMARY KEY );
INSERT INTO #PhotoId_A select FACASSETID from akr_facility2.gis.AKR_ATTACH_evw where FACASSETID is not null group by FACASSETID order by FACASSETID
INSERT INTO #PhotoId_F select FEATUREID from akr_facility2.gis.AKR_ATTACH_evw where FEATUREID is not null group by FEATUREID order by FEATUREID
INSERT INTO #PhotoId_G select GEOMETRYID from akr_facility2.gis.AKR_ATTACH_evw where GEOMETRYID is not null group by GEOMETRYID order by GEOMETRYID
INSERT INTO #PhotoId_L select FACLOCID from akr_facility2.gis.AKR_ATTACH_evw where FACLOCID is not null group by FACLOCID order by FACLOCID
SELECT g.FACLOCID, g.FACASSETID, g.MAPLABEL, g.BLDGTYPE, g.Shape, dbo.concat4id(p1.ID, p2.ID, p3.ID, p4.ID) AS PhotoId
INTO #Buildings
FROM akr_facility2.gis.AKR_BLDG_CENTER_PT_evw as g
LEFT JOIN #PhotoId_A as p1 on p1.ID = g.FACASSETID
LEFT JOIN #PhotoId_F as p2 on p2.ID = g.FEATUREID
LEFT JOIN #PhotoId_G as p3 on p3.ID = g.GEOMETRYID
LEFT JOIN #PhotoId_L as p4 on p4.ID = g.FACLOCID
WHERE (p1.ID IS NOT NULL OR p2.ID IS NOT NULL OR p3.ID IS NOT NULL OR p4.ID IS NOT NULL OR g.FACLOCID IS NOT NULL OR g.FACASSETID IS NOT NULL)
SELECT g.FACLOCID, g.FACASSETID, g.MAPLABEL, g.Shape, dbo.concat4id(p1.ID, p2.ID, p3.ID, p4.ID) AS PhotoId
INTO #Parking
FROM akr_facility2.gis.PARKLOTS_PY_evw as g
LEFT JOIN #PhotoId_A as p1 on p1.ID = g.FACASSETID
LEFT JOIN #PhotoId_F as p2 on p2.ID = g.FEATUREID
LEFT JOIN #PhotoId_G as p3 on p3.ID = g.GEOMETRYID
LEFT JOIN #PhotoId_L as p4 on p4.ID = g.FACLOCID
WHERE (p1.ID IS NOT NULL OR p2.ID IS NOT NULL OR p3.ID IS NOT NULL OR p4.ID IS NOT NULL OR g.FACLOCID IS NOT NULL OR g.FACASSETID IS NOT NULL)
SELECT g.FACLOCID, g.FACASSETID, g.MAPLABEL, g.ISBRIDGE, g.Shape, dbo.concat4id(p1.ID, p2.ID, p3.ID, p4.ID) AS PhotoId
INTO #Trails
FROM akr_facility2.gis.TRAILS_LN_evw as g
LEFT JOIN #PhotoId_A as p1 on p1.ID = g.FACASSETID
LEFT JOIN #PhotoId_F as p2 on p2.ID = g.FEATUREID
LEFT JOIN #PhotoId_G as p3 on p3.ID = g.GEOMETRYID
LEFT JOIN #PhotoId_L as p4 on p4.ID = g.FACLOCID
WHERE (p1.ID IS NOT NULL OR p2.ID IS NOT NULL OR p3.ID IS NOT NULL OR p4.ID IS NOT NULL OR g.FACLOCID IS NOT NULL OR g.FACASSETID IS NOT NULL)
AND g.LINETYPE = 'Center line'
SELECT g.FACLOCID, g.FACASSETID, g.MAPLABEL, g.Shape, dbo.concat4id(p1.ID, p2.ID, p3.ID, p4.ID) AS PhotoId,
CASE WHEN g.TRLFEATTYPE = 'Other' THEN g.TRLFEATTYPEOTHER ELSE g.TRLFEATTYPE END +
CASE WHEN g.TRLFEATSUBTYPE is NULL THEN '' ELSE ', ' + g.TRLFEATSUBTYPE END AS FEATTYPE,
g.TRLFEATDESC AS FEATDESC
INTO #Trail_Feats
FROM akr_facility2.gis.TRAILS_FEATURE_PT_evw as g
LEFT JOIN #PhotoId_A as p1 on p1.ID = g.FACASSETID
LEFT JOIN #PhotoId_F as p2 on p2.ID = g.FEATUREID
LEFT JOIN #PhotoId_G as p3 on p3.ID = g.GEOMETRYID
LEFT JOIN #PhotoId_L as p4 on p4.ID = g.FACLOCID
WHERE (p1.ID IS NOT NULL OR p2.ID IS NOT NULL OR p3.ID IS NOT NULL OR p4.ID IS NOT NULL OR g.FACASSETID IS NOT NULL)
OR (g.FACLOCID IS NOT NULL AND TRLFEATTYPE <> 'Trail Head'AND TRLFEATTYPE <> 'Trail End' AND TRLFEATTYPEOTHER <> 'AnchorPt')
SELECT g.FACLOCID, g.FACASSETID, g.Shape, dbo.concat4id(p1.ID, p2.ID, p3.ID, p4.ID) AS PhotoId,
CASE WHEN g.TRLATTRTYPE = 'Other' THEN g.TRLATTRTYPEOTHER ELSE g.TRLATTRTYPE END +
CASE WHEN g.TRLATTRVALUE is NULL THEN '' ELSE ', ' + g.TRLATTRVALUE END AS ATTTYPE,
g.TRLATTRDESC as ATTDESC
INTO #Trail_Atts
FROM akr_facility2.gis.TRAILS_ATTRIBUTE_PT_evw as g
LEFT JOIN #PhotoId_A as p1 on p1.ID = g.FACASSETID
LEFT JOIN #PhotoId_F as p2 on p2.ID = g.FEATUREID
LEFT JOIN #PhotoId_G as p3 on p3.ID = g.GEOMETRYID
LEFT JOIN #PhotoId_L as p4 on p4.ID = g.FACLOCID
WHERE (p1.ID IS NOT NULL OR p2.ID IS NOT NULL OR p3.ID IS NOT NULL OR p4.ID IS NOT NULL OR g.FACLOCID IS NOT NULL OR g.FACASSETID IS NOT NULL)
SELECT g.FACLOCID, g.FACASSETID, g.MAPLABEL, g.ISBRIDGE, g.Shape, dbo.concat4id(p1.ID, p2.ID, p3.ID, p4.ID) AS PhotoId
INTO #Roads
FROM akr_facility2.gis.ROADS_LN_evw as g
LEFT JOIN #PhotoId_A as p1 on p1.ID = g.FACASSETID
LEFT JOIN #PhotoId_F as p2 on p2.ID = g.FEATUREID
LEFT JOIN #PhotoId_G as p3 on p3.ID = g.GEOMETRYID
LEFT JOIN #PhotoId_L as p4 on p4.ID = g.FACLOCID
WHERE (p1.ID IS NOT NULL OR p2.ID IS NOT NULL OR p3.ID IS NOT NULL OR p4.ID IS NOT NULL OR g.FACLOCID IS NOT NULL OR g.FACASSETID IS NOT NULL)
AND g.LINETYPE = 'Center line'
SELECT g.FACLOCID, g.FACASSETID, g.MAPLABEL, g.Shape, dbo.concat4id(p1.ID, p2.ID, p3.ID, p4.ID) AS PhotoId,
CASE WHEN g.RDFEATTYPE = 'Other' THEN g.RDFEATTYPEOTHER ELSE RDFEATTYPE END +
CASE WHEN g.RDFEATSUBTYPE is NULL THEN '' ELSE ', ' + g.RDFEATSUBTYPE END AS FEATTYPE,
g.RDFEATDESC AS FEATDESC
INTO #Road_Feats
FROM akr_facility2.gis.ROADS_FEATURE_PT_evw as g
LEFT JOIN #PhotoId_A as p1 on p1.ID = g.FACASSETID
LEFT JOIN #PhotoId_F as p2 on p2.ID = g.FEATUREID
LEFT JOIN #PhotoId_G as p3 on p3.ID = g.GEOMETRYID
LEFT JOIN #PhotoId_L as p4 on p4.ID = g.FACLOCID
WHERE (p1.ID IS NOT NULL OR p2.ID IS NOT NULL OR p3.ID IS NOT NULL OR p4.ID IS NOT NULL OR g.FACLOCID IS NOT NULL OR g.FACASSETID IS NOT NULL)
SELECT g.FACLOCID, g.FACASSETID, g.MAPLABEL, g.Shape, dbo.concat4id(p1.ID, p2.ID, p3.ID, p4.ID) AS PhotoId,
CASE WHEN g.ASSETTYPE = 'Other' THEN g.ASSETTYPEOTHDESC ELSE ASSETTYPE END AS FEATTYPE,
g.ASSETDESC AS FEATDESC
INTO #Misc_Pt
FROM akr_facility2.gis.AKR_ASSET_PT_evw as g
LEFT JOIN #PhotoId_A as p1 on p1.ID = g.FACASSETID
LEFT JOIN #PhotoId_F as p2 on p2.ID = g.FEATUREID
LEFT JOIN #PhotoId_G as p3 on p3.ID = g.GEOMETRYID
LEFT JOIN #PhotoId_L as p4 on p4.ID = g.FACLOCID
WHERE (p1.ID IS NOT NULL OR p2.ID IS NOT NULL OR p3.ID IS NOT NULL OR p4.ID IS NOT NULL OR g.FACLOCID IS NOT NULL OR g.FACASSETID IS NOT NULL)
SELECT g.FACLOCID, g.FACASSETID, g.MAPLABEL, g.Shape, dbo.concat4id(p1.ID, p2.ID, p3.ID, p4.ID) AS PhotoId,
CASE WHEN g.ASSETTYPE = 'Other' THEN g.ASSETTYPEOTHDESC ELSE ASSETTYPE END AS FEATTYPE,
g.ASSETDESC AS FEATDESC
INTO #Misc_Py
FROM akr_facility2.gis.AKR_ASSET_PY_evw as g
LEFT JOIN #PhotoId_A as p1 on p1.ID = g.FACASSETID
LEFT JOIN #PhotoId_F as p2 on p2.ID = g.FEATUREID
LEFT JOIN #PhotoId_G as p3 on p3.ID = g.GEOMETRYID
LEFT JOIN #PhotoId_L as p4 on p4.ID = g.FACLOCID
WHERE (p1.ID IS NOT NULL OR p2.ID IS NOT NULL OR p3.ID IS NOT NULL OR p4.ID IS NOT NULL OR g.FACLOCID IS NOT NULL OR g.FACASSETID IS NOT NULL)
SELECT g.FACLOCID, g.FACASSETID, g.MAPLABEL, g.Shape, dbo.concat4id(p1.ID, p2.ID, p3.ID, p4.ID) AS PhotoId,
CASE WHEN g.ASSETTYPE = 'Other' THEN g.ASSETTYPEOTHDESC ELSE ASSETTYPE END AS FEATTYPE,
g.ASSETDESC AS FEATDESC
INTO #Misc_Ln
FROM akr_facility2.gis.AKR_ASSET_LN_evw as g
LEFT JOIN #PhotoId_A as p1 on p1.ID = g.FACASSETID
LEFT JOIN #PhotoId_F as p2 on p2.ID = g.FEATUREID
LEFT JOIN #PhotoId_G as p3 on p3.ID = g.GEOMETRYID
LEFT JOIN #PhotoId_L as p4 on p4.ID = g.FACLOCID
WHERE (p1.ID IS NOT NULL OR p2.ID IS NOT NULL OR p3.ID IS NOT NULL OR p4.ID IS NOT NULL OR g.FACLOCID IS NOT NULL OR g.FACASSETID IS NOT NULL)
-------------------------
--
-- facilities.csv
--
-------------------------
-- Facilities in GIS matching FMSS Location records or having a photo and but no FACASSETID (selected in assets below)
SELECT
-- GIS Attributes
g.Kind,
g.FACLOCID + CASE WHEN f.[Type] = 'SALVAGE' THEN ' (Salvage)' ELSE '' END AS ID,
COALESCE(g.MAPLABEL, '') AS [Name],
g.Latitude, g.Longitude,
g.Photo_Id,
-- FMSS Location Attributes
COALESCE(FORMAT(TRY_CAST(f.CRV AS FLOAT), 'C', 'en-us'), 'Unknown') AS CRV,
COALESCE(FORMAT(TRY_CAST(f.DM AS FLOAT), 'C', 'en-us'), 'Unknown') AS DM,
COALESCE(CONVERT(varchar, YEAR(GetDate()) - TRY_CONVERT(INT, f.YearBlt)) + ' yrs', YearBlt) AS Age,
f.Description AS [Desc],
COALESCE(COALESCE(f.PARKNAME, f.PARKNUMB), '') AS [Park_Id],
f.Qty + ' ' + f.UM + g.Size as Size,
f.Parent, f.Status AS [Status]
FROM
akr_facility2.dbo.FMSSExport AS f
RIGHT JOIN
(
-- Buildings (Center Point)
SELECT
'Building' AS Kind,
FACLOCID, MAPLABEL,
Shape.STY AS Latitude, Shape.STX AS Longitude,
'' as Size,
PhotoId AS Photo_Id
FROM
#Buildings
WHERE
FACASSETID IS NULL
UNION ALL
-- Parking Lots (Centroid)
SELECT
'Parking' AS Kind,
FACLOCID, MAPLABEL,
Shape.STCentroid().STY AS Latitude, Shape.STCentroid().STX AS Longitude,
' (GIS: '+FORMAT(GEOGRAPHY::STGeomFromText(shape.STAsText(),4269).STArea() * 3.28084 * 3.28084,'N0') + 'sf)' as Size,
PhotoId AS Photo_Id
FROM
#Parking
WHERE
FACASSETID IS NULL
UNION ALL
SELECT DISTINCT
-- road features (asserts by default, must have a FACLOCID)
'Road' AS Kind,
FACLOCID,
FEATTYPE + CASE WHEN FEATDESC IS NULL THEN ' (' + FEATDESC + ')' ELSE '' END AS MAPLABEL,
Shape.STY AS Latitude, Shape.STX AS Longitude,
'' AS Size,
PhotoID AS Photo_Id
FROM
#Road_Feats
WHERE
FACASSETID IS NULL AND PhotoId IS NULL
UNION ALL
SELECT DISTINCT
-- trail features (asserts by default, must have a FACLOCID)
'Trail' AS Kind,
FACLOCID,
FEATTYPE + CASE WHEN FEATDESC IS NULL THEN ' (' + FEATDESC + ')' ELSE '' END AS MAPLABEL,
Shape.STY AS Latitude, Shape.STX AS Longitude,
'' AS Size,
PhotoID AS Photo_Id
FROM
#Trail_Feats
WHERE
FACASSETID IS NULL AND PhotoId IS NULL
-- SKIP trail attributes - Will only be an asset (FACLOCID is used as a foreign key to trail)
UNION ALL
-- Trails (All start and end points for a given FACLOCID that are not coincident
-- with another end or start point respectively)
SELECT
'Trail' AS Kind,
g1.FACLOCID, g1.MAPLABEL,
g1.Latitude, g1.Longitude,
'(GIS: '+FORMAT(g2.Feet,'N0') + 'ft)' as Size,
g1.PhotoId AS Photo_Id
FROM (
SELECT
FACLOCID, PhotoId, MAPLABEL,
Latitude, Longitude
FROM (
SELECT
FACLOCID, PhotoId, MAPLABEL,
Shape.STStartPoint().STY AS Latitude, Shape.STStartPoint().STX AS Longitude
FROM
#Trails
WHERE
FACASSETID IS NULL AND ISBRIDGE <> 'Yes'
UNION ALL
SELECT
FACLOCID, PhotoId, MAPLABEL,
Shape.STEndPoint().STY AS Latitude, Shape.STEndPoint().STX AS Longitude
FROM
#Trails
WHERE
FACASSETID IS NULL AND ISBRIDGE <> 'Yes'
) AS temp
GROUP BY
FACLOCID, PhotoId, MAPLABEL, Latitude, Longitude
HAVING
COUNT(*) = 1
) AS g1
JOIN (
SELECT
FACLOCID, PhotoId, MAPLABEL,
SUM(GEOGRAPHY::STGeomFromText(shape.STAsText(),4269).STLength()) * 3.28084 as Feet
FROM
#Trails
WHERE
FACASSETID IS NULL AND ISBRIDGE <> 'Yes'
GROUP BY
FACLOCID, PhotoId, MAPLABEL
) AS g2
ON COALESCE(g1.FACLOCID,'') = COALESCE(g2.FACLOCID,'')
AND COALESCE(g1.PhotoId,'') = COALESCE(g2.PhotoId,'')
AND COALESCE(g1.MAPLABEL,'') = COALESCE(g2.MAPLABEL,'')
UNION ALL
-- Roads (All start and end points for a given FACLOCID that are not coincident
-- with another end or start point respectively)
SELECT
'Road' AS Kind,
g1.FACLOCID, g1.MAPLABEL,
g1.Latitude, g1.Longitude,
' (GIS: '+FORMAT(g2.Miles,'N2') + 'mi)' as Size,
g1.PhotoId AS Photo_Id
FROM (
SELECT
FACLOCID, PhotoId, MAPLABEL,
Latitude, Longitude
FROM (
SELECT
FACLOCID, PhotoId, MAPLABEL,
Shape.STStartPoint().STY AS Latitude, Shape.STStartPoint().STX AS Longitude
FROM
#Roads
WHERE
FACASSETID IS NULL AND ISBRIDGE <> 'Yes'
UNION ALL
SELECT
FACLOCID, PhotoId, MAPLABEL,
Shape.STEndPoint().STY AS Latitude, Shape.STEndPoint().STX AS Longitude
FROM
#Roads
WHERE
FACASSETID IS NULL AND ISBRIDGE <> 'Yes'
) AS temp
GROUP BY
FACLOCID, PhotoId, MAPLABEL, Latitude, Longitude
HAVING
COUNT(*) = 1
) AS g1
JOIN (
SELECT
FACLOCID, PhotoId, MAPLABEL,
SUM(GEOGRAPHY::STGeomFromText(shape.STAsText(),4269).STLength()) * 0.000621371 as Miles
FROM
#Roads
WHERE
FACASSETID IS NULL AND ISBRIDGE <> 'Yes'
GROUP BY
FACLOCID, PhotoId, MAPLABEL
) AS g2
ON COALESCE(g1.FACLOCID,'') = COALESCE(g2.FACLOCID,'')
AND COALESCE(g1.PhotoId,'') = COALESCE(g2.PhotoId,'') AND COALESCE(g1.MAPLABEL,'') = COALESCE(g2.MAPLABEL,'')
UNION ALL
-- Trail Bridges (Middle vertex, or average of two middle vertices)
SELECT
'Bridge' AS Kind,
FACLOCID, MAPLABEL,
-- Mid point of bridge
CASE
WHEN
(Shape.STNumPoints() % 2) = 0
THEN --Even number of vertices
(Shape.STPointN(Shape.STNumPoints()/2).STY + Shape.STPointN(1 + Shape.STNumPoints()/2).STY)/2.0
ELSE -- Odd
Shape.STPointN(1 + Shape.STNumPoints()/2).STY
END AS Latitude,
CASE
WHEN
(Shape.STNumPoints() % 2) = 0
THEN --Even
(Shape.STPointN(Shape.STNumPoints()/2).STX + Shape.STPointN(1 + Shape.STNumPoints()/2).STX)/2.0
ELSE -- Odd
Shape.STPointN(1 + Shape.STNumPoints()/2).STX
END AS Longitude,
'' AS Size,
PhotoId AS Photo_Id
FROM
#Trails
WHERE
FACASSETID IS NULL AND ISBRIDGE = 'Yes'
UNION ALL
-- Road Bridges (Middle vertex, or average of two middle vertices)
SELECT
'Bridge' AS Kind,
FACLOCID, MAPLABEL,
-- Get mid point of bridge
CASE
WHEN
(Shape.STNumPoints() % 2) = 0
THEN --Even number of vertices
(Shape.STPointN(Shape.STNumPoints()/2).STY + Shape.STPointN(1 + Shape.STNumPoints()/2).STY)/2.0
ELSE -- Odd
Shape.STPointN(1 + Shape.STNumPoints()/2).STY
END AS Latitude,
CASE
WHEN
(Shape.STNumPoints() % 2) = 0
THEN --Even
(Shape.STPointN(Shape.STNumPoints()/2).STX + Shape.STPointN(1 + Shape.STNumPoints()/2).STX)/2.0
ELSE -- Odd
Shape.STPointN(1 + Shape.STNumPoints()/2).STX
END AS Longitude,
'' AS Size,
PhotoId AS Photo_Id
FROM
#Roads
WHERE
FACASSETID IS NULL AND ISBRIDGE = 'Yes'
UNION ALL
-- Miscellaneous points (Center Point)
SELECT
'Misc' AS Kind,
FACLOCID, MAPLABEL,
Shape.STY AS Latitude, Shape.STX AS Longitude,
'' as Size,
PhotoId AS Photo_Id
FROM
#Misc_Pt
WHERE
FACASSETID IS NULL
UNION ALL
-- Miscellaneous polygons (centroid)
SELECT
'Misc' AS Kind,
FACLOCID, MAPLABEL,
Shape.STCentroid().STY AS Latitude, Shape.STCentroid().STX AS Longitude,
'' as Size,
PhotoId AS Photo_Id
FROM
#Misc_Py
WHERE
FACASSETID IS NULL
UNION ALL
-- Miscellaneous lines (All start and end points for a given FACASSETID that are not coincident
-- with another end or start point respectively)
SELECT
'Misc' AS Kind,
g1.FACLOCID, g1.MAPLABEL,
g1.Latitude, g1.Longitude,
'' as Size,
g1.PhotoId AS Photo_Id
FROM (
SELECT
FACLOCID, PhotoId, MAPLABEL, FEATTYPE,
Latitude, Longitude
FROM (
SELECT
FACLOCID, PhotoId, MAPLABEL, FEATTYPE,
Shape.STStartPoint().STY AS Latitude, Shape.STStartPoint().STX AS Longitude
FROM
#Misc_Ln
WHERE
FACASSETID IS NULL
UNION ALL
SELECT
FACLOCID, PhotoId, MAPLABEL, FEATTYPE,
Shape.STEndPoint().STY AS Latitude, Shape.STEndPoint().STX AS Longitude
FROM
#Misc_Ln
WHERE
FACASSETID IS NULL
) AS temp
GROUP BY
FACLOCID, PhotoId, MAPLABEL, FEATTYPE, Latitude, Longitude
HAVING
COUNT(*) = 1
) AS g1
) AS g
ON
g.FACLOCID = f.Location
-------------------------
--
-- assets.csv
--
-------------------------
-- Items in GIS matching FMSS Asset records
SELECT
-- GIS
g.Kind,
COALESCE(g.FACASSETID, 'N/A') AS ID,
g.[Name] AS [Name],
g.Photo_Id,
g.Latitude, g.Longitude,
-- FMSS
f.Location AS Parent,
COALESCE(f.[Description], COALESCE(g.[Desc], 'Not an FMSS Asset')) AS [Desc]
FROM
akr_facility2.dbo.FMSSExport_Asset AS f
RIGHT JOIN
(
SELECT DISTINCT
-- road features (asserts by default, has a photoID or FACASSETID)
'Road' AS Kind,
FACASSETID,
PhotoID AS Photo_Id,
FEATTYPE AS [Name],
FEATDESC as [Desc],
Shape.STY AS Latitude, Shape.STX AS Longitude
FROM
#Road_Feats
WHERE
FACASSETID IS NOT NULL OR PhotoId IS NOT NULL
UNION ALL
SELECT DISTINCT
-- trail features (asserts by default, has a photoID or FACASSETID)
'Trail' AS Kind,
FACASSETID,
PhotoID AS Photo_Id,
FEATTYPE AS [Name],
FEATDESC as [Desc],
Shape.STY AS Latitude, Shape.STX AS Longitude
FROM
#Trail_Feats
WHERE
FACASSETID IS NOT NULL OR PhotoId IS NOT NULL
UNION ALL
-- trail attributes (surface material, etc) (asserts by default, has a photoID or FACASSETID)
SELECT DISTINCT
'Trail' AS Kind,
FACASSETID,
PhotoID AS Photo_Id,
ATTTYPE AS [Name],
ATTDESC as [Desc],
Shape.STY AS Latitude, Shape.STX AS Longitude
FROM
#Trail_Atts
WHERE
FACASSETID IS NOT NULL OR PhotoId IS NOT NULL
UNION ALL
-- Buildings (Center Point) - Typically out-buildings that are grouped with a main structure
SELECT
'Building' AS Kind,
FACASSETID,
PhotoId AS Photo_Id,
MAPLABEL AS [Name],
BLDGTYPE as [Desc],
Shape.STY AS Latitude, Shape.STX AS Longitude
FROM
#Buildings
WHERE
FACASSETID IS NOT NULL
UNION ALL
-- Parking Lots (Centroid)
SELECT
'Parking' AS Kind,
FACASSETID,
PhotoId AS Photo_Id,
MAPLABEL AS [Name],
'' as [Desc],
--' (GIS: '+FORMAT(GEOGRAPHY::STGeomFromText(shape.STAsText(),4269).STArea() * 3.28084 * 3.28084,'N0') + 'sf)' as Size,
Shape.STCentroid().STY AS Latitude, Shape.STCentroid().STX AS Longitude
FROM
#Parking
WHERE
FACASSETID IS NOT NULL
UNION ALL
-- Roads (All start and end points for a given FACASSETID that are not coincident
-- with another end or start point respectively)
SELECT
'Road' AS Kind,
g1.FACASSETID,
g1.PhotoId AS Photo_Id,
g1.MAPLABEL AS [Name],
'' AS [Desc],
-- ' (GIS: '+FORMAT(g2.Miles,'N2') + 'mi)' as Size,
g1.Latitude, g1.Longitude
FROM (
SELECT
FACASSETID, PhotoId, MAPLABEL,
Latitude, Longitude
FROM (
SELECT
FACASSETID, PhotoId, MAPLABEL,
Shape.STStartPoint().STY AS Latitude, Shape.STStartPoint().STX AS Longitude
FROM
#Roads
WHERE
FACASSETID IS NOT NULL AND ISBRIDGE <> 'Yes'
UNION ALL
SELECT
FACASSETID, PhotoId, MAPLABEL,
Shape.STEndPoint().STY AS Latitude, Shape.STEndPoint().STX AS Longitude
FROM
#Roads
WHERE
FACASSETID IS NOT NULL AND ISBRIDGE <> 'Yes'
) AS temp
GROUP BY
FACASSETID, PhotoId, MAPLABEL, Latitude, Longitude
HAVING
COUNT(*) = 1
) AS g1
JOIN (
SELECT
FACASSETID, PhotoId, MAPLABEL,
SUM(GEOGRAPHY::STGeomFromText(shape.STAsText(),4269).STLength()) * 0.000621371 as Miles
FROM
#Roads
WHERE
FACASSETID IS NOT NULL AND ISBRIDGE <> 'Yes'
GROUP BY
FACASSETID, PhotoId, MAPLABEL
) AS g2
ON g1.FACASSETID = g2.FACASSETID
AND COALESCE(g1.PhotoId,'') = COALESCE(g2.PhotoId,'') AND COALESCE(g1.MAPLABEL,'') = COALESCE(g2.MAPLABEL,'')
UNION ALL
-- Trails (All start and end points for a given FACASSETID that are not coincident
-- with another end or start point respectively)
SELECT
'Trail' AS Kind,
g1.FACASSETID,
g1.PhotoId AS Photo_Id,
g1.MAPLABEL AS [Name],
'' AS [Desc],
-- '(GIS: '+FORMAT(g2.Feet,'N0') + 'ft)' as Size,
g1.Latitude, g1.Longitude
FROM (
SELECT
FACASSETID, PhotoId, MAPLABEL,
Latitude, Longitude
FROM (
SELECT
FACASSETID, PhotoId, MAPLABEL,
Shape.STStartPoint().STY AS Latitude, Shape.STStartPoint().STX AS Longitude
FROM
#Trails
WHERE
FACASSETID IS NOT NULL AND ISBRIDGE <> 'Yes'
UNION ALL
SELECT
FACASSETID, PhotoId, MAPLABEL,
Shape.STEndPoint().STY AS Latitude, Shape.STEndPoint().STX AS Longitude
FROM
#Trails
WHERE
FACASSETID IS NOT NULL AND ISBRIDGE <> 'Yes'
) AS temp
GROUP BY
FACASSETID, PhotoId, MAPLABEL, Latitude, Longitude
HAVING
COUNT(*) = 1
) AS g1
JOIN (
SELECT
FACASSETID, PhotoId, MAPLABEL,
SUM(GEOGRAPHY::STGeomFromText(shape.STAsText(),4269).STLength()) * 3.28084 as Feet
FROM
#Trails
WHERE
FACASSETID IS NOT NULL AND ISBRIDGE <> 'Yes'
GROUP BY
FACASSETID, PhotoId, MAPLABEL
) AS g2
ON g1.FACASSETID = g2.FACASSETID
AND COALESCE(g1.PhotoId,'') = COALESCE(g2.PhotoId,'') AND COALESCE(g1.MAPLABEL,'') = COALESCE(g2.MAPLABEL,'')
UNION ALL
-- Trail Bridges (Middle vertex, or average of two middle vertices)
SELECT
'Bridge' AS Kind,
FACASSETID,
PhotoId AS Photo_Id,
MAPLABEL AS [Name],
'' AS [Desc],
-- '' AS Size,
-- Mid point of bridge
CASE
WHEN
(Shape.STNumPoints() % 2) = 0
THEN --Even number of vertices
(Shape.STPointN(Shape.STNumPoints()/2).STY + Shape.STPointN(1 + Shape.STNumPoints()/2).STY)/2.0
ELSE -- Odd
Shape.STPointN(1 + Shape.STNumPoints()/2).STY
END AS Latitude,
CASE
WHEN
(Shape.STNumPoints() % 2) = 0
THEN --Even
(Shape.STPointN(Shape.STNumPoints()/2).STX + Shape.STPointN(1 + Shape.STNumPoints()/2).STX)/2.0
ELSE -- Odd
Shape.STPointN(1 + Shape.STNumPoints()/2).STX
END AS Longitude
FROM
#Trails
WHERE
FACASSETID IS NOT NULL AND ISBRIDGE = 'Yes'
UNION ALL
-- Road Bridges (Middle vertex, or average of two middle vertices)
SELECT
'Bridge' AS Kind,
FACASSETID,
PhotoId AS Photo_Id,
MAPLABEL AS [Name],
'' AS [Desc],
-- '' AS Size,
-- Get mid point of bridge
CASE
WHEN
(Shape.STNumPoints() % 2) = 0
THEN --Even number of vertices
(Shape.STPointN(Shape.STNumPoints()/2).STY + Shape.STPointN(1 + Shape.STNumPoints()/2).STY)/2.0
ELSE -- Odd
Shape.STPointN(1 + Shape.STNumPoints()/2).STY
END AS Latitude,
CASE
WHEN
(Shape.STNumPoints() % 2) = 0
THEN --Even
(Shape.STPointN(Shape.STNumPoints()/2).STX + Shape.STPointN(1 + Shape.STNumPoints()/2).STX)/2.0
ELSE -- Odd
Shape.STPointN(1 + Shape.STNumPoints()/2).STX
END AS Longitude
FROM
#Roads
WHERE
FACASSETID IS NOT NULL AND ISBRIDGE = 'Yes'
UNION ALL
-- Miscellaneous points (Center Point)
SELECT
'Misc' AS Kind,
FACASSETID,
PhotoId AS Photo_Id,
MAPLABEL AS [Name],
'' AS [Desc],
Shape.STY AS Latitude, Shape.STX AS Longitude
FROM
#Misc_Pt
WHERE
FACASSETID IS NOT NULL
UNION ALL
-- Miscellaneous polygons (centroid)
SELECT
'Misc' AS Kind,
FACASSETID,
PhotoId AS Photo_Id,
MAPLABEL AS [Name],
'' AS [Desc],
Shape.STCentroid().STY AS Latitude, Shape.STCentroid().STX AS Longitude
FROM
#Misc_Py
WHERE
FACASSETID IS NOT NULL
UNION ALL
-- Miscellaneous lines (All start and end points for a given FACASSETID that are not coincident
-- with another end or start point respectively)
SELECT
'Misc' AS Kind,
g1.FACASSETID,
g1.PhotoId AS Photo_Id,
g1.MAPLABEL AS [Name],
g1.FEATTYPE AS [Desc],
g1.Latitude, Longitude
FROM (
SELECT
FACASSETID, PhotoId, MAPLABEL, FEATTYPE,
Latitude, Longitude
FROM (
SELECT
FACASSETID, PhotoId, MAPLABEL, FEATTYPE,
Shape.STStartPoint().STY AS Latitude, Shape.STStartPoint().STX AS Longitude
FROM
#Misc_Ln
WHERE
FACASSETID IS NOT NULL
UNION ALL
SELECT
FACASSETID, PhotoId, MAPLABEL, FEATTYPE,
Shape.STEndPoint().STY AS Latitude, Shape.STEndPoint().STX AS Longitude
FROM
#Misc_Ln
WHERE
FACASSETID IS NOT NULL
) AS temp
GROUP BY
FACASSETID, PhotoId, MAPLABEL, FEATTYPE, Latitude, Longitude
HAVING
COUNT(*) = 1
) AS g1
) AS g
ON
g.FACASSETID = f.Asset
DROP TABLE #PhotoId_A;
DROP TABLE #PhotoId_F;
DROP TABLE #PhotoId_G;
DROP TABLE #PhotoId_L;
DROP TABLE #Buildings;
DROP TABLE #Parking;
DROP TABLE #Trails;
DROP TABLE #Trail_Feats
DROP TABLE #Trail_Atts
DROP TABLE #Roads;
DROP TABLE #Road_Feats
DROP TABLE #Misc_Pt
DROP TABLE #Misc_Py
DROP TABLE #Misc_Ln
-------------------------
--
-- parents.csv
--
-------------------------
SELECT Parent, Location, Description, Asset_Code
FROM FMSSExport
WHERE Type <> 'SALVAGE'
AND Parent IS NOT NULL
ORDER BY Parent, Location
-------------------------
--
-- all_assets.csv
--
-------------------------
SELECT Location, Asset, Description
FROM FMSSExport_Asset
WHERE [Description] NOT LIKE '%REMOVED%'
AND [Location] IS NOT NULL
ORDER BY [Location]