globalforest/databases/populatedb.py

96 lines
3.3 KiB
Python
Raw Permalink Normal View History

2020-09-28 23:18:26 -04:00
from xml.etree import ElementTree as ET
import sys
import sqlite_utils
KML = "{http://www.opengis.net/kml/2.2}"
def iterate_kml(filepath):
fp = open(filepath)
parser = ET.XMLPullParser(["end"])
2020-10-14 18:13:22 -04:00
autoincrement = 0
2020-09-28 23:18:26 -04:00
while True:
chunk = fp.read(1024 * 8)
parser.feed(chunk)
for event, element in parser.read_events():
assert event == "end"
if element.tag == f"{KML}Placemark":
2020-10-14 18:13:22 -04:00
autoincrement += 1
2020-09-28 23:18:26 -04:00
name = element.find(f".//{KML}name").text
description = 'soon'
#description = element.find(f".//{KML}description").text
longitude, latitude, unknown = map(
float, element.find(f".//{KML}coordinates").text.split(",")
)
2020-10-14 18:13:22 -04:00
2020-09-28 23:18:26 -04:00
yield {
2020-10-14 18:13:22 -04:00
"FMID": autoincrement,
"MarketName": name,
"Website": "web_url",
"Facebook": "fb_page_id",
2020-10-14 18:13:22 -04:00
"Twitter": "tw_user",
"Youtube": "yt_user",
"OtherMedia": "om_user",
"street": "1 afxhq",
"city": "none",
"County": "none",
"State": "none",
"zip": "none",
"Season1Date": "1970-01-01",
"Season1Time": "",
"Season2Date": "",
"Season2Time": "",
"Season3Date": "",
"Season3Time": "",
"Season4Date": "",
"Season4Time": "",
"x": latitude,
"y": longitude,
"Location": description,
"Credit": "",
"WIC": "",
"WICcash": "",
"SFMNP": "",
"SNAP": "",
"Organic": "",
"Bakedgoods": "",
"Cheese": "",
"Crafts": "",
"Flowers": "",
"Eggs": "",
"Seafood": "",
"Herbs": "",
"Vegetables": "",
"Honey": "",
"Jams": "",
"Maple": "",
"Meat": "",
"Nursery": "",
"Nuts": "",
"Plants": "",
"Poultry": "",
"Prepared": "",
"Soap": "",
"Trees": "",
"Wine": "",
"Coffee": "",
"Beans": "",
"Fruits": "",
"Grains": "",
"Juices": "",
"Mushrooms": "",
"PetFood": "",
"Tofu": "",
"WildHarvested": "",
"updateTime": ""
}
2020-09-28 23:18:26 -04:00
if not chunk:
break
if __name__ == "__main__":
db = sqlite_utils.Database("fountains.db")
2020-10-14 18:13:22 -04:00
db["locations"].insert_all(iterate_kml(sys.argv[-1]))