-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGIS_With_Python.qmd
More file actions
132 lines (96 loc) · 3.23 KB
/
Copy pathGIS_With_Python.qmd
File metadata and controls
132 lines (96 loc) · 3.23 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
---
title: "Doing GIS with Python"
format:
html:
code-fold: false
jupyter: python3
keep-ipynb: true
---
## Start a Python Project
Import the packages needed
```{python}
# Packages for handling files
import os
import zipfile
import shutil
# Packages for reading and plotting data
import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt
```
## Import the point data from github
```{python}
PID_Metadata = pd.read_csv('https://raw.githubusercontent.com/Police-Involved-Deaths-CA/Data/main/MostRecentUpdate/PID_locations_Metadata.csv')
print('Metadata')
print(PID_Metadata)
PID_locations = pd.read_csv('https://raw.githubusercontent.com/Police-Involved-Deaths-CA/Data/main/MostRecentUpdate/PID_locations.csv')
print('\nData Preview')
print(PID_locations.head())
PID_locations.to_csv('Data/temp_files/PID_locations.csv')
```
## Import the Census Data
Extract a .zipfile of census data downloaded from simply analytics
* Read the metadata
```{python}
BC_Census_Data = 'SimplyAnalytics_Shapefiles_2023-02-02_23_07_59_7fa10dab487cee919a8d7e30ddf85ff3'
with zipfile.ZipFile('Data/'+BC_Census_Data+'.zip','r') as BCD:
print('.zipfile contains:')
print (BCD.namelist())
BCD.extractall('Data/temp_files/')
print('\n Varible Names:')
with open('Data/temp_files/variable_names.txt') as var_name:
print(var_name.read())
```
## Read the Shapefile
* Rename the columns
* Change the projection
* Calculate the new columns
* Plot a map
```{python}
# Read Data
BC_subDivs = gpd.read_file('Data/temp_files/'+BC_Census_Data+'.shp')
# Rename Columns
BC_subDivs = BC_subDivs.rename(columns = {
'VALUE0':'Pop_Indigenous',
'VALUE1':'Pop_Total',
'VALUE2':'Pop_Visible_Minority'
})
# Project to BC Albers
BC_subDivs = BC_subDivs.to_crs(3005)
# Calculate White and Non-White Population Totals
BC_subDivs['Pop_NonWhite'] = BC_subDivs[['Pop_Indigenous','Pop_Visible_Minority']].sum(axis=1)
BC_subDivs['Pop_White'] = BC_subDivs['Pop_Total'] - BC_subDivs['Pop_NonWhite']
# Make a Map
fig,ax=plt.subplots(figsize=(6,6))
BC_subDivs.plot(column='Pop_Total',ax=ax,edgecolor='k',legend=True)
ax.set_title('Population by BC Sub-Divison 2022')
```
## Saving Data
Lets save the data and also create a .zip file so the data can be downloaded more easily.
```{python}
# Shapefile is a more complex format often used in desktop GIS. We're saving to a temp folder because we will then zip the data for easier download
BC_subDivs.to_file('Data/temp_files/BC_subDivs_2022.shp')
# Save the .shp to a .zip for easier download
filepath = 'Data/Workshop_Data.zip'
# Delete old .zip file *if it exists*
try:
os.unlink(filepath)
except:
pass
with zipfile.ZipFile(filepath, 'a') as zipf:
source_path = 'Data/temp_files/'
source_name = 'BC_subDivs_2022'
destination_name = 'BC_subDivs_2022'
# Add the BC_subDivs_2022.shp
for tag in ['.shp','.dbf','.prj','.shx']:
zipf.write(source_path+source_name+tag, destination_name+tag)
# Add the deaths data as well
source_path = 'Data/PID_locations.csv'
destination = 'PID_locations.csv'
zipf.write(source_path, destination)
# Delete data in temp_files
for root, dirs, files in os.walk('Data/temp_files'):
for f in files:
if f != '.gitignore':
os.unlink(os.path.join(root, f))
```