-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplore_ecoregion_shapefiles_2012.py
More file actions
74 lines (57 loc) · 2.12 KB
/
explore_ecoregion_shapefiles_2012.py
File metadata and controls
74 lines (57 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import fiona
import matplotlib.pyplot as plt
from descartes import PolygonPatch
import pyodbc
import pandas as pd
import numpy as np
import difflib
#from shapely.geometry import Polygpipon, MultiPolygon, shape
ECO_names_shapefile = []
ECO_names_animals = []
with fiona.collection('data\ecoregions_2012\wwf_terr_ecos.shp', "r") as input:
names = []
BLUE = '#6699cc'
fig = plt.figure(1, figsize=(6, 6), dpi=90)
ax = fig.add_subplot(111)
for f in input:
names.append(f['properties']['ECO_NAME'])
ECO_names_shapefile = np.array(list(set(names)))
# plotting the shapes
# for f in input[:1]:
# patch = PolygonPatch(f['geometry'],fc=BLUE ,ec='black',alpha=0.5)
# ax.add_patch(patch)
#plt.autoscale()
#plt.show()
# get the different ecoregions in the animals database
# connect to db
# set up some constants
conn_string = 'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=data\WildfinderUpdate.mdb'
# connect to db
con = pyodbc.connect(conn_string)
cur = con.cursor()
# run a query and get the results
SQL = 'SELECT DISTINCT ECOREGION_NAME FROM ecoregions;'
results = cur.execute(SQL).fetchall()
ECO_names_animals = []
for region in results:
ECO_names_animals.append(region[0])
cur.close()
ECO_names_animals = np.array(ECO_names_animals)
ECO_names_shapefile = np.array(ECO_names_shapefile)
check = np.isin(ECO_names_animals, ECO_names_shapefile)
nr_true = np.count_nonzero(check)
nr_false = np.size(check) - nr_true
missing_eco = ECO_names_animals[~check]
eco_replacement_dict = {}
eco_regions_unmatched = []
# make a dict for matching the missing ecoregions, defaul similarity of 0.6 is used.
for ecoregion in missing_eco:
matches = difflib.get_close_matches(ecoregion, ECO_names_shapefile)
if matches == []:
eco_regions_unmatched.append(ecoregion)
else:
eco_replacement_dict[ecoregion] = difflib.get_close_matches(ecoregion, ECO_names_shapefile)[0]
if not eco_regions_unmatched:
print("succes! all ecoregions have been matched")
else:
print("not alle coregions have been matched. following ecoregions are missing: ", eco_regions_unmatched)