""" NOTE: below there are queries that are specific to TriMet's geo database (PostGIS), and a landmarks table.
"""
import os
from ott.utils import file_utils
from sqlalchemy.orm import Session
from .db_exporter import DbExporter

import logging
log = logging.getLogger(__file__)


# map TriMet landmark types to something more readable / relatable
LANDMARK_TYPES = {}
#LANDMARK_TYPES[2] = 'airport' # maybe we do this if we need the PDX alias
LANDMARK_TYPES[7] = 'station'
LANDMARK_TYPES[10] = 'pr'
LANDMARK_TYPES[14] = 'tc'
LANDMARK_TYPES[15] = 'facility'
LANDMARK_TYPES[16] = 'fare_outlet'
LANDMARK_TYPES[17] = 'br'
LANDMARK_TYPES[26] = 'tvm'


class Landmarks(DbExporter):
    """ export Transit data from
    """
    def __init__(self):
        super(Landmarks, self).__init__()
        csv_output = self.config.get('landmarks_csv', section='geocoder', def_val="TRIMET-landmarks.csv")
        self.file_path = os.path.join(self.cache_dir, csv_output)

    def query_and_output(self):
        LandmarksOrm = self.get_table_orm('landmark_ext')
        session = Session(self.engine)
        fp = open(self.file_path, 'wb')
        csv_writer = file_utils.make_csv_writer(fp, self.csv_columns)

        type_ids = LANDMARK_TYPES.keys()
        for i, a in enumerate(session.query(LandmarksOrm).all()):
            if a.type in type_ids:
                row = {'id': a.id, 'name': a.name, 'address': a.address, 'zipcode': a.zip_code, 'lon': a.lon, 'lat': a.lat, 'layer_id': LANDMARK_TYPES[a.type]}
                csv_writer.writerow(row)

    @classmethod
    def export(cls):
        l = Landmarks()
        l.query_and_output()

@@ -361,17 +361,16 @@
 ES = %7B%7D%0A
-#
 LANDMARK
@@ -390,16 +390,17 @@
 airport'
+ 
  # maybe
@@ -436,16 +436,53 @@
 X alias%0A
+LANDMARK_TYPES%5B9%5D = %22transportation%22%0A
 LANDMARK
@@ -555,16 +555,42 @@
  = 'tc'%0A
+LANDMARK_TYPES%5B17%5D = 'br'%0A
 LANDMARK
@@ -667,45 +667,630 @@
 PES%5B
-17%5D = 'br'%0ALANDMARK_TYPES%5B26%5D = 'tvm'
+26%5D = 'tvm'%0A%0ALANDMARK_TYPES%5B1%5D  = %22activity_center%22%0ALANDMARK_TYPES%5B22%5D = %22community_center%22%0ALANDMARK_TYPES%5B23%5D = %22town_center%22%0ALANDMARK_TYPES%5B18%5D = %22social_service%22%0ALANDMARK_TYPES%5B6%5D  = %22library%22%0ALANDMARK_TYPES%5B3%5D  = %22city_hall%22%0ALANDMARK_TYPES%5B25%5D = %22government_agency%22%0ALANDMARK_TYPES%5B11%5D = %22post_office%22%0ALANDMARK_TYPES%5B4%5D  = %22fire_station%22%0ALANDMARK_TYPES%5B20%5D = %22correctional_facility%22%0A%0ALANDMARK_TYPES%5B5%5D  = %22hospital%22%0ALANDMARK_TYPES%5B24%5D = %22medical_clinic%22%0ALANDMARK_TYPES%5B19%5D = %22major_employer%22%0A%0ALANDMARK_TYPES%5B8%5D  = %22museum%22%0ALANDMARK_TYPES%5B12%5D = %22school%22%0ALANDMARK_TYPES%5B13%5D = %22shopping%22%0ALANDMARK_TYPES%5B21%5D = %22hotel_motel%22
 %0A%0A%0Ac

add all landmarks