File size: 3,166 Bytes
fdf95c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
import geopandas as gpd
from shapely.geometry import Point, LineString
import pickle
import requests

# Load geospatial data
expressway = gpd.read_file('data/expressway.shp')
expressway.to_crs(epsg=3857, inplace=True)

geosubset = gpd.read_file('data/geosubset.shp')
geosubset.to_crs(epsg=3857, inplace=True)
geosubset['storey'] = geosubset['storey'].astype(int)
geosubset['floor_area'] = geosubset['floor_area'].astype(float)
geosubset['age_asof_t'] = geosubset['age_asof_t'].astype(int)
geosubset['transactDa'] = pd.to_datetime(geosubset['transactDa'])

mrt_raw = pd.read_csv('data/mrt.csv')
geo_mrt = gpd.GeoDataFrame(mrt_raw, crs="EPSG:4326",
                             geometry=[Point(xy) for xy in zip(mrt_raw['long'], mrt_raw['lat'])])
geo_mrt.to_crs(epsg=3857, inplace=True)

mall_df = pd.read_csv('data/mall.csv')
geo_mall = gpd.GeoDataFrame(mall_df, crs="EPSG:4326",
                             geometry=[Point(xy) for xy in zip(mall_df['long'], mall_df['lat'])])
geo_mall.to_crs(epsg=3857, inplace=True)

pri_sch_raw = pd.read_csv('data/pri_sch_latlong.csv')
geo_pri = gpd.GeoDataFrame(pri_sch_raw, crs="EPSG:4326",
                             geometry=[Point(xy) for xy in zip(pri_sch_raw['long'], pri_sch_raw['lat'])])
geo_pri.to_crs(epsg=3857, inplace=True)

sec_sec_raw = pd.read_csv('data/sec_sch_latlong.csv')
geo_sec = gpd.GeoDataFrame(sec_sec_raw, crs="EPSG:4326",
                             geometry=[Point(xy) for xy in zip(sec_sec_raw['long'], sec_sec_raw['lat'])])
geo_sec.to_crs(epsg=3857, inplace=True)

postal = pd.read_csv('data/district.csv')
postal_melt = pd.DataFrame(postal['2dpostal'].str.split(",").to_list(), index=postal.district).stack().reset_index()[['district', 0]]
postal_final = postal_melt.merge(postal, how='left', on='district').drop(columns=['2dpostal'])
postal_final.columns = ['district', '2dpostal', 'town']
postal_final['2dpostal'] = postal_final['2dpostal'].astype(str).str.strip()

# Load models
xgb = pickle.load(open('model/xgb_final.sav', "rb"))
enc = pickle.load(open('model/encoder.sav', "rb"))

# Load CIs
sd_hdb = 34.88572450193993
sd_condo = 112.3696357113714
sd_ec = 66.34935799217807
sd_apt = 137.16498181900047

# test one map api here
def test_postal(location):
    searchQuery = "https://developers.onemap.sg/commonapi/search?searchVal=" + location + "&returnGeom=Y&getAddrDetails=Y"
    response = requests.get(searchQuery)
    
    try:
        result = response.json()['results'][0]
        latitude = result['LATITUDE']
        longitude = result['LONGITUDE']
        return [result['SEARCHVAL'], result['ADDRESS'], float(latitude), float(longitude)]
    except: 
        return 'INVALID LOCATION'

# Actual predictions
def getSVY21(location):
    searchQuery = "https://developers.onemap.sg/commonapi/search?searchVal=" + location + "&returnGeom=Y&getAddrDetails=Y"
    response = requests.get(searchQuery)
    
    try:
        result = response.json()['results'][0]
        latitude = result['LATITUDE']
        longitude = result['LONGITUDE']
        return float(latitude), float(longitude)
    except: 
        return 'INVALID LOCATION'

print('utils imported!')