gazetteer/gazetteer/places/imports.py

48 lines
1.2 KiB
Python
Raw Normal View History

2011-08-18 22:44:56 +05:30
import csv
from settings import DATA_DIR
from os.path import join
2011-08-19 18:35:24 +05:30
from places.models import Feature, FeatureType
2011-08-18 22:44:56 +05:30
from django.contrib.gis.geos import Point
2011-08-19 18:35:24 +05:30
def import_ftypes(f):
2011-08-18 22:44:56 +05:30
t = csv.reader(f, delimiter="\t")
for row in t:
2011-08-19 18:35:24 +05:30
ft = FeatureType()
ft.feature_class = row[0]
ft.code = row[1]
ft.name = row[2]
try:
ft.description = row[3]
except:
ft.description = ''
ft.save()
print "saved " + ft.name
def import_gazetteer(f, limit):
t = csv.reader(f, delimiter="\t")
i = 0
for row in t:
ft = Feature()
if Feature.objects.filter(url=row[0]).count() > 0:
2011-08-18 22:44:56 +05:30
print "duplicate row " + row[0]
else:
2011-08-19 18:35:24 +05:30
ft.url = row[0]
ft.preferred_name = row[1]
2011-08-18 22:44:56 +05:30
try:
2011-08-19 18:35:24 +05:30
fcode = FeatureType.objects.get(code=row[2])
2011-08-18 22:44:56 +05:30
except:
2011-08-19 18:35:24 +05:30
fcode = None
2011-08-18 22:44:56 +05:30
2011-08-19 18:35:24 +05:30
ft.feature_type = fcode
ft.admin1 = row[4]
ft.admin2 = row[5]
ft.geometry = Point(float(row[7]), float(row[6]))
2011-08-19 18:35:24 +05:30
ft.save()
print "saved " + ft.preferred_name
i += 1
if i > limit:
break
2011-08-18 22:44:56 +05:30