-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnadlan_procedures.py
2580 lines (2439 loc) · 111 KB
/
nadlan_procedures.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 6 11:37:53 2021
First download all nadlan deals:
then, do concat_all and save to Nadlan_full_cities folder
then do recover_neighborhoods_for_all_cities(recover_XY=True)
then process each df_city
NEW SEARCH and PROCESS (search by NEIGHBORHOODs):
1) use neighborhood_city_code_counts.csv file to search for cities/neighborhoods
2) for each city_neighborhood: do body post and parse to dataframe
3) now go over each row and try to fill in cols:
X, Y: from body requests with the FULLADRESS
if no FULLADRESS try:
get_XY_coords_using_GUSH and get_address_using_PARCEL_ID
4) now search for historic deals if TREND_FORMAT != ''
COMbining Neighborhood and cities dataset:
apperentely there were X, Y = 0 so i did the following:
inds=df[df['Y']==0].index
df.loc[inds,'DescLayerID']=np.nan
inds=df[df['X']==0].index
df.loc[inds,'DescLayerID']=np.nan
gdf=load_gush_parcel_shape_file()
df=recover_XY_using_gush_parcel_shape_file(df, gush_gdf=gdf,desc='NaN')
@author: ziskin
"""
# TODO: each call to body with street name yields district, city and street
# name and mid-street coords along with neiborhood based upon mid-street coords.
# however, if we request street and building number we get more precise location
# and neighborhood. We should re-run the body request for each line in the DataFrame
# and get the precise coords and nieghborhood
from MA_paths import work_david
nadlan_path = work_david / 'Nadlan_deals'
muni_path = work_david/'gis/muni_il'
apts = ['דירה', 'דירה בבית קומות']
intel_kiryat_gat = [31.599645, 34.785265]
bad_gush = ['1240-43']
def transform_lat_lon_point_to_new_israel(lat, lon):
from pyproj import Transformer
from shapely.geometry import Point
transformer = Transformer.from_crs("EPSG:4326", "EPSG:2039")
p = transformer.transform(lon, lat)
return Point(p)
intel_kiryat_gat_ITM = transform_lat_lon_point_to_new_israel(
34.785265, 31.599645)
def produce_dfs_for_circles_around_point(point=intel_kiryat_gat_ITM,
path=work_david, point_name='Intel_kiryat_gat',
nadlan_path=nadlan_path,
min_dis=0, max_dis=10, savepath=None):
from Migration_main import path_glob
from cbs_procedures import geo_location_settelments_israel
import pandas as pd
print('Producing nadlan deals with cities around point {} with radii of {} to {} kms.'.format(
point, min_dis, max_dis))
df = geo_location_settelments_israel(path)
df_radii = filter_df_with_distance_from_point(df, point,
min_distance=min_dis, max_distance=max_dis)
cities_radii = [x for x in df_radii['city_code']]
files = path_glob(nadlan_path, '*/')
available_city_codes = [x.as_posix().split('/')[-1] for x in files]
available_city_codes = [int(x)
for x in available_city_codes if x.isdigit()]
dfs = []
cnt = 1
for city_code in cities_radii:
if city_code in available_city_codes:
print(city_code)
# print('found {} city.'.format(city_code))
df = concat_all_nadlan_deals_from_one_city_and_save(
city_code=int(city_code))
if not df.empty:
dfs.append(df)
cnt += 1
print('found total {} cities within {}-{} km radius.'.format(cnt, min_dis, max_dis))
df = pd.concat(dfs, axis=0)
if savepath is not None:
filename = 'Nadlan_deals_around_{}_{}-{}.csv'.format(
point_name, min_dis, max_dis)
df.to_csv(savepath/filename, na_rep='None', index=False)
print('{} was saved to {}.'.format(filename, savepath))
return df
def produce_n_largest_cities_nadlan_time_series(df, col='NIS_per_M2', n=10):
import pandas as pd
df = df[df['DEALNATUREDESCRIPTION'].isin(apts)]
cities = get_all_city_codes_from_largest_to_smallest()
inds = cities.iloc[0:n].index
dfs = []
for ind in inds:
print('producing time seires for {}'.format(ind))
city_df = df[df['city_code'] == ind]
city_mean = city_df.set_index('DEALDATETIME').resample(
'MS')[col].mean().rolling(12, center=True).mean()
city_mean = city_mean.to_frame('{}_{}'.format(col, ind))
city_mean.index.name = ''
dfs.append(city_mean)
df_cities = pd.concat(dfs, axis=1)
return df_cities
def remove_outlier_by_value_counts(df, col, thresh=5, keep_nans=True):
import pandas as pd
nans = df[df[col].isnull()]
vc = df[col].value_counts()
vc = vc[vc > thresh]
print('filtering df by allowing for minmum {} value counts in {}.'.format(thresh, col))
vals = [x for x in vc.index]
df = df[df[col].isin(vals)]
if keep_nans:
df = pd.concat([df, nans], axis=0)
df = df.reset_index(drop=True)
return df
def tries_requests_example():
from requests.exceptions import RequestException, ConnectionError
tries = 100
for i in range(tries):
try:
for file in files:
city_code = file.as_posix().split('/')[-1]
try:
get_historic_deals_for_city_code(city_code=city_code)
except FileNotFoundError:
continue
except ConnectionError:
if i < tries - 1:
print('retrying...')
sleep_between(5, 10)
continue
else:
raise
break
# this one worked:
tries = 100
for i in range(tries):
try:
for c in reversed(cc):
try:
get_all_no_street_nadlan_deals_from_settelment(
city_code=c, savepath=nadlan_path/str(c), pop_warn=None)
except UnboundLocalError:
continue
except RequestException:
if i < tries - 1:
print('retrying...')
sleep_between(5, 10)
continue
else:
raise
break
# def convert_headers_to_dict(filepath):
# f = open(filepath, "r")
# lines = f.readlines()
# headers = {}
# for old_line in lines:
# line = old_line.replace("\n", "")
# split_here = line.find(":")
# headers[line[:split_here]] = line[split_here+2:]
# return headers
# def go_over_df_and_add_location_and_neighborhood_data(df):
# import pandas as pd
# loc_df = pd.DataFrame()
# for i, row in df.iterrows():
# fa = row['FULLADRESS']
# print('getting data for {} '.format(fa))
# body = produce_nadlan_rest_request(full_address=fa)
# loc_df = loc_df.append(parse_body_request_to_dataframe(body))
# print('Done!')
# df = pd.concat([df, loc_df], axis=1)
# return df
# def fill_in_missing_str_in_the_same_col(df, same_val_col='GUSH', missin_col='Neighborhood'):
# for group, inds in df.groupby([same_val_col]).groups.items():
# if len(inds) > 1:
def load_gush_parcel_shape_file(path=work_david/'gis', shuma=False):
import geopandas as gpd
if shuma:
print('loading shuma gush parcel file...')
gdf = gpd.read_file(
path/'parcel_shuma_15-6-21.shp', encoding='cp1255')
else:
print('loading regular gush parcel file...')
gdf = gpd.read_file(
work_david/'gis/PARCEL_ALL - Copy.shp', encoding='cp1255')
return gdf
def recover_XY_using_gush_parcel_shape_file(df_all, gush_gdf, desc='SETL_MID_POINT'):
import pandas as pd
df_all = df_all.copy().reset_index(drop=True)
city = df_all['City'].unique()[0]
if desc is not None:
if desc == 'NaN':
df = df_all[df_all['DescLayerID'].isnull()].copy()
else:
df = df_all[df_all['DescLayerID'] == desc].copy()
if df.empty:
print('No {} XYs were found in dataframe...'.format(desc))
return df_all
lots = [x[0] for x in df['GUSH'].str.split('-')]
parcels = [x[1] for x in df['GUSH'].str.split('-')]
df['lot'] = lots
df['parcel'] = parcels
groups = df.groupby(['lot', 'parcel']).groups
total = len(groups)
cnt_all = 0
cnt_gush = 0
print('Found {} deals with non recovered XY for city: {} and with DescLayerID of {}.'.format(total, city, desc))
# for i, (ind, row) in enumerate(df.iterrows()):
for i, ((lot, parcel), inds) in enumerate(groups.items()):
print('Fetching XY for {}-{} ({}/{}).'.format(lot, parcel, i+1, total))
gdf = gush_gdf.query('GUSH_NUM=={} & PARCEL=={}'.format(lot, parcel))
if gdf.empty:
print('No XY were recovered for {}-{}, trying only GUSH'.format(lot, parcel))
gdf = gush_gdf.query('GUSH_NUM=={}'.format(lot))
if gdf.empty:
print(
'No XY were recovered for {}-{} (even GUSH)...'.format(lot, parcel))
continue
df_all.loc[inds, 'X'] = gdf.unary_union.centroid.x
df_all.loc[inds, 'Y'] = gdf.unary_union.centroid.y
df_all.loc[inds, 'DescLayerID'] = 'XY_recovered_shape_gush_only'
cnt_gush += 1
else:
if gdf.centroid.size > 1:
df_all.loc[inds, 'X'] = gdf.centroid.iloc[-1].x
df_all.loc[inds, 'Y'] = gdf.centroid.iloc[-1].y
df_all.loc[inds, 'DescLayerID'] = 'XY_recovered_shape_last'
else:
df_all.loc[inds, 'X'] = gdf.centroid.x.item()
df_all.loc[inds, 'Y'] = gdf.centroid.y.item()
df_all.loc[inds, 'DescLayerID'] = 'XY_recovered_shape'
cnt_all += 1
print('Recovered exact {} XYs and {} GUSH XYs out of {} total records.'.format(
cnt_all, cnt_gush, total))
return df_all
def recover_neighborhoods_for_all_cities(path=nadlan_path/'Nadlan_full_cities',
gush_file=None):
from Migration_main import path_glob
import numpy as np
# first run concat_all...
folders = path_glob(nadlan_path, '*/')
folders = [x for x in folders if any(i.isdigit() for i in x.as_posix())]
# files = path_glob(nadlan_path, 'Nadlan_deals_city_*_street_*.csv')
city_codes = [x.as_posix().split('/')[-1] for x in folders]
city_codes = sorted(np.unique(city_codes))
print('found {} city codes: {}'.format(
len(city_codes), ', '.join(city_codes)))
for city_code in sorted(city_codes):
try:
recover_neighborhoods_for_one_city(path, city_code, gdf=gush_file)
except FileNotFoundError:
print('{} not found in {}, skipping...'.format(city_code, path))
continue
return
def recover_neighborhoods_for_one_city(path=nadlan_path/'Nadlan_full_cities',
city_code=8700, gdf=None):
from Migration_main import path_glob
import pandas as pd
file = path_glob(
path, 'Nadlan_deals_city_{}_all_streets_*.csv'.format(city_code))
file = file[0]
if 'recovered_neighborhoods' in file.as_posix():
print('{} already recovered_neighborhoods, skipping...'.format(file))
return
df = pd.read_csv(file, na_values='None')
if gdf is not None:
print('First, recovering XY using shape file:')
df = recover_XY_using_gush_parcel_shape_file(df, gdf)
print('Recovering neighborhoods in {}:'.format(city_code))
df = recover_neighborhood_for_df(df)
if df.empty:
print('no empty neighborhoods in {} found.'.format(city_code))
return
filename = file.as_posix().split('/')[-1]
filename = '_'.join(filename.split(
'_')[:-1]) + '_recovered_neighborhoods' + '_h.csv'
df.to_csv(path/filename, na_rep='None', index=False)
print('Successfully saved {}'.format(filename))
file.unlink()
return
def recover_neighborhood_for_df(df_all):
import numpy as np
import pandas as pd
df_all = df_all.copy().reset_index(drop=True)
city = df_all['City'].unique()[0]
df = df_all[df_all['Neighborhood'].isnull()]
if df.empty:
return df_all
df['lot'] = [x[0] for x in df['GUSH'].str.split('-')]
df['parcel'] = [x[1] for x in df['GUSH'].str.split('-')]
groups = df.groupby(['lot', 'parcel']).groups
total = len(groups)
df.loc[:, 'Neighborhood'] = 'None'
df.loc[:, 'Neighborhood_code'] = np.nan
df.loc[:, 'Neighborhood_id'] = np.nan
cnt = 0
print('Found {} deals in empty neighborhoods for city: {}.'.format(total, city))
for i, ((lot, parcel), inds) in enumerate(groups.items()):
# for i, (ind, row) in enumerate(df.iterrows()):
print('Fetching Nieghborhood for {}-{} ({}/{}).'.format(lot, parcel, i+1, total))
x = df.loc[inds[0], 'X']
y = df.loc[inds[0], 'Y']
df_neigh = get_neighborhoods_area_by_XY(x, y)
if df_neigh.empty:
continue
cnt += 1
df.loc[inds, 'Neighborhood'] = df_neigh['Neighborhood'].item()
df.loc[inds, 'Neighborhood_code'] = df_neigh['Neighborhood_code'].item()
df.loc[inds, 'Neighborhood_id'] = df_neigh['Neighborhood_id'].item()
sleep_between(0.3, 0.35)
print('Recovered {} Neighborhoods out of {} total no streets records.'.format(
cnt, total))
df_all.loc[df.index, 'Neighborhood'] = df['Neighborhood']
df_all.loc[df.index, 'Neighborhood_code'] = df['Neighborhood_code']
df_all.loc[df.index, 'Neighborhood_id'] = df['Neighborhood_id']
return df_all
def recover_XY_for_all_no_streets(nadlan_path=nadlan_path):
from Migration_main import path_glob
import numpy as np
folders = path_glob(nadlan_path, '*/')
folders = [x for x in folders if any(i.isdigit() for i in x.as_posix())]
city_codes = [x.as_posix().split('/')[-1] for x in folders]
city_codes = sorted(np.unique([int(x) for x in city_codes]))
for city_code in city_codes:
try:
recover_XY_for_one_city(nadlan_path, city_code)
except FileNotFoundError:
continue
print('Done recovering XY from no streets.')
return
def recover_XY_for_one_city(nadlan_path=nadlan_path, city_code=8400):
from Migration_main import path_glob
import pandas as pd
files = sorted(path_glob(nadlan_path/str(city_code),
'Nadlan_deals_city_{}_no_streets_*_h.csv'.format(city_code)))
file = files[0]
print('processing recovery of XY in city code: {}'.format(city_code))
if 'recovered' not in file.as_posix() and len(files) == 2:
file.unlink()
print('deleted {}'.format(file))
return
elif 'recovered' in file.as_posix() and len(files) == 1:
print('{} already recovered, skipping...'.format(file))
return
elif 'recovered' not in file.as_posix() and len(files) == 1:
df = pd.read_csv(file, na_values='None')
try:
df = recover_XY_address_and_neighborhoods_using_GUSH(df)
except AttributeError:
print('nan detected in {}, deleting...'.format(file))
file.unlink()
return
filename = file.as_posix().split('/')[-1]
if '_recovered' not in filename:
filename = '_'.join(filename.split(
'_')[:-1]) + '_recovered' + '_h.csv'
df.to_csv(nadlan_path/str(city_code) /
filename, na_rep='None', index=False)
print('Successfully saved {}'.format(filename))
# if '_recovered' not in filename:
# now delete the old file:
file.unlink()
return
def recover_XY_address_and_neighborhoods_using_GUSH(df_no_streets):
import numpy as np
# assume we deal with no_streets_df
df = df_no_streets.copy()
total = len(df)
city = df['City'].unique()[0]
df['Neighborhood'] = 'None'
df['Neighborhood_code'] = np.nan
df['Neighborhood_id'] = np.nan
cnt_arr = np.array([0, 0, 0])
print('Found {} no streets deals for city: {}.'.format(total, city))
for i, row in df_no_streets.iterrows():
print('Fetching X, Y for {} ({}/{}).'.format(row['GUSH'], i+1, total))
try:
x, y, parcel_id = get_XY_coords_using_GUSH(row['GUSH'])
df.at[i, 'X'] = x
df.at[i, 'Y'] = y
df.at[i, 'ObjectID'] = parcel_id
sleep_between(0.05, 0.1)
cnt_arr[0] += 1
df.at[i, 'DescLayerID'] = 'XY_recovered'
except ValueError:
print('No X, Y found for {}.'.format(row['GUSH']))
continue
try:
df_address = get_address_using_PARCEL_ID(parcel_id)
df.at[i, 'FULLADRESS'] = df_address['FULLADRESS']
df.at[i, 'Street'] = df_address['Street']
sleep_between(0.05, 0.1)
df.at[i, 'DescLayerID'] = 'ADDR_V1_recovered'
cnt_arr[1] += 1
except ValueError:
print('No address found for {}.'.format(row['GUSH']))
pass
df_neigh = get_neighborhoods_area_by_XY(x, y)
if df_neigh.empty:
continue
df.at[i, 'Neighborhood'] = df_neigh['Neighborhood'].item()
df.at[i, 'Neighborhood_code'] = df_neigh['Neighborhood_code'].item()
df.at[i, 'Neighborhood_id'] = df_neigh['Neighborhood_id'].item()
sleep_between(0.05, 0.1)
cnt_arr[2] += 1
print('Recovered {} Xys, {} Addresses and {} Neighborhoods out of {} total no streets records.'.format(
cnt_arr[0], cnt_arr[1], cnt_arr[2], total))
return df
def get_neighborhoods_area_by_XY(X, Y):
import requests
import pandas as pd
body = {'x': X,
'y': Y,
'mapTolerance': 39.6875793751587,
'layers': [{'LayerType': 0,
'LayerName': 'neighborhoods_area',
'LayerFilter': ''}]}
url = 'https://ags.govmap.gov.il/Identify/IdentifyByXY'
r = requests.post(url, json=body)
if not r.json()['data']:
print('No neighborhood found in {}, {}'.format(X, Y))
return pd.DataFrame()
results = r.json()['data'][0]['Result'][0]['tabs'][0]['fields']
df = pd.DataFrame(results).T
df.columns = ['Neighborhood', 'disc', 'City',
'Neighborhood_code', 'Neighborhood_id']
df.drop('disc', axis=1, inplace=True)
df.drop(['FieldName', 'FieldType'], axis=0, inplace=True)
df = df.reset_index(drop=True)
df['Neighborhood_code'] = pd.to_numeric(
df['Neighborhood_code'], errors='coerce')
df['Neighborhood_id'] = pd.to_numeric(
df['Neighborhood_id'], errors='coerce')
print('Successfuly recovered neighborhood as {}.'.format(
df['Neighborhood'].item()))
return df
def get_address_using_PARCEL_ID(parcel_id='596179', return_first_address=True):
import requests
import pandas as pd
body = {'locateType': 3, 'whereValues': ["PARCEL_ID", parcel_id, "number"]}
url = 'https://ags.govmap.gov.il/Search/SearchLocate'
r = requests.post(url, json=body)
df = pd.DataFrame(r.json()['data']['Values'])
if df.empty:
raise ValueError
city = [x[0] for x in df['Values']]
street = [x[1] for x in df['Values']]
building = [int(x[2]) for x in df['Values']]
df['City'] = city
df['Street'] = street
df['Building'] = building
fa = []
for i, row in df.iterrows():
bi = row['Building']
ci = row['City']
st = row['Street']
fa.append('{} {}, {}'.format(st, bi, ci))
df['FULLADRESS'] = fa
df.drop(['Values', 'Created', 'IsEditable'], axis=1, inplace=True)
if return_first_address:
print('Succsefully recoverd first address as {}'.format(
df.iloc[0]['FULLADRESS']))
return df.iloc[0]
else:
return df
def recover_XY_using_govmap(df, desc='NaN', shuma=True):
import pandas as pd
df = df.copy().reset_index(drop=True)
city = df['City'].unique()[0]
if desc is not None:
if desc == 'NaN':
df = df[df['DescLayerID'].isnull()].copy()
else:
df = df[df['DescLayerID'] == desc].copy()
if df.empty:
print('No {} XYs were found in dataframe...'.format(desc))
return df
lots = [x[0] for x in df['GUSH'].str.split('-')]
parcels = [x[1] for x in df['GUSH'].str.split('-')]
df['lot'] = lots
df['parcel'] = parcels
groups = df.groupby(['lot', 'parcel']).groups
total = len(groups)
cnt_all = 0
print('Found {} deals with non recovered XY for city: {} and with DescLayerID of {}.'.format(total, city, desc))
# for i, (ind, row) in enumerate(df.iterrows()):
for i, ((lot, parcel), inds) in enumerate(groups.items()):
print('Fetching XY from GovMAP for {}-{} ({}/{}).'.format(lot, parcel, i+1, total))
x, y, parcel = get_XY_coords_using_GUSH('{}-{}'.format(lot, parcel), shuma=shuma)
sleep_between(0.3, 0.35)
df.loc[inds, 'X'] = x
df.loc[inds, 'Y'] = y
cnt_all += 1
print('Recovered exact {} XYs out of {} total records.'.format(
cnt_all, total))
return df
def get_XY_coords_using_GUSH(GUSH='1533-149-12', shuma=False):
import requests
g = GUSH.split('-')
lot = g[0] # גוש
parcel = g[1] # חלקה
if shuma:
url = 'https://es.govmap.gov.il/TldSearch/api/DetailsByQuery?query=גוש שומה {} חלקה {}&lyrs=32768&gid=govmap'.format(
lot, parcel)
else:
url = 'https://es.govmap.gov.il/TldSearch/api/DetailsByQuery?query=גוש {} חלקה {}&lyrs=262144&gid=govmap'.format(
lot, parcel)
r = requests.get(url)
rdict = r.json()
if rdict['ErrorMsg'] is not None:
raise ValueError('could not find coords for {}: {}'.format(
GUSH, rdict['ErrorMsg']))
else:
if shuma:
assert rdict['data']['PARCEL_ALL_SHUMA'][0]['AData']['GUSH_NUM'] == lot
assert rdict['data']['PARCEL_ALL_SHUMA'][0]['AData']['PARCEL'] == parcel
X = rdict['data']['PARCEL_ALL_SHUMA'][0]['X']
Y = rdict['data']['PARCEL_ALL_SHUMA'][0]['Y']
parcel_id = rdict['data']['PARCEL_ALL_SHUMA'][0]['ObjectID']
print('SHUMA lot/parcel!')
else:
assert rdict['data']['GOVMAP_PARCEL_ALL'][0]['AData']['GUSH_NUM'] == lot
assert rdict['data']['GOVMAP_PARCEL_ALL'][0]['AData']['PARCEL'] == parcel
X = rdict['data']['GOVMAP_PARCEL_ALL'][0]['X']
Y = rdict['data']['GOVMAP_PARCEL_ALL'][0]['Y']
parcel_id = rdict['data']['GOVMAP_PARCEL_ALL'][0]['ObjectID']
print('succsesfully recoverd X, Y for {}'.format(GUSH))
# if get_address_too:
# df = get_address_using_PARCEL_ID(parcel_id)
# df['X'] = X
# df['Y'] = Y
# df['ObjectID'] = parcel_id
# return df
# else:
return X, Y, parcel_id
def filter_df_with_distance_from_point(df, point, min_distance=0,
max_distance=10):
import geopandas as gpd
# ditance is in kms, but calculation is in meters:
if not isinstance(df, gpd.GeoDataFrame):
print('dataframe is not GeoDataFrame!')
return
print('fitering nadlan deals from {} to {} km distance from {}'.format(
min_distance, max_distance, point))
df['distance_to_point'] = df.distance(point) / 1000.0
df = df.loc[(df['distance_to_point'] >= min_distance) &
(df['distance_to_point'] < max_distance)]
return df
def sleep_between(start=2, end=4):
"""sleep between start and end seconds"""
from numpy import random
from time import sleep
sleeptime = random.uniform(start, end)
print("sleeping for: {:.2f} seconds".format(sleeptime))
sleep(sleeptime)
# print("sleeping is over")
return
def concat_all_processed_nadlan_deals_and_save(path=nadlan_path/'Nadlan_full_cities',
savepath=work_david):
from Migration_main import path_glob
import pandas as pd
files = path_glob(path, 'Nadlan_deals_processed_city_*_all_streets_*.csv')
print('loading and concating {} cities.'.format(len(files)))
dtypes = {'FULLADRESS': 'object', 'Street': 'object', 'FLOORNO': 'object',
'NEWPROJECTTEXT': 'object', 'PROJECTNAME': 'object'}
dfs = [pd.read_csv(file, na_values='None', parse_dates=['DEALDATETIME'],
dtype=dtypes) for file in files]
df = pd.concat(dfs, axis=0)
yrmin = df['DEALDATETIME'].min().year
yrmax = df['DEALDATETIME'].max().year
filename = 'Nadlan_deals_processed_{}-{}'.format(yrmin, yrmax)
fcsv = filename + '.csv'
fhdf = filename + '.hdf'
df.to_csv(savepath/fcsv, na_rep='None', index=False)
df.to_hdf(savepath/fhdf, complevel=9, mode='w', key='nadlan',
index=False)
print('{} was saved to {}.'.format(filename, path))
return df
def process_nadlan_deals_from_all_cities(path=nadlan_path/'Nadlan_full_cities'):
from Migration_main import path_glob
import pandas as pd
files = path_glob(path, 'Nadlan_deals_city_*_all_streets_*.csv')
total = len(files)
for i, file in enumerate(files):
dtypes = {'FULLADRESS': 'string',
'Street': 'string', 'FLOORNO': 'string'}
df = pd.read_csv(file, na_values='None', parse_dates=['DEALDATETIME'],
dtype=dtypes)
city = df['City'].unique()[0]
city_code = df['city_code'].unique()[0]
print('processing {} ({}) ({}/{}).'.format(city, city_code, i+1, total))
print('Found {} deals.'.format(len(df)))
df = process_nadlan_deals_df_from_one_city(df)
filename = 'Nadlan_deals_processed_' + \
'_'.join(file.as_posix().split('/')[-1].split('_')[2:])
df.to_csv(path/filename, na_rep='None', index=False)
print('{} was saved to {}.'.format(filename, path))
print('Done!')
return
def process_nadlan_deals_df_from_one_city(df):
"""first attempt of proccessing nadlan deals df:
removal of outliers and calculation of various indices"""
from Migration_main import remove_outlier
from cbs_procedures import read_statistical_areas_gis_file
from cbs_procedures import read_social_economic_index
import geopandas as gpd
import numpy as np
df = df.reset_index(drop=True)
df = df[[x for x in df.columns if 'Unnamed' not in x]]
# first, drop some cols:
# df = df.drop(['NEWPROJECTTEXT', 'PROJECTNAME', 'TYPE',
# 'POLYGON_ID', 'TREND_IS_NEGATIVE', 'TREND_FORMAT',
# 'ObjectID', 'DescLayerID'], axis=1)
# now convert to geodataframe using X and Y:
df = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df['X'], df['Y']))
df.crs = {'init': 'epsg:2039'}
# now try to add CBS statistical areas for each address coords:
stats = read_statistical_areas_gis_file()
stats = stats[stats['city_code'] == df['city_code'].iloc[0]]
# so iterate over stats geodataframe and check if asset coords are
# within the stat area polygon (also all population 2019):
# use the same loop to to input the social economic index from CBS:
SEI = read_social_economic_index()
SEI = SEI[SEI['city_code'] == df['city_code'].iloc[0]]
for i, row in stats.iterrows():
poly = row['geometry']
stat_code = row['stat11']
pop2019 = row['pop_total']
inds = df.geometry.within(poly)
df.loc[inds, 'stat_code'] = stat_code
df.loc[inds, 'pop2019_total'] = pop2019
SEI_code = SEI[SEI['stat_code'] == stat_code]
# print(stat_code)
try:
df.loc[inds, 'SEI_value'] = SEI_code['index_value'].values[0]
df.loc[inds, 'SEI_rank'] = SEI_code['rank'].values[0]
df.loc[inds, 'SEI_cluster'] = SEI_code['cluster'].values[0]
except IndexError:
df.loc[inds, 'SEI_value'] = np.nan
df.loc[inds, 'SEI_rank'] = np.nan
df.loc[inds, 'SEI_cluster'] = np.nan
# remove outliers in squared meters per asset:
df = remove_outlier(df, 'DEALNATURE')
if len(df) > 100:
df = remove_outlier_by_value_counts(df, 'ASSETROOMNUM', thresh=5)
df = remove_outlier(df, 'DEALAMOUNT')
# calculate squared meters per room:
df['M2_per_ROOM'] = df['DEALNATURE'] / df['ASSETROOMNUM']
df['NIS_per_M2'] = df['DEALAMOUNT'] / df['DEALNATURE']
# try to guess new buildings:
df['New'] = df['BUILDINGYEAR'] == df['DEALDATETIME'].dt.year
df['Age'] = df['DEALDATETIME'].dt.year - df['BUILDINGYEAR']
# try to guess ground floors apts.:
df['Ground'] = df['FLOORNO'].str.contains('קרקע')
df = df.reset_index(drop=True)
return df
def concat_all_nadlan_deals_from_all_cities_and_save(nadlan_path=work_david/'Nadlan_deals',
savepath=None, drop_dup=True,
delete_files=False):
"""concat all nadlan deals for all the cities in nadlan_path"""
from Migration_main import path_glob
import numpy as np
folders = path_glob(nadlan_path, '*/')
folders = [x for x in folders if any(i.isdigit() for i in x.as_posix())]
# files = path_glob(nadlan_path, 'Nadlan_deals_city_*_street_*.csv')
city_codes = [x.as_posix().split('/')[-1] for x in folders]
city_codes = np.unique(city_codes)
print('found {} city codes: {}'.format(
len(city_codes), ', '.join(city_codes)))
for city_code in sorted(city_codes):
concat_all_nadlan_deals_from_one_city_and_save(nadlan_path=nadlan_path,
city_code=int(
city_code),
savepath=savepath,
delete_files=delete_files,
drop_dup=drop_dup)
return
def post_nadlan_historic_deals(keyvalue):
"""take keyvalue from body or df fields and post to nadlan.gov.il deals
REST API historic deals"""
import requests
url = 'https://www.nadlan.gov.il/Nadlan.REST/Main/GetAllAssestHistoryDeals'
r = requests.post(url, json=str(keyvalue))
if r.status_code != 200:
raise ValueError('couldnt get a response ({}).'.format(r.status_code))
result = r.json()
if not result:
raise TypeError('no results found')
return result
def get_historic_deals_for_city_code(main_path=nadlan_path, city_code=8700):
from Migration_main import path_glob
import os
import pandas as pd
# from pathlib import Path
streets = path_glob(main_path / str(city_code),
'Nadlan_deals_city_{}_*street*.csv'.format(city_code))
# check for the h suffix and filter these files out (already done historic):
streets = [x for x in streets if '_h' not in x.as_posix().split('/')[-1]]
print('Found {} streets to check in {}.'.format(len(streets), city_code))
cnt = 1
for street in streets:
filename = street.as_posix().split('.')[0] + '_h.csv'
# if Path(filename).is_file():
# print('{} already exists, skipping...'.format(filename))
# cnt += 1
# continue
df = pd.read_csv(street, na_values='None')
try:
df = get_historic_nadlan_deals_from_a_street_df(df, unique=True)
except IndexError as e:
print('{} in file: {}, deleting'.format(e, street))
if 'no_streets' and 'nan' in street.as_posix():
street.unlink()
if not df.empty:
df.to_csv(filename, na_rep='None', index=False)
print('File was rewritten ({} out of {}).'.format(cnt, len(streets)))
# delete old file:
street.unlink()
else:
print('skipping and renaming ({} of out {})'.format(cnt, len(streets)))
os.rename(street.as_posix(), filename)
cnt += 1
print('Done fetching historic deals for {}.'.format(city_code))
return
def get_historic_nadlan_deals_from_a_street_df(df, unique=True):
import pandas as pd
import numpy as np
# first get the unique dataframe of KEYVALUE (should be the same)
if unique:
df = df.sort_index().groupby('KEYVALUE').filter(lambda group: len(group) == 1)
df['DEALDATETIME'] = pd.to_datetime(df['DEALDATETIME'])
city = df['City'].iloc[0]
street = df['Street'].iloc[0]
total_keys = len(df)
print('Fetching historic deals on {} street in {} (total {} assets).'.format(
street, city, total_keys))
cnt = 0
dfs = []
for i, row in df.iterrows():
keyvalue = row['KEYVALUE']
city_code = row['city_code']
street_code = row['street_code']
other_fields = row.loc['ObjectID': 'Street']
try:
result = post_nadlan_historic_deals(keyvalue)
dfh_key = parse_one_json_nadlan_page_to_pandas(result, city_code,
street_code,
historic=True)
other_df = pd.DataFrame().append(
[other_fields.to_frame().T] * len(dfh_key), ignore_index=True)
dfs.append(pd.concat([dfh_key, other_df], axis=1))
sleep_between(0.1, 0.15)
cnt += 1
except TypeError:
sleep_between(0.01, 0.05)
continue
except ValueError:
sleep_between(0.01, 0.05)
continue
print('Found {} assets with historic deals out of {}.'.format(cnt, total_keys))
try:
dfh = pd.concat(dfs)
except ValueError:
print('No historic deals found for {}, skipping...'.format(street))
return pd.DataFrame()
dfh = dfh.reset_index()
dfh = dfh.sort_index()
df_new = pd.concat([df, dfh], axis=0)
df_new.replace(to_replace=[None], value=np.nan, inplace=True)
df_new = df_new.sort_values('GUSH')
df_new = df_new.reset_index(drop=True)
# try to fill in missing description from current deals:
grps = df_new.groupby('GUSH').groups
for gush, ind in grps.items():
if len(ind) > 1:
desc = df_new.loc[ind, 'DEALNATUREDESCRIPTION'].dropna()
try:
no_nan_desc = desc.values[0]
except IndexError:
no_nan_desc = np.nan
df_new.loc[ind, 'DEALNATUREDESCRIPTION'] = df_new.loc[ind,
'DEALNATUREDESCRIPTION'].fillna(no_nan_desc)
# finally make sure that we don't add duplicate deals:
cols = df_new.loc[:, 'DEALAMOUNT':'TREND_FORMAT'].columns
df_new = df_new.drop_duplicates(subset=df_new.columns.difference(cols))
return df_new
def concat_all_nadlan_deals_from_one_city_and_save(nadlan_path=work_david/'Nadlan_deals',
city_code=8700,
savepath=None,
delete_files=False,
drop_dup=True):
"""concat all nadlan deals for all streets in a specific city"""
import pandas as pd
import numpy as np
from Migration_main import path_glob
import click
city_path = nadlan_path / str(city_code)
try:
files = path_glob(
city_path, 'Nadlan_deals_city_{}_*street*.csv'.format(city_code))
except FileNotFoundError:
return pd.DataFrame()
dfs = [pd.read_csv(file, na_values='None') for file in files]
df = pd.concat(dfs)
df['DEALDATETIME'] = pd.to_datetime(df['DEALDATETIME'])
df.drop(['DEALDATE', 'DISPLAYADRESS'], axis=1, inplace=True)
print('concated all {} ({}) csv files.'.format(
df['City'].unique()[0], city_code))
df = df.sort_index()
df = df.iloc[:, 2:]
# first drop records with no full address:
# df = df[~df['FULLADRESS'].isna()]
# now go over all the unique GUSHs and fill in the geoloc codes if missing
# and then drop duplicates
# good_district = df['District'].unique()[df['District'].unique() != ''][0]
try:
good_district = df['District'].value_counts().index[0]
except IndexError:
good_district = np.nan
good_city = df['City'].value_counts().index[0].strip()
df = df.copy()
if not pd.isnull(good_district):
df.loc[:, 'District'].fillna(good_district, inplace=True)
df.loc[:, 'City'].fillna(good_city, inplace=True)
df['City'] = df['City'].str.strip()
df = df[df['City'] == good_city]
# take care of street_code columns all but duplicates bc of neighhood code/street code:
if drop_dup:
df = df.drop_duplicates(
subset=df.columns.difference(['street_code', 'Street']))
# lasty, extract Building number from FULLADRESS and is NaN remove record:
df['Building'] = df['FULLADRESS'].astype(str).str.extract('(\d+)')
# df = df[~df['Building'].isna()]
if savepath is not None:
yrmin = df['DEALDATETIME'].min().year
yrmax = df['DEALDATETIME'].max().year
filename = 'Nadlan_deals_city_{}_all_streets_{}-{}.csv'.format(
city_code, yrmin, yrmax)
df.to_csv(savepath/filename, na_rep='None')
print('{} was saved to {}'.format(filename, savepath))
if delete_files:
msg = 'Deleting all {} city files, Do you want to continue?'.format(
city_code)
if click.confirm(msg, default=True):
[x.unlink() for x in files]
print('{} files were deleted.'.format(city_code))
return df
def get_all_nadlan_deals_from_one_city(path=work_david, city_code=5000,
savepath=None,
sleep_between_streets=[1, 5],
start_from_street_code=None):
"""Get all nadlan deals for specific city from nadlan.gov.il."""
import pandas as pd
import os
from cbs_procedures import read_street_city_names
df = read_street_city_names(path=path, filter_neighborhoods=True)
streets_df = df[df['city_code'] == city_code]
st_df = streets_df.reset_index(drop=True)
if city_code == 1061:
st_df['city_name'] = st_df[st_df['city_code'] == 1061]['city_name'].str.replace(
'נוף הגליל', 'נצרת עילית')
if start_from_street_code is not None:
ind = st_df[st_df['street_code'] == start_from_street_code].index
st_df = st_df.iloc[ind[0]:]
print('Starting from street {} for city {}'.format(
st_df.iloc[0]['street_name'], st_df.iloc[0]['city_name']))
bad_streets_df = pd.DataFrame()
all_streets = len(st_df)
cnt = 1
for i, row in st_df.iterrows():
city_name = row['city_name']
street_name = row['street_name']
street_code = row['street_code']
print('Fetching Nadlan deals, city: {} , street: {} ({} out of {})'.format(
city_name, street_name, cnt, all_streets))
if not savepath.is_dir():
print('Folder {} not found...Creating it.'.format(savepath))
os.mkdir(savepath)
df = get_all_historical_nadlan_deals(
city=city_name, street=street_name, city_code=city_code,
street_code=street_code, savepath=savepath, sleep_between_streets=True)
if df is None:
bad_df = pd.DataFrame(
[city_code, city_name, street_code, street_name]).T
bad_df.columns = ['city_code', 'city_name',
'street_code', 'street_name']
print('no data for street: {} in {}'.format(street_name, city_name))
bad_streets_df = bad_streets_df.append(bad_df)
elif df.empty:
cnt += 1
continue
if sleep_between_streets is not None:
sleep_between(sleep_between_streets[0], sleep_between_streets[1])
cnt += 1
print('Done scraping {} city ({}) from nadlan.gov.il'.format(
city_name, city_code))
if savepath is not None:
filename = 'Nadlan_missing_streets_{}.csv'.format(city_code)
bad_streets_df.to_csv(savepath/filename)
print('{} was saved to {}'.format(filename, savepath))
return
def get_all_city_codes_from_largest_to_smallest(path=work_david):
"""get all city codes and sort using population (2015)"""
from cbs_procedures import read_periphery_index
dff = read_periphery_index(work_david)
city_codes = dff.sort_values('Pop2015', ascending=False)
city_codes['NameHe'] = city_codes['NameHe'].str.replace('*','')
return city_codes
def parse_one_json_nadlan_page_to_pandas(page, city_code=None,
street_code=None,
historic=False, add_extra_cols=True):
# keep_no_address=False):
"""parse one request of nadlan deals JSON to pandas"""
import pandas as pd
if historic:
df = pd.DataFrame(page)
else:
df = pd.DataFrame(page['AllResults'])
# df.set_index('DEALDATETIME', inplace=True)
# df.index = pd.to_datetime(df.index)
df['DEALDATETIME'] = pd.to_datetime(df['DEALDATETIME'])
df['DEALAMOUNT'] = df['DEALAMOUNT'].str.replace(',', '').astype(int)
# M^2:
df['DEALNATURE'] = pd.to_numeric(df['DEALNATURE'])
# room number:
df['ASSETROOMNUM'] = pd.to_numeric(df['ASSETROOMNUM'])
# building year:
df['BUILDINGYEAR'] = pd.to_numeric(df['BUILDINGYEAR'])
# number of floors in building:
df['BUILDINGFLOORS'] = pd.to_numeric(df['BUILDINGFLOORS'])
# if new from contractor (1st hand):
df['NEWPROJECTTEXT'] = pd.to_numeric(