# Path: farmsList/settings.py
# class ProdConfig(Config):
#     """Production configuration."""
#     ENV = 'prod'
#     DEBUG = False
#     SQLALCHEMY_DATABASE_URI = os_env.get('HEROKU_POSTGRESQL_IVORY_URL')
#     DEBUG_TB_ENABLED = False  # Disable Debug toolbar
#     HTTP_SERVER = "acres.herokuapp.com"
# 
# class DevConfig(Config):
#     """Development configuration."""
#     ENV = 'dev'
#     DEBUG = True
#     MAIL_DEBUG = True
#     DB_NAME = 'dev.db'
#     # Put the db file in project root
#     # DB_PATH = os.path.join(Config.PROJECT_ROOT, DB_NAME)
#     SQLALCHEMY_DATABASE_URI = 'postgresql://urbanlandlocatoradmin:@localhost/urban_land_locator' #'sqlite:///{0}'.format(DB_PATH)
#     DEBUG_TB_ENABLED = True
#     ASSETS_DEBUG = True  # Don't bundle/minify static assets
#     HTTP_SERVER = "localhost:5001"


import os
import json
import xlrd
import urllib2
from sqlalchemy import create_engine
from farmsList.settings import ProdConfig, DevConfig
		conn.execute("DROP TABLE t_parcel_to_soil")

class WestSacWaterUpdater():
	def update(self):
		print 'code goes here'

class FoodDesertUpdater():
	def update(self):
		# Grab the food desert excel file from the USDA ERS website and get it into a python object we can use
		xmlBinaryString = urllib2.urlopen('http://www.ers.usda.gov/datafiles/Food_Access_Research_Atlas/Download_the_Data/Current_Version/DataDownload.xlsx').read()
		workbook = xlrd.open_workbook(file_contents=xmlBinaryString)
		sheet = workbook.sheet_by_name('Food Access Research Atlas Data')
		foodDesertCensusTracts = []
		debugPrint('Got the USDA Data.')

		# Now go through and grab all the census tracts that qualify as any of the 4 publicly atlased USDA ERS Food Desert Measures
		for rownum in xrange(sheet.nrows):
			if rownum == 0:
				continue
			rowValues = sheet.row_values(rownum)
			if rowValues[1] == 'CA' and rowValues[2] in ['El Dorado', 'Placer', 'Sacramento', 'Sutter', 'Yolo', 'Yuba'] and 0 < sum(rowValues[3:6]):
				foodDesertCensusTracts.append(int(rowValues[0]))
		debugPrint('Found the food deserts in SACOG region.')

		# Finally build a geojson layer from the 2010 census tract boundaries file with the tracts that are 'food deserts'
		foodDesertGeomStrings = []
		for censusTract in foodDesertCensusTracts:
			# censusTractFile = open('{}/parcels-pristine/foodDesertsCalifornia.geojson'.format(filepath), 'r')
			censusTractFile = open('parcels-pristine/foodDesertsCalifornia.geojson', 'r')
			censusTracts = json.loads(censusTractFile.read())

# Path: farmsList/settings.py
# class ProdConfig(Config):
#     """Production configuration."""
#     ENV = 'prod'
#     DEBUG = False
#     SQLALCHEMY_DATABASE_URI = os_env.get('HEROKU_POSTGRESQL_IVORY_URL')
#     DEBUG_TB_ENABLED = False  # Disable Debug toolbar
#     HTTP_SERVER = "acres.herokuapp.com"
# 
# class DevConfig(Config):
#     """Development configuration."""
#     ENV = 'dev'
#     DEBUG = True
#     MAIL_DEBUG = True
#     DB_NAME = 'dev.db'
#     # Put the db file in project root
#     # DB_PATH = os.path.join(Config.PROJECT_ROOT, DB_NAME)
#     SQLALCHEMY_DATABASE_URI = 'postgresql://urbanlandlocatoradmin:@localhost/urban_land_locator' #'sqlite:///{0}'.format(DB_PATH)
#     DEBUG_TB_ENABLED = True
#     ASSETS_DEBUG = True  # Don't bundle/minify static assets
#     HTTP_SERVER = "localhost:5001"


# Path: farmsList/updaters.py
import os
import json
import xlrd
import urllib2
from sqlalchemy import create_engine
from farmsList.settings import ProdConfig, DevConfig

		conn.execute("DROP TABLE t_parcel_to_soil")

class WestSacWaterUpdater():
	def update(self):
		print 'code goes here'

class FoodDesertUpdater():
	def update(self):
		# Grab the food desert excel file from the USDA ERS website and get it into a python object we can use
		xmlBinaryString = urllib2.urlopen('http://www.ers.usda.gov/datafiles/Food_Access_Research_Atlas/Download_the_Data/Current_Version/DataDownload.xlsx').read()
		workbook = xlrd.open_workbook(file_contents=xmlBinaryString)
		sheet = workbook.sheet_by_name('Food Access Research Atlas Data')
		foodDesertCensusTracts = []
		debugPrint('Got the USDA Data.')

		# Now go through and grab all the census tracts that qualify as any of the 4 publicly atlased USDA ERS Food Desert Measures
		for rownum in xrange(sheet.nrows):
			if rownum == 0:
				continue
			rowValues = sheet.row_values(rownum)
			if rowValues[1] == 'CA' and rowValues[2] in ['El Dorado', 'Placer', 'Sacramento', 'Sutter', 'Yolo', 'Yuba'] and 0 < sum(rowValues[3:6]):
				foodDesertCensusTracts.append(int(rowValues[0]))
		debugPrint('Found the food deserts in SACOG region.')

		# Finally build a geojson layer from the 2010 census tract boundaries file with the tracts that are 'food deserts'
		foodDesertGeomStrings = []
		for censusTract in foodDesertCensusTracts:
			# censusTractFile = open('{}/parcels-pristine/foodDesertsCalifornia.geojson'.format(filepath), 'r')
			censusTractFile = open('parcels-pristine/foodDesertsCalifornia.geojson', 'r')
			censusTracts = json.loads(censusTractFile.read())

			for tract in censusTracts['features']: