Skip to content

Commit

Permalink
Merge pull request #53 from the-scouts/crs-changes
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner authored May 13, 2020
2 parents bdabcca + b5beca5 commit 4c6484d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
11 changes: 7 additions & 4 deletions src/geographies/district_boundaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
from src.base import Base
from src.data.scout_census import ScoutCensus

WGS_84 = 4326
BNG = 27700


# noinspection PyUnresolvedReferences
# To solve warnings for shapely methods
Expand Down Expand Up @@ -44,11 +47,11 @@ def create_district_boundaries(self):

# Uses the lat and long co-ordinates from above to create a GeoDataFrame
all_points = gpd.GeoDataFrame(all_locations, geometry=gpd.points_from_xy(all_locations.long, all_locations.lat))
all_points.crs = {"init": "epsg:4326"}
all_points.crs = f"epsg:{WGS_84}"

# Converts the co-ordinate reference system into OS36 which uses
# (x-y) coordinates in metres, rather than (long, lat) coordinates.
all_points = all_points.to_crs({"init": "epsg:27700"})
all_points = all_points.to_crs(f"epsg:{BNG}")
all_points.reset_index(inplace=True)

self.logger.info(f"Found {len(all_points.index)} different Section points")
Expand Down Expand Up @@ -102,10 +105,10 @@ def create_district_boundaries(self):
data_df = gpd.GeoDataFrame(data, columns=output_columns, geometry=[district_polygon])
output_gpd = gpd.GeoDataFrame(pd.concat([output_gpd, data_df], axis=0, sort=False))

output_gpd.crs = {"init": "epsg:27700"}
output_gpd.crs = f"epsg:{BNG}"

# Convert co-ordinates back to WGS84, which uses latitude and longitude
output_gpd = output_gpd.to_crs({"init": "epsg:4326"})
output_gpd = output_gpd.to_crs(f"epsg:{WGS_84}")
output_gpd.reset_index(drop=True, inplace=True)

self.logger.debug(f"output gpd\n{output_gpd}")
Expand Down
7 changes: 5 additions & 2 deletions src/geographies/geography.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
from src.data.ons_pd import ONSPostcodeDirectory
from src.data.scout_data import ScoutData

WGS_84 = 4326
BNG = 27700


# noinspection PyUnresolvedReferences
class Geography(Base):
Expand Down Expand Up @@ -177,8 +180,8 @@ def filter_boundaries_near_scout_area(self, scout_data: ScoutData, boundary: str

# Pivots the co-ordinate reference system into OS36 which uses
# (x-y) coordinates in metres, rather than (long, lat) coordinates.
data_with_points.crs = {"init": "epsg:4326"}
data_with_points = data_with_points.to_crs({"init": "epsg:27700"})
data_with_points.crs = f"epsg:{WGS_84}"
data_with_points = data_with_points.to_crs(f"epsg:{BNG}")
# TODO work out way to avoid co-ordinate pivot (i.e. convert 3km into GPS co-ordinates)

self.logger.info(f"Filters for records that satisfy {field} in {value_list}")
Expand Down
10 changes: 6 additions & 4 deletions src/maps/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

from typing import Dict, Union

WGS_84 = 4326


class Map(Base):
def __init__(self, scout_data_object: ScoutData, map_name: str):
Expand Down Expand Up @@ -275,15 +277,15 @@ def add_custom_data(self, csv_file_path: Path, layer_name: str, location_cols, m
# Merge with ONS Postcode Directory to obtain dataframe with lat/long
ons_pd_data = self.scout_data.ons_pd.data
custom_data = pd.merge(custom_data, ons_pd_data, how="left", left_on=location_cols, right_index=True, sort=False)
location_cols = {"crs": 4326, "x": "long", "y": "lat"}
location_cols = {"crs": WGS_84, "x": "long", "y": "lat"}

# Create geo data frame with points generated from lat/long or OS
custom_data = gpd.GeoDataFrame(custom_data, geometry=gpd.points_from_xy(x=custom_data[location_cols["x"]], y=custom_data[location_cols["y"]]),)

# Convert the 'Co-ordinate reference system' (crs) to WGS_84 (i.e. lat/long) if not already
if location_cols["crs"] != 4326:
custom_data.crs = {"init": f"epsg:{location_cols['crs']}"}
custom_data = custom_data.to_crs({"init": "epsg:4326"})
if location_cols["crs"] != WGS_84:
custom_data.crs = f"epsg:{location_cols['crs']}"
custom_data = custom_data.to_crs(f"epsg:{WGS_84}")

self._map_plotter.add_layer(layer_name, markers_clustered)

Expand Down
4 changes: 2 additions & 2 deletions src/maps/map_plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

# WGS_84 (World Geodetic System 1984) is a system for global positioning used in GPS.
# It is used by folium to plot the data.
WGS_84 = "4326"
WGS_84 = 4326


class MapPlotter(Base):
Expand Down Expand Up @@ -114,7 +114,7 @@ def _filter_shape_file(self, shape_file_path: Path):
self.logger.info(f"Resulting in {len(all_shapes.index)} shapes")

# Covert shape file to world co-ordinates
self.geo_data = all_shapes.to_crs({"init": f"epsg:{WGS_84}"})
self.geo_data = all_shapes.to_crs(f"epsg:{WGS_84}")
# self.logger.debug(f"geo_data\n{self.geo_data}")

def add_areas(self, name: str, show: bool, boundary_name: str, colourmap: colormap.ColorMap):
Expand Down

0 comments on commit 4c6484d

Please sign in to comment.