Skip to content

Instantly share code, notes, and snippets.

View jamesdamillington's full-sized avatar

James Millington jamesdamillington

View GitHub Profile
@jamesdamillington
jamesdamillington / geopandas-mapclassify.py
Last active September 21, 2023 16:47
Testing mapclassify with geopandas
import geopandas as gpd
import pandas as pd
england = pd.read_csv("https://raw.githubusercontent.com/jamesdamillington/spar/main/data/brexit/brexit_vote.csv",
index_col='Area_Code')
lads = gpd.read_file("https://raw.githubusercontent.com/jamesdamillington/spar/main/data/brexit/local_authority_districts.geojson").set_index('lad16cd')
Egdf = lads.merge(england, how='inner',
left_index=True, right_index=True)
@jamesdamillington
jamesdamillington / csv_to_shp_points.py
Created April 26, 2023 16:29
Convert a list of coordinates (e.g. lat, long) with an id in a csv to a shapefile and GeoPandas gdf
#import rquired packages
import pandas as pd
import geopandas as gpd
from shapely.geometry import Point
#read data and check column names (for coords)
df = pd.read_csv("data/StationLocationIGS.csv")
df.columns
#create geometry column from lat and long using shapely
@jamesdamillington
jamesdamillington / round_gpd_geoms.py
Last active August 22, 2022 16:44
reduce the precision of geometries in a Pandas GeoDataFrame
#when saving GeoDataFrames as csv, high precision geometries can produce large files on disk
#reducing unnecessary precision can help reduce file size
#e.g. ONS digital vector boundary data converted from shp to gpd results in nano-metre precision!
import geopandas as gpd
from shapely.wkt import loads
from shapely.wkt import dumps
#set desired number of decimal places (e.g. see https://xkcd.com/2170/)
precision = 0
@jamesdamillington
jamesdamillington / csv_to_gpd.py
Created August 22, 2022 15:43
read a csv (containing a column with geometries) as geopandas gdf
#import relevant packages
import pandas as pd
import geopandas as gpd
from shapely.wkt import loads
#set variables for this particular data set
file_path = "data/mydata.csv" #location of data
geom_col = 'geometry' #name of column containing geometries
epsg_id = 27700 #see https://epsg.io/
crs_str = "EPSG:" + str(epsg_id) #concat
@jamesdamillington
jamesdamillington / gitsio.md
Last active August 16, 2022 16:29
What can we do with a gist?

What can we do with a Gist?

Markdown

Here's a list

  • of
  • pointless
  • stuff

Now go home