-
Notifications
You must be signed in to change notification settings - Fork 1
/
surfaces.py
1004 lines (876 loc) · 32.4 KB
/
surfaces.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
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
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import abc
import concurrent.futures
import concurrent.futures
import multiprocessing
import warnings
from typing import Type, Union, Optional, Iterable, Collection
from weakref import WeakKeyDictionary
import geopandas as gpd
import numpy as np
import rasterstats
import shapely.geometry.base
from geopandas import GeoDataFrame
from geopandas import GeoSeries
from pandas import Series, DataFrame
from util_raster import get_utm_from_lon_lat, get_raster_path, get_raster_size, overlay
warnings.filterwarnings('ignore', '.*PyGEOS.*')
import osmium
import pandas as pd
import functools
import itertools
import os
import math
import tempfile
from typing import Iterator
import pyrosm
def osmium_extract(
file: str,
osmium_executable_path: str,
bbox: list[float],
bbox_latlon=True
) -> str:
if bbox_latlon:
bbox = [bbox[1], bbox[0], bbox[3], bbox[2]]
string: str = ','.join(str(b) for b in bbox)
name = file.rpartition(os.sep)[2] if os.sep in file else file
tempdir = tempfile.gettempdir()
if not os.path.exists(tempdir):
os.makedirs(tempdir)
temp = os.path.join(tempdir, name)
os.system(f'{osmium_executable_path} extract -b {string} {file} -o {temp} --overwrite')
return temp
@functools.singledispatch
def pyrosm_extract(
source: str,
osmium_executable_path: str = None,
bbox: Optional[list[float]] = None,
bbox_latlon=True
) -> str:
path = pyrosm.get_data(source)
if bbox:
path = osmium_extract(path, osmium_executable_path, bbox, bbox_latlon)
return path
@pyrosm_extract.register(list)
def _(
source: list[str],
osmium_executable_path: str = None,
bbox: Union[list[float], None] = None,
bbox_latlon=True
) -> Iterator[str]:
with concurrent.futures.ThreadPoolExecutor() as threads, concurrent.futures.ProcessPoolExecutor() as processes:
files = threads.map(pyrosm.get_data, source)
if bbox is not None:
yield from processes.map(
osmium_extract,
itertools.repeat(osmium_executable_path, len(source)),
bbox,
itertools.repeat(bbox_latlon)
)
else:
yield from files
def gen_zonal_stats(
interface: Union[dict, GeoSeries, GeoDataFrame, str],
raster: str,
**kwargs
) -> np.ndarray:
"""
:param interface: dict with 'features' key OR geopandas object OR shapely file path
:param raster: .tif file path
:param kwargs: kwargs to pass to rasterstats.gen_zonal_stats()
:return: np.ndarray
"""
stats: Collection[str] = tuple('min max mean count sum median nodata'.split())
rows = len(interface['features'])
columns = len(stats)
gen = rasterstats.gen_zonal_stats(
interface,
raster,
stats=stats,
# stats=stats,
# nodata=0,
# nodata=-1,
**kwargs
)
# chain = itertools.chain.from_iterable(map(dict.values, gen))
# # # I am assuming that the output dict keys match the order of stats however I am not sure I have this guarantee
# def assertion() -> Iterator[Iterable[float]]:
# for output in gen:
# assert all(a == b for a, b in zip(output.keys(), stats))
# yield from output.values()
# # yield output.values()
#
# chain = assertion()
#
chain = itertools.chain.from_iterable(map(dict.values, gen))
arr = np.fromiter(chain, dtype=np.float64, count=rows * columns)
# arr /= 255
arr = arr.reshape((rows, columns))
return arr
class RasterStats(abc.ABC):
def overlay( self, **kwargs):
overlay(self.rasterstats, self.raster, **kwargs)
@property
def raster(self) -> str:
return self._raster()
@abc.abstractmethod
def _raster(self) -> str:
...
@classmethod
@abc.abstractmethod
def _rasterstats_from_file(
cls,
file: str,
shadow_dir: str,
zoom: int,
threshold: tuple[float, float],
mask: Optional[list[float]],
raster: Optional[str],
) -> GeoDataFrame:
...
@classmethod
def rasterstats_from_file(
cls,
file: Union[str, list[str]],
shadow_dir: str,
zoom: int,
threshold: tuple[float, float] = (0.0, 1.0),
mask: Optional[list[float]] = None,
raster: Optional[str] = None,
) -> GeoDataFrame:
"""
:param file: .pbf file or pyrosm source
:param shadow_dir: directory of all shadow xtiles and ytiles
:param zoom: slippy map zoom
:param threshold: cut-off threshold for array values
:param mask: [miny, minx, maxy, maxx] to restrict raster array
:param raster: optional raster file that will be used instead of generating a raster for the file
:return: GeoDataFrame
geometry as original lines, even though the area is a 4m buffer
"""
return cls._rasterstats_from_file(
file,
shadow_dir=shadow_dir,
zoom=zoom,
threshold=threshold,
mask=mask,
raster=raster,
)
@abc.abstractmethod
def _rasterstats(self) -> GeoDataFrame:
...
@property
def rasterstats(self, ) -> GeoDataFrame:
return self._rasterstats()
@classmethod
@abc.abstractmethod
def _rastersize_from_file(
cls,
file: Union[str, Iterable[str]],
zoom: int,
mask: Optional[list[float]]
) -> int:
...
def rastersize(
self,
zoom: int,
mask: Optional[list[float]] = None
) -> int:
"""
:param zoom: slippy tile zoom
:param mask: mask to restrict raster [miny, minx, maxy, maxx]
:return: raster array size in bytes
"""
return self._rastersize(zoom, mask)
@abc.abstractmethod
def _rastersize(self, zoom: int, mask: Optional[list[float]]):
...
@classmethod
def rastersize_from_file(
cls,
file: Union[str, Iterable[str]],
zoom: int,
mask: Optional[list[float]] = None,
) -> int:
"""
:param file: osm.pbf file,
:param zoom: slippy tile zoom
:param mask: mask to restrict raster [miny, minx, maxy, maxx]
:return: raster array size in bytes
"""
return cls._rastersize_from_file(file, zoom=zoom, mask=mask)
@classmethod
def rastersize_from_files(
cls,
files: Iterable[str],
zoom: int,
mask: Optional[Iterable[list[float]]] = None
) -> dict[str, int]:
"""
:param files: osm.pbf files
:param zoom: slippy tile zoom
:param mask: masks to restrict rasters [[miny, minx, maxy, maxx]]
:return: {file: raster array size}
"""
if mask is None:
mask = itertools.repeat(None)
return {
f: cls._rastersize_from_file(f, zoom=zoom, mask=m)
for f, m in zip(files, mask)
}
class MetaRasterOsmium(type(osmium.SimpleHandler), type(abc.ABC), metaclass=type):
"""
Metaclasses are typically too complicated to be necessary, but the polymorphism of inheriting from both
osmium.SimpleHandler and RasterStats requires a metaclass that inherits from both of the respective metaclasses,
Because nothing is implemented here, it inherits __new__ and __init__ directly from osmium.SimpleHandler
"""
class DescriptorParks(osmium.SimpleHandler, RasterStats, metaclass=MetaRasterOsmium):
wkbfab = osmium.geom.WKBFactory()
def _raster(self) -> str:
surfaces = self._surfaces
return get_raster_path(
*self.rasterstats.total_bounds,
basedir=surfaces.shadow_dir,
threshold=surfaces.threshold,
mask=surfaces.mask,
zoom=surfaces.zoom
)
def _rastersize(self, zoom: int, mask: Optional[list[float]]):
gdf = self.gdf
return get_raster_size(*gdf.total_bounds, zoom=zoom, mask=mask)
@classmethod
def _rastersize_from_file(
cls,
file: Union[str, Iterable[str]],
zoom: int,
mask: Optional[list[float]],
) -> int:
if '.' not in file:
file = pyrosm.get_data(file)
surfaces = Surfaces(file, shadow_dir='', zoom=zoom, mask=mask)
gdf = surfaces.parks.gdf
return get_raster_size(*gdf.total_bounds, zoom=zoom, mask=mask)
def _rasterstats(self) -> GeoDataFrame:
surfaces = self._surfaces
if surfaces in self._rasterstats_:
return self._rasterstats_[surfaces]
gdf = self.gdf
mask = surfaces.mask
if mask is not None:
box = shapely.geometry.box(mask[1], mask[0], mask[3], mask[2])
gdf = gdf.clip(box)
raster = get_raster_path(
*gdf.total_bounds,
zoom=surfaces.zoom,
basedir=surfaces.shadow_dir,
threshold=surfaces.threshold,
outpath=surfaces.raster,
mask=surfaces.mask,
)
# Some geometry can be None and rasterstats will raise an exception
# loc = gdf['geometry'].notna()
# geometry: GeoSeries = gdf.loc[loc, 'geometry']
gdf = gdf[gdf['geometry'].notna()]
geometry = gdf.geometry
step = math.ceil(len(geometry) / multiprocessing.cpu_count())
slices = [
slice(left, left + step)
for left in range(0, len(geometry), step)
]
interfaces = [
getattr(geometry.iloc[s], '__geo_interface__')
for s in slices
]
with concurrent.futures.ThreadPoolExecutor() as threads:
results = threads.map(gen_zonal_stats, interfaces, itertools.repeat(raster))#, itertools.repeat(stats))
results = list(results)
arr = np.concatenate(results)
stats: Collection[str] = tuple('min max mean count sum median nodata'.split())
data = {
stat: arr[:, i]
for i, stat in enumerate(stats)
}
centroid: shapely.geometry.Point = geometry.iloc[0].centroid
lon = centroid.x
lat = centroid.y
crs = get_utm_from_lon_lat(lon, lat)
area = (
geometry.to_crs(crs)
.area
.values
)
data['area'] = area
data['name'] = gdf['name'].values
result = GeoDataFrame(
data=data,
index=gdf.index,
crs=gdf.crs,
geometry=gdf.geometry,
)
stats: Collection[str] = tuple('min max mean count sum median nodata'.split())
normalize = set('min max mean sum median'.split())
asint = {'nodata', 'count'}
for stat in stats:
if stat in normalize:
result[stat] = result[stat] / 255
elif stat in asint:
result[stat] = result[stat].astype('Int64')
result['name'] = Series.astype(result['name'], 'string')
self._rasterstats_[surfaces] = result
return result
@classmethod
def _rasterstats_from_file(
cls,
file: str,
shadow_dir: str,
zoom: int,
threshold: tuple[float, float],
mask: Optional[list[float]],
raster: Optional[str],
) -> GeoDataFrame:
if '.' not in file:
file = pyrosm.get_data(file)
surfaces = Surfaces(file, shadow_dir, zoom, threshold, mask, raster)
parks = surfaces.parks
return parks.rasterstats
#
# gdf = parks.gdf
# raster = get_raster_path(
# *gdf.total_bounds,
# zoom=zoom,
# basedir=shadow_dir,
# threshold=threshold,
# outpath=raster,
# mask=mask,
# )
#
# # Some geometry can be None and rasterstats will raise an exception
# # loc = gdf['geometry'].notna()
# # geometry: GeoSeries = gdf.loc[loc, 'geometry']
# gdf = gdf[gdf['geometry'].notna()]
# geometry = gdf.geometry
#
# step = math.ceil(len(geometry) / multiprocessing.cpu_count())
# slices = [
# slice(l, l + step)
# for l in range(0, len(geometry), step)
# ]
# interfaces = [
# getattr(geometry.iloc[s], '__geo_interface__')
# for s in slices
# ]
#
# with concurrent.futures.ProcessPoolExecutor() as processes:
# results = processes.map(gen_zonal_stats, interfaces, itertools.repeat(raster))
# results = list(results)
# arr = np.concatenate(results)
#
# data = {
# stat: arr[:, i]
# for i, stat in enumerate(stats)
# }
#
# centroid: shapely.geometry.Point = geometry.iloc[0].centroid
# lon = centroid.x
# lat = centroid.y
# crs = get_utm_from_lon_lat(lon, lat)
# area = (
# geometry.to_crs(crs)
# .area
# .values
# )
# data['area'] = area
# data['name'] = gdf['name'].values
#
# result = GeoDataFrame(
# data=data,
# index=gdf.index,
# crs=gdf.crs,
# geometry=gdf.geometry,
# )
# stats: Collection[str] = tuple('min max mean count sum median nodata'.split())
# normalize = set('min max mean sum median'.split())
# asint = {'nodata', 'count'}
# for stat in stats:
# if stat in normalize:
# result[stat] = result[stat] / 255
# elif stat in asint:
# result[stat] = result[stat].astype('Int64')
#
# result['name'] = Series.astype(result['name'], 'string')
# return result
#
def __init__(self):
self._rasterstats_: WeakKeyDictionary[Surfaces, GeoDataFrame] = WeakKeyDictionary()
super(DescriptorParks, self).__init__()
self.natural = {'wood', 'grass'}
self.land_use = {
'wood', 'grass', 'forest', 'orchard', 'village_green',
'vineyard', 'cemetery', 'meadow', 'village_green',
}
self.leisure = {
'dog_park', 'park', 'playground', 'recreation_ground',
}
self.name = {}
self.geometry = {}
self.ways = set()
self._cache: WeakKeyDictionary[Surfaces, GeoDataFrame] = WeakKeyDictionary()
self._bbox: WeakKeyDictionary[Surfaces, list[float]] = WeakKeyDictionary()
def area(self, a: osmium.osm.Area):
# TODO: What about nodes marked 'point of interest'?
tags: osmium.osm.TagList = a.tags
# Qualifiers
if not (
tags.get('natural', None) in self.natural
or tags.get('land_use', None) in self.land_use
or tags.get('leisure', None) in self.leisure
):
return
id_ = a.orig_id()
if a.from_way():
self.ways.add(id_)
try:
self.geometry[id_] = self.wkbfab.create_multipolygon(a)
except RuntimeError:
...
if 'name' in tags:
self.name[id_] = tags['name']
def apply_file(self, filename, locations=False, idx='flex_mem'):
for key, value in self.__dict__.items():
if not key.startswith('_') and isinstance(value, dict):
value.clear()
super(DescriptorParks, self).apply_file(filename, locations, idx)
def __get__(self, instance: 'Surfaces', owner: Type['Surfaces']) -> 'DescriptorParks':
self._surfaces = instance
return self
def __set__(self, instance, value):
self._cache[instance] = value
def __delete__(self, instance):
del self._cache[instance]
@property
def gdf(self) -> GeoDataFrame:
surfaces = self._surfaces
if surfaces not in self._cache:
self.apply_file(surfaces.file, locations=True)
index = np.fromiter(self.geometry.keys(), dtype=np.uint64, count=len(self.geometry))
geometry = GeoSeries.from_wkb(list(self.geometry.values()), index=index)
index = np.fromiter(self.name.keys(), dtype=np.uint64, count=len(self.name))
name = np.fromiter(self.name.values(), dtype='U128', count=len(self.name))
name = Series(name, index=index, dtype='string')
index = np.fromiter(self.ways, dtype=np.uint64, count=len(self.ways))
ways = np.full(len(self.ways), True, dtype=bool)
ways = Series(ways, index=index, dtype='boolean')
# if np.all(name.index.values == geometry.index.values):
# raise RuntimeError('you can do this better')
gdf = GeoDataFrame({
'name': name,
'way': ways,
}, crs=4326, geometry=geometry)
gdf.loc[gdf['way'].isna(), 'way'] = False
self.__set__(surfaces, gdf)
return gdf
return self._cache[surfaces]
@gdf.setter
def gdf(self, value):
self._cache[self._surfaces] = value
@gdf.deleter
def gdf(self):
del self._cache[self._surfaces]
class DescriptorNetwork(RasterStats):
network_type: str
def _raster(self) -> str:
surfaces = self.networks.surfaces
return get_raster_path(
*self.rasterstats.total_bounds,
basedir=surfaces.shadow_dir,
threshold=surfaces.threshold,
mask=surfaces.mask,
zoom=surfaces.zoom,
)
def _rastersize(self, zoom: int, mask: Optional[list[float]]):
gdf = self.gdf
return get_raster_size(*gdf.total_bounds, zoom=zoom, mask=mask)
@classmethod
def _rastersize_from_file(
cls,
file: Union[str, Iterable[str]],
zoom: int,
mask: Optional[list[float]],
) -> Union[float, dict[str, float]]:
if '.' not in file:
file = pyrosm.get_data(file)
surfaces = Surfaces(file, shadow_dir='', zoom=zoom, mask=mask)
network: DescriptorNetwork = surfaces.networks.__getattribute__(cls.network_type)
gdf = network.gdf
return get_raster_size(*gdf.total_bounds, zoom=zoom, mask=mask)
def _rasterstats(self) -> GeoDataFrame:
surfaces = self.networks.surfaces
if surfaces in self._rasterstats_:
return self._rasterstats_[surfaces]
gdf = self.gdf
mask = surfaces.mask
if mask is not None:
box = shapely.geometry.box(mask[1], mask[0], mask[3], mask[2])
gdf = gdf.clip(box)
# Some geometry can be None and rasterstats will raise an exception
# This was a significant slip-up. I should have just dropped the NA geometries from the entire GDF
# loc = gdf.geometry.notna()
# geometry: GeoSeries = gdf.loc[loc, 'geometry']
gdf: GeoDataFrame = gdf[gdf.geometry.notna()]
geometry = gdf.geometry
# We are working with lines so we are buffering each line by 4 meters, which is about a car lane
centroid: shapely.geometry.Point = gdf.geometry.iloc[0].centroid
lon = centroid.x
lat = centroid.y
crs = get_utm_from_lon_lat(lon, lat)
buffer = (geometry
.to_crs(crs)
.buffer(2) # was originally a 4 meter buffer, but a lane is 4 meters wide so should be 2 meters
# probably doesn't matter too much
# .buffer(4)
)
area = buffer.area
buffer = buffer.to_crs(4326)
raster = get_raster_path(
*buffer.total_bounds,
zoom=surfaces.zoom,
basedir=surfaces.shadow_dir,
threshold=surfaces.threshold,
outpath=surfaces.raster,
mask=surfaces.mask,
)
step = math.ceil(len(geometry) / multiprocessing.cpu_count())
slices = [
slice(left, left + step)
for left in range(0, len(buffer), step)
]
interfaces = [
getattr(buffer[s], '__geo_interface__')
for s in slices
]
with concurrent.futures.ThreadPoolExecutor() as threads:
results = threads.map(gen_zonal_stats, interfaces, itertools.repeat(raster))#, itertools.repeat(stats))
results = list(results)
arr = np.concatenate(results)
stats: Collection[str] = tuple('min max mean count sum median nodata'.split())
columns = {
stat: arr[:, i]
for i, stat in enumerate(stats)
}
columns['area'] = area
columns['name'] = gdf['name']
result = GeoDataFrame(
columns,
index=gdf.index,
crs=gdf.crs,
geometry=geometry.values
)
normalize = set('min max mean sum median'.split())
asint = {'nodata', 'count'}
for stat in stats:
if stat in normalize:
result[stat] = result[stat] / 255
elif stat in asint:
result[stat] = result[stat].astype('Int64')
result['name'] = Series.astype(result['name'], 'string')
self._rasterstats_[surfaces] = result
return result
@classmethod
def _rasterstats_from_file(
cls,
file: str,
shadow_dir: str,
zoom: int,
threshold: tuple[float, float],
mask: Optional[list[float]],
raster: Optional[str],
) -> GeoDataFrame:
if '.' not in file:
file = pyrosm.get_data(file)
surfaces = Surfaces(file, shadow_dir, zoom, threshold, mask, raster)
network: DescriptorNetwork = surfaces.networks.__getattribute__(cls.network_type)
return network.rasterstats
# network: DescriptorNetwork = surfaces.networks.__getattribute__(cls.network_type)
# return network.rasterstats(zoom, threshold, mask, raster)
# # gdf = network.gdf
#
# # Some geometry can be None and rasterstats will raise an exception
# # This was a significant slip-up. I should have just dropped the NA geometries from the entire GDF
# # loc = gdf.geometry.notna()
# # geometry: GeoSeries = gdf.loc[loc, 'geometry']
# gdf: GeoDataFrame = gdf[gdf.geometry.notna()]
# geometry = gdf.geometry
#
# # We are working with lines so we are buffering each line by 4 meters, which is about a car lane
# centroid: shapely.geometry.Point = gdf.geometry.iloc[0].centroid
# lon = centroid.x
# lat = centroid.y
# crs = get_utm_from_lon_lat(lon, lat)
# buffer = (
# geometry.to_crs(crs)
# .buffer(2) # was originally a 4 meter buffer, but a lane is 4 meters wide so should be 2 meters
# # probably doesn't matter too much
# # .buffer(4)
# )
# area = buffer.area
# buffer = buffer.to_crs(4326)
#
# raster = get_raster_path(
# *buffer.total_bounds,
# zoom=zoom,
# basedir=shadow_dir,
# threshold=threshold,
# outpath=raster,
# mask=mask,
# )
#
# step = math.ceil(len(geometry) / multiprocessing.cpu_count())
# slices = [
# slice(l, l + step)
# for l in range(0, len(buffer), step)
# ]
# interfaces = [
# getattr(buffer[s], '__geo_interface__')
# for s in slices
# ]
#
# with concurrent.futures.ProcessPoolExecutor() as processes:
# results = processes.map(gen_zonal_stats, interfaces, itertools.repeat(raster))
# results = list(results)
#
# arr = np.concatenate(results)
#
# stats: Collection[str] = tuple('min max mean count sum median nodata'.split())
# columns = {
# stat: arr[:, i]
# for i, stat in enumerate(stats)
# }
#
# columns['area'] = area
# columns['name'] = gdf['name']
#
# result = GeoDataFrame(
# columns,
# index=gdf.index,
# crs=gdf.crs,
# geometry=geometry.values
# )
# normalize = set('min max mean sum median'.split())
# asint = {'nodata', 'count'}
# for stat in stats:
# if stat in normalize:
# result[stat] = result[stat] / 255
# elif stat in asint:
# result[stat] = result[stat].astype('Int64')
#
# result['name'] = Series.astype(result['name'], 'string')
# return result
#
def __get__(self, instance: 'DescriptorNetworks', owner: Type['DescriptorNetworks']):
self.networks = instance
return self
def _get_network(self) -> tuple[GeoDataFrame, GeoDataFrame]:
networks = self.networks
surfaces = self.networks.surfaces
osm: pyrosm.OSM = networks._osm[surfaces]
# nodes, geometry = osm.get_network(self.network_type, None, True)
nodes, geometry = None, osm.get_network(self.network_type, None, False)
self._bbox[surfaces] = geometry.total_bounds
nodes: Optional[GeoDataFrame]
geometry: GeoDataFrame
if 'u' in geometry:
geometry = geometry['id name geometry u v length surface tunnel'.split()]
else:
geometry = geometry['id name geometry length tunnel'.split()]
loc = pd.Series.isin(geometry['tunnel'], {'passage', 'yes', 'building_passage', 'covered'})
geometry = geometry.loc[~loc]
geometry = geometry.drop('tunnel', axis=1)
geometry = geometry.set_index('id')
return nodes, geometry
def __init__(self):
self._cache: WeakKeyDictionary[Surfaces, tuple[GeoDataFrame, GeoDataFrame]] = WeakKeyDictionary()
self._bbox: WeakKeyDictionary[Surfaces, list[float]] = WeakKeyDictionary()
self._rasterstats_: WeakKeyDictionary[Surfaces, GeoDataFrame] = WeakKeyDictionary()
@property
def gdf(self) -> GeoDataFrame:
surfaces = self.networks.surfaces
if surfaces not in self._cache:
self._cache[surfaces] = self._get_network()
return self._cache[surfaces][1]
# if instance not in self._cache:
# self._cache[instance] = self._get_network()
# return self._cache[instance][1]
@gdf.setter
def gdf(self, value):
surfaces = self.networks.surfaces
nodes, geometry = self._cache[surfaces]
self._cache[surfaces] = (nodes, value)
@gdf.deleter
def gdf(self):
del self._cache[self.networks.surfaces]
@property
def nodes(self) -> GeoDataFrame:
surfaces = self.networks.surfaces
if surfaces not in self._cache:
self._cache[surfaces] = self._get_network()
return self._cache[surfaces][0]
@nodes.setter
def nodes(self, value):
surfaces = self.networks.surfaces
nodes, geometry = self._cache[surfaces]
self._cache[surfaces] = (value, geometry)
@nodes.deleter
def nodes(self):
del self._cache[self.networks.surfaces]
class DescriptorWalkingNetwork(DescriptorNetwork):
network_type = 'walking'
class DescriptorCyclingNetwork(DescriptorNetwork):
network_type = 'cycling'
class DescriptorDrivingNetwork(DescriptorNetwork):
network_type = 'driving'
class DescriptorDrivingServiceNetwork(DescriptorNetwork):
network_type = 'driving+service'
class DescriptorAllNetwork(DescriptorNetwork):
network_type = 'all'
class DescriptorNetworks:
walking = DescriptorWalkingNetwork()
cycling = DescriptorCyclingNetwork()
driving = DescriptorDrivingNetwork()
driving_service = DescriptorDrivingServiceNetwork()
all = DescriptorAllNetwork()
def __init__(self):
self.surfaces: Optional['Surfaces'] = None
self._osm: WeakKeyDictionary[Surfaces, pyrosm.OSM] = WeakKeyDictionary()
def __get__(self, instance: 'Surfaces', owner):
self.surfaces = instance
if instance is not None and instance not in self._osm:
self._osm[instance] = pyrosm.OSM(self.surfaces.file)
return self
class Surfaces:
parks = DescriptorParks()
networks = DescriptorNetworks()
def __init__(
self,
file: str,
shadow_dir: str,
zoom: int,
threshold: tuple[float, float] = (0.0, 1.0),
mask: Optional[list[float]] = None,
raster: Optional[str] = None,
):
if file.rpartition('.')[2] != 'pbf':
raise ValueError(f"{file=} is not a PBF file")
self.file = file
self.shadow_dir = shadow_dir
self.zoom = zoom
self.threshold = threshold
self.mask = mask
self.raster = raster
@classmethod
def concatenate_from_files(cls, files: list[str]) -> DataFrame:
"""
:param files: list of .feather files that will be concatenated for comparison across cities
:return a single Dataframe, with the OSM ID and filename as index, and statistics as values
"""
names = (
file.rpartition('.')[0]
for file in files
)
def gdfs() -> Iterator[GeoDataFrame]:
it_files = iter(files)
with concurrent.futures.ThreadPoolExecutor() as threads:
prev_future = threads.submit(gpd.read_feather, next(it_files))
try:
next_future = threads.submit(gpd.read_feather, next(it_files))
except StopIteration:
yield prev_future.result()
return
for file in files:
yield prev_future.result()
prev_future = next_future
next_future = threads.submit(gpd.read_feather, file)
yield next_future.result()
concat = pd.concat((
gdf.drop('geometry', axis=1)
.assign(name=next(names))
.set_index('name', append=True)
for gdf in gdfs()
))
return concat
#
if __name__ == '__main__':
mask_ = [41.85784676139911, -87.64350384884648, 41.88852432376579, -87.61311978580892]
loop_path = osmium_extract(
'/home/arstneio/Downloads/chi.osm.pbf',
'~/PycharmProjects/StaticOSM/work/osmium-tool/build/osmium',
[41.85784676139911, -87.64350384884648, 41.88852432376579, -87.61311978580892]
)
driving = Surfaces.networks.driving.rastersize_from_file(
loop_path,
16,
mask=mask_
)
parks = Surfaces.parks.rastersize_from_file(
loop_path,
16,
mask=mask_
)
print()
# network = Surfaces.networks.driving.rasterstats_from_file(
# loop_path,
# '/home/arstneio/Downloads/shadows_new/chi-summer',
# 16,
# )
# print(f'{(time.time() - t) / 60:.1f} minutes')
# network.to_feather('chicago.feather')
# print()
#
# if __name__ == '__main__':
# print('surfaces')
# chicago = pyrosm_extract(
# 'chicago',
# osmium_executable_path='~/PycharmProjects/StaticOSM/work/osmium-tool/build/osmium',
# bbox=[41.865140845410046, -87.634181491039, 41.88789218539278, -87.61083554343192],
# )
# sao_res = Surfaces.networks.driving.rasterstats_from_file(
# chicago,
# '/home/arstneio/Downloads/shadows/test/winter/',
# zoom=16,
# )
# print(sao_res.total_bounds)
#
# london = pyrosm_extract(
# 'london',
# osmium_executable_path='~/PycharmProjects/StaticOSM/work/osmium-tool/build/osmium',
# bbox=[51.48810230578064, -0.02620147457317379, 51.50680415101511, 0.0001485471745494128],
# )
# l_res = Surfaces.networks.driving.rasterstats_from_file(
# london,
# '/home/arstneio/Downloads/shadows/test/winter/',
# zoom=16,
# )
# print(l_res.total_bounds)
# print()
# t = time.time()
# driving = Surfaces.networks.driving.rasterstats_from_file(
# 'london',
# '/home/arstneio/Downloads/shadows/test/winter/',
# zoom=16,
# # threshold=.25
# )
# print(f'parks took {int(time.time() - t)} seconds; {len(parks)=}')
# parks = Surfaces.parks.rasterstats_from_file(
# '/home/arstneio/Downloads/ams.osm.pbf',
# '/home/arstneio/Downloads/shadows/test/winter/',
# zoom=16,
# # threshold=.25
# )
# print()
# t = time.time()
# networks = Surfaces.networks.driving.rasterstats_from_file(
# path,
# '/home/arstneio/Downloads/shadows/test/winter/',
# zoom=16,