Пример #1
0
def resolve_nuts(row):
    if not 'regions' in RES:
        RES['regions'] = load_region_hierarchy()
    if not 'geocoder' in RES:
        RES['geocoder'] = shapegeocode.geocoder(
            reference_data_path('nuts2-shapefile/data/NUTS_RG_10M_2006.shp'),
            filter=lambda r: r['STAT_LEVL_'] == 3)

    try:
        data = RES['geocoder'].geocode(float(row['lat']), float(row['lon']))
        if data is None:
            return {}
        nuts3_code = data.get('NUTS_ID')
        nuts3 = RES['regions'].get(nuts3_code, {})
        nuts2 = RES['regions'].get(nuts3_code[:4], {})
        nuts1 = RES['regions'].get(nuts3_code[:3], {})
        return {
            'nuts3': nuts3_code,
            'nuts3_label': nuts3.get('LABEL'),
            'nuts2': nuts3_code[:4],
            'nuts2_label': nuts2.get('LABEL'),
            'nuts1': nuts3_code[:3],
            'nuts1_label': nuts1.get('LABEL'),
            }
    except Exception, e:
        log.exception(e)
        return {}  
Пример #2
0
def merge():
    geocoder = shapegeocode.geocoder(
        'nuts2-shapefile/data/NUTS_RG_10M_2006.shp',
        filter=lambda r: r['STAT_LEVL_'] == 3)
    regions = load_region_hierarchy()
    engine = util.make_engine()
    table = sl.get_table(engine, 'fts')
    for row in sl.distinct(engine, table, *KEYS):
        loc = geocode(row)
        if loc is None:
            continue
        row.update(loc)
        reg = find_region(geocoder, regions, row)
        row.update(reg)
        log.info("Geocoded: %s/%s - %s",
            row['lat'], row['lon'], row.get('nuts3_label'))
        sl.upsert(engine, table, row, KEYS)
Пример #3
0
    def __init__(self):
        myshp = open("MEOW/meow_ecos.shp", "rb")
        mydbf = open("MEOW/meow_ecos.dbf", "rb")
        self.gc = shapegeocode.geocoder(
            'World_EEZ_v9_20161021/eez_boundaries.shp')

        self.r = shapefile.Reader(shp=myshp, dbf=mydbf)

        self.fout = open('meow.csv', 'w', encoding='utf8', newline='')
        self.csv_writer = csv.writer(self.fout)

        row = ('id', 'ECO_CODE', 'ECOREGION', 'PROV_CODE', 'PROVINCE',
               'RLM_CODE', 'REALM', 'ALT_CODE', 'ECO_CODE_X', 'Lat_Zone',
               'Country1', 'Country2', 'Country3', 'Country4', 'Country5',
               'Country6')
        self.csv_writer.writerow(row)

        self.shapes = self.r.shapes()
        self.records = self.r.records()
        self.total_cnt = len(self.shapes)
Пример #4
0
    while len(q) > 0:
        current = q.pop()
        elems.nodes[current].valid = True
        for e in elems.nodes[current].edges:
            if not edges[e].valid:
                edges[e].valid = True
                pair = edges[e].getPair(current)
                elems.nodes[pair].valid = True
                q.append(pair)

end = timer()  #DEBUGGING
print "Time: " + str(end - start)  #DEBUGGING

print "Geocoding"  #DEBUGGING
start = timer()  #DEBUGGING
gc = shapegeocode.geocoder(
    "./SHP/tl_2013_06_place.shp")  #USER: Edit shapefile name here
elems.geocode(gc)
end = timer()  #DEBUGGING
print "Time: " + str(end - start)  #DEBUGGING

##DEBUGGING
#for n in map(lambda k: elems.nodes[k], elems.nodes):
#    invalidEdges = len(filter(lambda e: not edges[e].valid, n.edges))
#    if invalidEdges > 0:
#        print "Node " + n.ref + " connected to " + invalidEdges + " invalidEdges"

##Get filename
valid = False
while not valid:
    filename = raw_input("Enter output filename: ")
    if (posixpath.isfile(filename)):
Пример #5
0
import shapegeocode
import csv

gc = shapegeocode.geocoder('World_EEZ_v9_20161021/eez.shp')
fin = open('marineregions_input_example.csv', 'r')
fout = open('marineregions_output_example.csv',
            'w',
            encoding='utf8',
            newline='')

#csv_reader = csv.DictReader(fin)
csv_reader = csv.reader(fin)
csv_writer = csv.writer(fout)
i = 0

print(csv_reader)
for row in csv_reader:
    if i == 0:
        row.extend(['Name', 'Sovereign', 'Success'])
        pass
    else:
        lat = float(row[1])
        lon = float(row[2])
        #lat = float(row['Latitude'])
        #lon = float(row['Longitude'])
        geocode = gc.geocode(lat, lon, max_dist=0)

        if geocode is not None:
            #row['Name'] = geocode['GeoName']
            #row['Sovereign'] = geocode['Sovereign1']
            #row['Success'] = 'FOUND'
if __name__ == "__main__":

	zip_file = "nyc_zip_codes.csv"
	subway_csv = "DOITT_SUBWAY_ENTRANCE_01_13SEPT2010.csv"
	nyc_shapefile = "nyc/nyc"

	file_name = sys.argv[1]
	lat_index = int(sys.argv[2])
	lon_index = int(sys.argv[3])

	subway_points = get_subway_points(subway_csv)
	tree = cKDTree(subway_points)

	zip_to_hood = get_zip_to_hood_mapping(zip_file)

	gc = shapegeocode.geocoder(nyc_shapefile)
	out_file = open(file_name+"_location", 'w')
	writer = csv.writer(out_file, delimiter=',')

	removed_count = 0
	total_count = 0

	with open(file_name, 'rb') as f:
		print "Running..."
		reader = csv.reader(f)
		for row in reader:
			try:
				lat = float(row[lat_index])
				lon = float(row[lon_index])

				subway_distance, index = tree.query(numpy.array([lat, lon]), k=1)