Mercurial > hg > plosone_underreview
comparison scripts/utils_spatial.py @ 8:0f3eba42b425 branch-tests
added notebooks and utils
author | Maria Panteli <m.x.panteli@gmail.com> |
---|---|
date | Mon, 11 Sep 2017 18:23:14 +0100 |
parents | e50c63cf96be |
children | 98718fdd8326 |
comparison
equal
deleted
inserted
replaced
7:46b2c713cc73 | 8:0f3eba42b425 |
---|---|
6 """ | 6 """ |
7 import numpy as np | 7 import numpy as np |
8 import json | 8 import json |
9 import pysal # before shapely in util_plots | 9 import pysal # before shapely in util_plots |
10 import fiona | 10 import fiona |
11 import os | |
11 import sys | 12 import sys |
12 sys.path.append('../misc') | 13 sys.path.append('../misc') |
13 import matplotlib.pyplot as plt | 14 import matplotlib.pyplot as plt |
14 | 15 |
15 | 16 |
16 def neighbors_from_json_file(data_countries, json_DB='../MergeBL-Smith/data/countries.json'): | 17 DATA_DIR = os.path.join(os.path.dirname(__file__), 'util_data') |
18 JSON_DB = os.path.join(DATA_DIR, 'countries.json') | |
19 SHAPEFILE = os.path.join(DATA_DIR, 'shapefiles', 'ne_10m_admin_0_countries.shp') | |
20 | |
21 | |
22 def neighbors_from_json_file(data_countries, json_DB=JSON_DB): | |
17 neighbors = {} | 23 neighbors = {} |
18 with open(json_DB) as json_file: | 24 with open(json_DB) as json_file: |
19 countries_dict = json.load(json_file) | 25 countries_dict = json.load(json_file) |
20 country_names = [] | 26 country_names = [] |
21 country_iso = [] | 27 country_iso = [] |
58 shp.close() | 64 shp.close() |
59 return countries | 65 return countries |
60 | 66 |
61 | 67 |
62 def replace_empty_neighbours_with_KNN(data_countries, w): | 68 def replace_empty_neighbours_with_KNN(data_countries, w): |
63 shapefile = "../MergeBL-Smith/shapefiles/ne_10m_admin_0_countries.shp" | 69 shapefile = SHAPEFILE |
64 no_neighbors_idx = w.islands | 70 no_neighbors_idx = w.islands |
65 knn = 10 | 71 knn = 10 |
66 wknn = pysal.knnW_from_shapefile(shapefile, knn) | 72 wknn = pysal.knnW_from_shapefile(shapefile, knn) |
67 knn_countries = get_countries_from_shapefile(shapefile) | 73 knn_countries = get_countries_from_shapefile(shapefile) |
68 neighbors = w.neighbors | 74 neighbors = w.neighbors |