#coding=UTF-8
import logging
import sys
reload(sys)
sys.setdefaultencoding('utf8')

from google.appengine.ext import db

import math

LAT_DEVIATION = 0.003
LNG_DEVIATION = 0.003

def getlandmark(lat, lng):
    for latlng in LANDMARK_KEYWORDS:
        ll = latlng.split(",")
        landlat = float(ll[0])
        landlng = float(ll[1])

        if math.fabs(landlat - float(lat)) < LAT_DEVIATION:
            if math.fabs(landlng - float(lng)) < LNG_DEVIATION:
                lm = Landmark()
                lm.loadfromdb(latlng)
                return lm
    return None


class LandmarkModel(db.Model):
    location = db.StringProperty(required=True)
    related_news = db.StringListProperty(str, default=[], required=True)
    parse_datetime = db.DateTimeProperty(auto_now=True)

class Landmark:
    def __init__(self, location="", related_news=[]):
        self.setlandmarkdata(location=location, related_news=related_news)

    def setlandmarkdata(self, location, related_news=[]):
        self.location = location
        self.related_news = related_news

    def __str__(self):
        str_args = (str(self.location), str(self.related_news))
        return "Location: %s, News Id: %s" % str_args

    def writetodb(self):
        news_entry = LandmarkModel.all().filter("location =", self.location).get()
        if news_entry == None:
            # create new entry
            key = LandmarkModel(location = self.location,
                                related_news = self.related_news)
            db.put(key)
            return "create new Landmark"
        else:
            # update exist entry
            news_entry.location = self.location
            news_entry.related_news = self.related_news
            return "Update exist Landmark"

    def loadfromdb(self, location):
        news_entry = LandmarkModel.all().filter("location =", location).get()
        if news_entry != None:
            self.location = news_entry.location
            self.related_news = news_entry.related_news
            return "Load Landmark from DB succeed"
        else:
            return "The loaded Landmark is not exist in DB"

