# import libraries
import os
from zipfile import ZipFile
import fiona
import geopandas as gpd
import matplotlib.pyplot as plt
# configure plot styles
plt.style.use("Solarize_Light2")
plt.rcParams["font.family"] = "Source Sans Pro"
plt.rcParams["figure.dpi"] = 96
plt.rcParams["axes.grid"] = False
plt.rcParams["text.color"] = "darkslategrey"
plt.rcParams["axes.labelcolor"] = "darkslategrey"
plt.rcParams["xtick.labelcolor"] = "darkslategrey"
plt.rcParams["ytick.labelcolor"] = "darkslategrey"
plt.rcParams["figure.titleweight"] = "semibold"
plt.rcParams["axes.titleweight"] = "semibold"
plt.rcParams["figure.titlesize"] = "13"
plt.rcParams["axes.titlesize"] = "12"
plt.rcParams["axes.labelsize"] = "10"
BASE2 = "#eee8d5"
BASE3 = "#fdf6e3"
# define data directories
BASE_PATH = os.path.join("data", "raw", "vector")
OUT_PATH = os.path.join("data", "vector")
ZIP_FILE = os.path.join(BASE_PATH, "os_bdline", "data.zip")
archive = ZipFile(ZIP_FILE, "r")
files = [name for name in archive.namelist() if name.endswith(".gpkg")]
if len(files) == 1:
layers = fiona.listlayers("zip://" + ZIP_FILE + "!" + files[0])
layers
layers
['boundary_line_ceremonial_counties', 'boundary_line_historic_counties', 'community_ward', 'country_region', 'county', 'county_electoral_division', 'district_borough_unitary', 'district_borough_unitary_ward', 'english_region', 'greater_london_const', 'high_water', 'historic_european_region', 'parish', 'polling_districts_england', 'scotland_and_wales_const', 'scotland_and_wales_region', 'unitary_electoral_division', 'westminster_const']
bd_line = gpd.read_file(
"zip://" + ZIP_FILE + "!" + files[0], layer="scotland_and_wales_const"
)
bd_line.head(5)
Name | Area_Code | Area_Description | File_Name | Feature_Serial_Number | Collection_Serial_Number | Global_Polygon_ID | Admin_Unit_ID | Census_Code | Hectares | Non_Inland_Area | Area_Type_Code | Area_Type_Description | Non_Area_Type_Code | Non_Area_Type_Description | geometry | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | Cumbernauld and Kilsyth P Const | SPC | Scottish Parliament Constituency | CENTRAL_SCOTLAND_PER | 1 | 1 | 124579 | 41304 | S16000093 | 10499.477 | 0.000 | VA | CIVIL VOTING AREA | None | None | MULTIPOLYGON (((278764.300 678216.902, 278765.... |
1 | Airdrie and Shotts P Const | SPC | Scottish Parliament Constituency | CENTRAL_SCOTLAND_PER | 2 | 2 | 124581 | 41531 | S16000079 | 21390.356 | 0.000 | VA | CIVIL VOTING AREA | None | None | MULTIPOLYGON (((286129.201 668542.601, 286107.... |
2 | Uddingston and Bellshill P Const | SPC | Scottish Parliament Constituency | CENTRAL_SCOTLAND_PER | 3 | 3 | 124752 | 41523 | S16000146 | 5285.806 | 0.000 | VA | CIVIL VOTING AREA | None | None | MULTIPOLYGON (((270152.202 656905.802, 270071.... |
3 | Motherwell and Wishaw P Const | SPC | Scottish Parliament Constituency | CENTRAL_SCOTLAND_PER | 4 | 4 | 124750 | 41376 | S16000133 | 5032.236 | 0.000 | VA | CIVIL VOTING AREA | None | None | MULTIPOLYGON (((279874.102 651119.498, 279847.... |
4 | Falkirk West P Const | SPC | Scottish Parliament Constituency | CENTRAL_SCOTLAND_PER | 5 | 5 | 124895 | 41377 | S16000113 | 10923.082 | 11.641 | VA | CIVIL VOTING AREA | None | None | MULTIPOLYGON (((274204.699 683523.603, 274215.... |
list(bd_line)
['Name', 'Area_Code', 'Area_Description', 'File_Name', 'Feature_Serial_Number', 'Collection_Serial_Number', 'Global_Polygon_ID', 'Admin_Unit_ID', 'Census_Code', 'Hectares', 'Non_Inland_Area', 'Area_Type_Code', 'Area_Type_Description', 'Non_Area_Type_Code', 'Non_Area_Type_Description', 'geometry']
list(bd_line["Area_Code"].unique())
['SPC', 'WPC']
bd_line = bd_line.loc[bd_line["Area_Code"] == "SPC"]
bd_line.shape
(73, 16)
bd_line.crs
<Derived Projected CRS: EPSG:27700> Name: OSGB36 / British National Grid Axis Info [cartesian]: - E[east]: Easting (metre) - N[north]: Northing (metre) Area of Use: - name: United Kingdom (UK) - offshore to boundary of UKCS within 49°45'N to 61°N and 9°W to 2°E; onshore Great Britain (England, Wales and Scotland). Isle of Man onshore. - bounds: (-9.0, 49.75, 2.01, 61.01) Coordinate Operation: - name: British National Grid - method: Transverse Mercator Datum: Ordnance Survey of Great Britain 1936 - Ellipsoid: Airy 1830 - Prime Meridian: Greenwich
base = bd_line.plot(color=BASE3, figsize=(7.5, 7.5))
bd_line.boundary.plot(ax=base, color="darkslategrey", linewidth=.4)
plt.title("OS Boundary-Line - Scottish Constituencies")
plt.xlabel("Easting (m)")
plt.ylabel("Northing (m)")
plt.text(
-1e4, 5.05e5,
"Contains OS data © Crown copyright and database right 2021"
)
base.set_aspect("equal", "box")
plt.show()
# dissolve boundaries
scotland = bd_line.dissolve(by="Area_Code")
scotland["Name"] = "Scotland"
scotland = scotland[["Name", "geometry"]]
scotland
Name | geometry | |
---|---|---|
Area_Code | ||
SPC | Scotland | MULTIPOLYGON (((55661.497 780686.500, 55667.49... |
base = scotland.plot(color=BASE3, figsize=(7.5, 7.5))
scotland.boundary.plot(ax=base, color="darkslategrey", linewidth=.4)
plt.title("OS Boundary-Line - Scotland")
plt.xlabel("Easting (m)")
plt.ylabel("Northing (m)")
plt.text(
-1e4, 5.05e5,
"Contains OS data © Crown copyright and database right 2021"
)
base.set_aspect("equal", "box")
plt.show()
# Western Isles
w_isles = bd_line.loc[bd_line["Name"] == "Na h-Eileanan an Iar P Const"]
w_isles
Name | Area_Code | Area_Description | File_Name | Feature_Serial_Number | Collection_Serial_Number | Global_Polygon_ID | Admin_Unit_ID | Census_Code | Hectares | Non_Inland_Area | Area_Type_Code | Area_Type_Description | Non_Area_Type_Code | Non_Area_Type_Description | geometry | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
104 | Na h-Eileanan an Iar P Const | SPC | Scottish Parliament Constituency | HIGHLANDS_AND_ISLANDS_PER | 105 | 105 | 137443 | 177688 | S16000110 | 327020.741 | 17359.266 | VA | CIVIL VOTING AREA | None | None | MULTIPOLYGON (((152019.227 966602.488, 152022.... |
base = w_isles.plot(color=BASE3, figsize=(7, 7))
w_isles.boundary.plot(ax=base, color="darkslategrey", linewidth=.4)
plt.title("OS Boundary-Line - Western Isles Constituency")
plt.xlabel("Easting (m)")
plt.ylabel("Northing (m)")
plt.text(
0, 7.7e5,
"Contains OS data © Crown copyright and database right 2021"
)
base.set_aspect("equal", "box")
plt.show()
ZIP_FILE = os.path.join(BASE_PATH, "ms_smp", "data.zip")
archive = ZipFile(ZIP_FILE, "r")
files = [name for name in archive.namelist() if name.endswith(".shp")]
if len(files) == 1:
print(fiona.listlayers("zip://" + ZIP_FILE + "!" + files[0]))
['energy_resources_smp_wind_plan_optionsPolygon']
smp = gpd.read_file("zip://" + ZIP_FILE + "!" + files[0])
smp.head(5)
name | copyright | date | comments | geometry | |
---|---|---|---|---|---|
0 | W1 | Open Government Licence. The following attribu... | 20200618 | None | POLYGON ((-6.56221 56.13845, -6.54968 56.13450... |
1 | E1 | Open Government Licence. The following attribu... | 20200618 | None | POLYGON ((-0.47392 56.91518, -0.45823 56.91451... |
2 | E3 | Open Government Licence. The following attribu... | 20200618 | None | POLYGON ((-1.59934 57.08059, -1.58427 57.08020... |
3 | E2 | Open Government Licence. The following attribu... | 20200618 | None | POLYGON ((-0.50129 57.47805, -0.48542 57.47756... |
4 | NE6 | Open Government Licence. The following attribu... | 20200618 | None | POLYGON ((-1.69815 58.18771, -1.68571 58.18225... |
list(smp)
['name', 'copyright', 'date', 'comments', 'geometry']
smp.shape
(15, 5)
smp.crs
<Geographic 2D CRS: GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84" ...> Name: WGS 84 Axis Info [ellipsoidal]: - lon[east]: Longitude (degree) - lat[north]: Latitude (degree) Area of Use: - undefined Datum: World Geodetic System 1984 - Ellipsoid: WGS 84 - Prime Meridian: Greenwich
if smp.crs != 27700:
smp = smp.to_crs("epsg:27700")
smp.crs
<Derived Projected CRS: EPSG:27700> Name: OSGB36 / British National Grid Axis Info [cartesian]: - E[east]: Easting (metre) - N[north]: Northing (metre) Area of Use: - name: United Kingdom (UK) - offshore to boundary of UKCS within 49°45'N to 61°N and 9°W to 2°E; onshore Great Britain (England, Wales and Scotland). Isle of Man onshore. - bounds: (-9.0, 49.75, 2.01, 61.01) Coordinate Operation: - name: British National Grid - method: Transverse Mercator Datum: Ordnance Survey of Great Britain 1936 - Ellipsoid: Airy 1830 - Prime Meridian: Greenwich
base = scotland.plot(color=BASE3, figsize=(7, 7))
scotland.boundary.plot(ax=base, color="darkslategrey", linewidth=.4)
smp.plot(ax=base, cmap="tab20b", column="name", legend=True)
plt.title("Sectoral Marine Plan options")
plt.text(
-1e4, 5.05e5,
"Contains OS & Marine Scotland data " +
"© Crown copyright and database right 2021"
)
plt.xlim(-2.5e4, 6.75e5)
plt.show()
# keep only N4
N4 = smp.loc[smp["name"] == "N4"]
N4
name | copyright | date | comments | geometry | |
---|---|---|---|---|---|
7 | N4 | Open Government Licence. The following attribu... | 20200618 | None | POLYGON ((132903.635 958763.249, 132097.526 95... |
# area of Sectoral Marine Plan Option N4
print("Area of N4:", int(N4.area / (1000 ** 2)), "sq. km")
Area of N4: 200 sq. km
base = w_isles.plot(color=BASE3, linewidth=.5, figsize=(7, 7))
w_isles.boundary.plot(ax=base, color="darkslategrey", linewidth=.5)
N4.plot(ax=base, color="powderblue")
N4.boundary.plot(ax=base, color="darkslategrey")
map_labels = zip(zip(N4.centroid.x-1500, N4.centroid.y-750), N4["name"])
for xy, lab in map_labels:
base.annotate(
text=lab, xy=xy, textcoords="data", size=16,
weight="semibold", rotation=32.5
)
plt.title("Sectoral Marine Plan option N4 adjacent to the Isle of Lewis")
plt.xlabel("Easting (m)")
plt.ylabel("Northing (m)")
plt.text(
9.1e4, 9.013e5,
"Contains OS & Marine Scotland data " +
"© Crown copyright and database right 2021",
backgroundcolor=BASE2
)
base.set_aspect("equal", "box")
plt.xlim(9e4, 1.6e5)
plt.ylim(9e5, 9.7e5)
plt.show()
study_area = gpd.GeoDataFrame(geometry=N4.buffer(15000), crs="EPSG:27700")
study_area["Name"] = (
"Study area boundary around Sectoral Marine Plan option N4"
)
study_area
geometry | Name | |
---|---|---|
7 | POLYGON ((144479.797 968302.247, 145349.028 96... | Study area boundary around Sectoral Marine Pla... |
base = w_isles.plot(color=BASE3, linewidth=.5, figsize=(7, 7))
w_isles.boundary.plot(ax=base, color="darkslategrey", linewidth=.5)
N4.plot(ax=base, color="powderblue")
N4.boundary.plot(ax=base, color="darkslategrey")
map_labels = zip(zip(N4.centroid.x-1500, N4.centroid.y-750), N4["name"])
for xy, lab in map_labels:
base.annotate(
text=lab, xy=xy, textcoords="data", size=16,
weight="semibold", rotation=32.5
)
study_area.boundary.plot(ax=base, color="crimson")
plt.title(
"Study area around Sectoral Marine Plan option N4 adjacent to the " +
"Isle of Lewis"
)
plt.xlabel("Easting (m)")
plt.ylabel("Northing (m)")
plt.text(
9.1e4, 9.163e5,
"Contains OS & Marine Scotland data " +
"© Crown copyright and database right 2021",
backgroundcolor=BASE2
)
base.set_aspect("equal", "box")
plt.xlim(9e4, 1.6e5)
plt.ylim(9.15e5, 9.85e5)
plt.show()
ZIP_FILE = os.path.join(BASE_PATH, "is_ccb", "data.zip")
archive = ZipFile(ZIP_FILE, "r")
files = [name for name in archive.namelist() if name.endswith(".shp")]
if len(files) == 1:
print(fiona.listlayers("zip://" + ZIP_FILE + "!" + files[0]))
['pub_commcnc']
ccb = gpd.read_file("zip://" + ZIP_FILE + "!" + files[0], bbox=study_area)
ccb.head(5)
la_s_code | local_auth | cc_name | active | url | sh_date_up | sh_src | sh_src_id | geometry | |
---|---|---|---|---|---|---|---|---|---|
0 | S12000013 | Eilean Siar | Airidhantuim | Yes | https://www.eastrenfrewshire.gov.uk/community-... | 2019-07-01 | 3675c98c-c1e8-4018-b7f9-55143061d698 | 18.0 | POLYGON ((139665.630 941880.130, 139534.000 94... |
1 | S12000013 | Eilean Siar | Barvas and Brue | No | https://www.cne-siar.gov.uk/your-council/commu... | 2019-07-01 | 3675c98c-c1e8-4018-b7f9-55143061d698 | 17.0 | POLYGON ((134908.570 944574.000, 134828.440 94... |
2 | S12000013 | Eilean Siar | Bernera | Yes | https://www.cne-siar.gov.uk/your-council/commu... | 2019-07-01 | 3675c98c-c1e8-4018-b7f9-55143061d698 | 7.0 | MULTIPOLYGON (((122018.330 929605.560, 122020.... |
3 | S12000013 | Eilean Siar | Back | Yes | https://www.cne-siar.gov.uk/your-council/commu... | 2019-07-01 | 3675c98c-c1e8-4018-b7f9-55143061d698 | 16.0 | POLYGON ((144705.400 938523.050, 144694.550 93... |
4 | S12000013 | Eilean Siar | Breasclete | Yes | https://www.cne-siar.gov.uk/your-council/commu... | 2019-07-01 | 3675c98c-c1e8-4018-b7f9-55143061d698 | 5.0 | POLYGON ((131530.760 935035.200, 131527.460 93... |
ccb.shape
(16, 9)
list(ccb)
['la_s_code', 'local_auth', 'cc_name', 'active', 'url', 'sh_date_up', 'sh_src', 'sh_src_id', 'geometry']
ccb.crs
<Derived Projected CRS: EPSG:27700> Name: OSGB36 / British National Grid Axis Info [cartesian]: - E[east]: Easting (metre) - N[north]: Northing (metre) Area of Use: - name: United Kingdom (UK) - offshore to boundary of UKCS within 49°45'N to 61°N and 9°W to 2°E; onshore Great Britain (England, Wales and Scotland). Isle of Man onshore. - bounds: (-9.0, 49.75, 2.01, 61.01) Coordinate Operation: - name: British National Grid - method: Transverse Mercator Datum: Ordnance Survey of Great Britain 1936 - Ellipsoid: Airy 1830 - Prime Meridian: Greenwich
base = w_isles.plot(color=BASE3, figsize=(9, 7))
N4.boundary.plot(ax=base, color="darkslategrey")
map_labels = zip(zip(N4.centroid.x-1500, N4.centroid.y-750), N4["name"])
for xy, lab in map_labels:
base.annotate(
text=lab, xy=xy, textcoords="data", size=16,
weight="semibold", rotation=32.5
)
ccb.plot(ax=base, cmap="tab20b", column="cc_name", legend=True)
ccb.boundary.plot(ax=base, color=BASE3, linewidth=.4)
w_isles.boundary.plot(ax=base, color="darkslategrey", linewidth=.5)
study_area.boundary.plot(ax=base, color="crimson")
base.set_aspect("equal", "box")
plt.title("Community councils near study area")
plt.xlabel("Easting (m)")
plt.ylabel("Northing (m)")
plt.text(
9.1e4, 9.09e5,
"Contains OS, Improvement Service & Marine Scotland data " +
"© Crown copyright and database right 2021",
backgroundcolor=BASE2
)
plt.xlim(9e4, 1.8e5)
plt.ylim(9.09e5, 9.825e5)
plt.show()