LANDMARK_KEYWORDS = {
    '25.0307687,121.5295286': ['永康街'],
    '25.12726,121.468942': ['琉園水晶博物館'],
    '25.0585451,121.5108187': ['永樂市場', '永樂布業商場'],
    '25.112834,121.56488': ['明德樂園'],
    '25.1767787,121.546692': ['小油坑遊憩區'],
    '25.0462219,121.5443766': ['微風廣場'],
    '25.1692476,121.539007': ['竹子湖'],
    '25.1549268,121.5381191': ['草山行館'],
    '25.0459924,121.5207182': ['行政院'],
    '25.04898,121.517077': ['台北轉運站'],
    '25.1023449,121.5536577': ['張大千紀念館'],
    '25.023387,121.5453446': ['國立臺北教育大學'],
    '25.0734824,121.5155336': ['保安宮'],
    '25.0694763,121.5212541': ['中山足球場'],
    '25.073131,121.524635': ['台北故事館'],
    '25.0441303,121.5293995': ['華山1914創意文化園區', '華山藝文特區', '華山文創園區'],
    '25.0479239,121.5170809': ['台北車站'],
    '24.969118,121.580506': ['三墩石茶壺博物館'],
    '25.1670449,121.56017': ['夢幻湖'],
    '25.037525,121.563782': ['臺北市政府'],
    '25.050391,121.5362861': ['袖珍博物館'],
    '25.04747,121.5175259': ['高鐵臺北站', '高鐵台北站'],
    '25.1516929,121.548373': ['前山公園'],
    '25.1555874,121.5526499': ['中山樓', '陽明山中山樓'],
    '25.182902,121.5649959': ['馬槽溫泉'],
    '25.0601727,121.5593073': ['松山區'],
    '25.0421268,121.5067086': ['西門紅樓'],
    '25.1531485,121.5174286': ['北投區'],
    '25.084873,121.525077': ['劍潭公園'],
    '25.0434592,121.5201798': ['教育部'],
    '25.0805694,121.5429712': ['國防部'],
    '25.133333,121.471389': ['關渡美術館'],
    '25.0444797,121.5322098': ['台北希望廣場'],
    '25.0130679,121.5416515': ['國立臺灣科技大學', '國立台灣科技大學', '臺科大', '台科大', 'NTUST'],
    '5.047006,121.563143': ['台北機廠'],
    '25.0481323,121.5177379': ['國父史蹟紀念館', '逸仙公園'],
    '25.0673666,121.5212688': ['大同大學'],
    '25.0366038,121.5138046': ['台北市立教育大學', '台北市立大學'],
    '25.0248777,121.5419522': ['科技部'],
    '25.0339031,121.5645099': ['台北101', 'Taipei 101'],
    '25.0298934,121.5554945': ['臨江街夜市', '通化夜市'],
    '25.0431034,121.5231768': ['蝴蝶宮昆蟲博物館'],
    '25.098493,121.5361083': ['雙溪公園'],
    '25.041687,121.546456': ['東區地下街'],
    '25.0253544,121.5616183': ['台北醫學大學'],
    '25.0743434,121.5457421': ['迎風河濱公園'],
    '25.1347802,121.5324453': ['士林區'],
    '25.1365244,121.5035237': ['七星公園'],
    '25.076902,121.526809': ['北安公園'],
    '25.0483289,121.5336235': ['樹火紀念紙博物館'],
    '5.0390097,121.5223209': ['臺北市青少年發展處', 'Y17台北市青少年育樂中心'],
    '25.0507444,121.5189791': ['台北當代藝術館'],
    '25.0337219,121.537698': ['建國假日花市'],
    '25.031672,121.511775': ['國立台灣藝術教育館'],
    '25.1230017,121.5137024': ['國立陽明大學', '陽明大學'],
    '24.9988534,121.5810682': ['台北市立動物園'],
    '25.1156276,121.5769132': ['內雙溪森林自然公園'],
    '25.1370521,121.5058375': ['凱達格蘭文化館'],
    '25.051225,121.5106299': ['海關博物館'],
    '25.0322049,121.5098835': ['台北植物園'],
    '25.0991799,121.5465894': ['至德園'],
    '25.0643486,121.5082': ['延三夜市', '大橋頭夜市'],
    '24.9691177,121.5943216': ['鐵觀音包種茶研發推廣中心'],
    '24.9679789,121.5872199': ['木柵觀光茶園'],
    '25.0287143,121.5723161': ['信義區'],
    '25.0566023,121.5071422': ['大稻埕碼頭'],
    '25.0173405,121.5397518': ['國立臺灣大學', '國立台灣大學', '臺灣大學', '台灣大學', 'NTU'],
    '25.1095565,121.5469651': ['林語堂故居', '林語堂先生紀念館'],
    '25.0568996,121.6180619': ['台北世界貿易中心南港展覽館', '南港展覽館'],
    '25.0422541,121.5252793': ['國立台北商業大學'],
    '25.0383919,121.6219879': ['胡適公園'],
    '25.0957569,121.5166018': ['國立台灣科學教育館', '科教館'],
    '25.0427543,121.5150337': ['國立臺灣博物館'],
    '25.0247972,121.5423013': ['資訊科學展示中心'],
    '25.0331797,121.5608466': ['台北國際會議中心'],
    '25.0293008,121.5205833': ['中正區'],
    '25.1669979,121.5661649': ['冷水坑遊憩區'],
    '25.136298,121.506296': ['台北市立圖書館北投分館'],
    '25.0685028,121.5456014': ['中山區'],
    '25.0707535,121.5204805': ['圓山公園'],
    '25.0751223,121.5363445': ['大佳河濱公園'],
    '25.0440858,121.5609048': ['松山文創園區', '松菸'],
    '25.0367424,121.5016321': ['台北市鄉土教育中心', '剝皮寮歷史街區'],
    '25.0791511,121.5330752': ['忠烈祠'],
    '25.067566,121.552699': ['臺北國際航空站', '松山機場'],
    '25.1668446,121.5633701': ['冷水坑溫泉'],
    '25.0254571,121.5273658': ['國立臺灣師範大學', '國立台灣師範大學‘,‘臺灣師範大學', '台灣師範大學', '師大', 'NTNU'],
    '25.098593,121.537564': ['至善公園'],
    '25.0230556,121.5052778': ['青年公園'],
    '25.1942462,121.5609363': ['陽明山國家公園'],
    '25.02036,121.525342': ['客家文化主題公園', '原兒童交通博物館'],
    '25.0339708,121.5622995': ['台北世界貿易中心', '世貿'],
    '25.0949147,121.5301283': ['士林官邸'],
    '25.0286709,121.5059293': ['南機場夜市'],
    '25.0263075,121.543846': ['大安區'],
    '25.1178477,121.5199001': ['國立台北健康護理大學'],
    '24.9862332,121.575843': ['國立政治大學', '政治大學'],
    '25.0317928,121.56212': ['信義威秀影城'],
    '24.978589,121.586895': ['指南宮'],
    '25.0418254,121.5149875': ['二二八和平紀念公園', '二二八公園'],
    '25.0494444,121.5511111': ['台北田徑場'],
    '24.9914555,121.5412565': ['景美夜市'],
    '25.1188043,121.4716836': ['關渡自然公園'],
    '25.0371623,121.4999007': ['艋舺龍山寺', '龍山寺'],
    '25.0405293,121.6162063': ['胡適紀念館'],
    '25.0861984,121.5281041': ['銘傳大學', '銘傳大學臺北校區', '銘傳大學台北校區'],
    '25.0300344,121.5093427': ['台灣林業陳列館'],
    '25.1033289,121.5311337': ['芝山文化生態綠園'],
    '25.085423,121.5192025': ['台北兒童育樂中心', '兒童育樂中心'],
    '25.0469903,121.522353': ['台北國際藝術村'],
    '25.030009,121.5358478': ['大安森林公園'],
    '25.052932,121.52223': ['台北之家', '光點台北'],
    '25.01482,121.5328885': ['公館夜市'],
    '25.042151,121.5069921': ['西門町'],
    '25.0467156,121.5790871': ['五分埔'],
    '25.0294935,121.4978839': ['萬華區'],
    '25.138948,121.502828': ['北投復興公園'],
    '25.0395251,121.5602453': ['國父紀念館'],
    '25.0645028,121.5133148': ['大同區'],
    '25.101139,121.549488': ['至善園'],
    '25.034731,121.521934': ['中正紀念堂'],
    '25.082837,121.557293': ['美麗華百樂園'],
    '25.0491092,121.5155697': ['台北地下街'],
    '25.10316,121.530785': ['芝山巖惠濟宮'],
    '25.0958292,121.5182994': ['台北市立天文科學教育館'],
    '25.055125,121.5177443': ['寧夏夜市'],
    '25.062946,121.53361': ['行天宮'],
    '25.096607,121.5308677': ['志成公園'],
    '25.0724216,121.524719': ['臺北市立美術館', '北美館'],
    '25.049278,121.5421557': ['遼寧夜市'],
    '25.0441153,121.5199974': ['立法院'],
    '25.0527858,121.5257009': ['林森公園'],
    '25.0453549,121.5311089': ['三創生活園區'],
    '24.9829373,121.5902526': ['貓空'],
    '25.1046983,121.5788672': ['碧山營地', '碧山露營場'],
    '25.083844,121.60214': ['大湖公園'],
    '25.031454,121.511227': ['國立歷史博物館'],
    '25.0574309,121.5097229': ['迪化街'],
    '25.047848,121.561924': ['京華城'],
    '25.0634668,121.5391412': ['榮星公園'],
    '25.0998934,121.5965214': ['龍船岩'],
    '25.078304,121.526137': ['圓山大飯店', '圓山飯店', '台北圓山飯店', '臺北圓山飯店'],
    '25.0727623,121.5161792': ['台北孔廟'],
    '25.038162,121.493488': ['華江雁鴨自然公園'],
    '24.9910542,121.5581177': ['仙跡岩'],
    '25.1682468,121.5742471': ['擎天崗'],
    '25.0826209,121.5830209': ['碧湖公園'],
    '25.0631665,121.5741724': ['台北花市'],
    '25.0454132,121.5319043': ['光華數位新天地', '光華商場'],
    '25.0861769,121.5553568': ['劍潭古寺'],
    '25.0530858,121.5249359': ['康樂公園'],
    '25.138127,121.514714': ['北投文物館'],
    '25.0503762,121.5740581': ['饒河街夜市', '饒河夜市'],
    '25.0430682,121.5919375': ['中央研究院'],
    '25.097094,121.51471': ['台北市立兒童新樂園'],
    '25.0278952,121.5340328': ['清真寺'],
    '25.071749,121.5303571': ['林安泰古厝'],
    '25.0125774,121.5300979': ['自來水園區'],
    '25.1366919,121.5071487': ['地熱谷', '地熱谷景觀公園'],
    '25.0393635,121.5083924': ['國軍歷史文物館'],
    '25.1023554,121.5484925': ['國立故宮博物院', '故宮', '故宮博物院'],
    '25.0428498,121.5378472': ['國立臺北科技大學', '國立台北科技大學', '北科', 'NTUT'],
    '25.1009681,121.5519764': ['順益台灣原住民博物館'],
    '25.087793,121.5242251': ['士林夜市'],
    '24.9880071,121.5752716': ['文山區'],
    '25.0835017,121.5903754': ['內湖區'],
    '25.0505355,121.549902': ['臺北小巨蛋'],
    '25.0493109,121.5170342': ['京站時尚廣場'],
    '25.103893,121.530267': ['芝山岩'],
    '25.0959028,121.529554': ['福林公園']
}
