-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_block_corr_from_parcels_table
More file actions
186 lines (163 loc) · 9.28 KB
/
create_block_corr_from_parcels_table
File metadata and controls
186 lines (163 loc) · 9.28 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
## Script to create census_block correspondence files from the parcels table
## By city_id and by location in or out of a TOD area.
## Using Gross SQFT as the weight, all or nothing based on where the biggest part of the block lies.
## Note had to use permanent rather than temporary tables, then delete at the end, because of a complicated ordinal ranking query plundered from stack overflow
## MHS 6-19-20
-- ====================================
-- ====================================
-- Start of the Block to City Corr work
-- ====================================
-- ====================================
# Create a copy of the final buildings table so I don't mess up the original one
create table v2050_parcel_capacity.copy_parcels_king_paa_final_alt
select * from 2014_parcel_baseyear_core.parcels_king_paa;
# Create a reference table, all blocks with their total gross sqft area. Had to use BigInt for the variable due to Int limits
drop table if exists v2050_parcel_capacity.blocks_parcels_area;
create table v2050_parcel_capacity.blocks_parcels_area
SELECT
census_block_id
,sum(gross_sqft) as total_gross_area
,count(parcel_id) as parcels
FROM v2050_parcel_capacity.copy_parcels_king_paa_final_alt
group by census_block_id;
alter table v2050_parcel_capacity.blocks_parcels_area add primary key(census_block_id);
# Sum area by census_block and city_id
drop table if exists v2050_parcel_capacity.blocks_cities_correspondence_working;
create table v2050_parcel_capacity.blocks_cities_correspondence_working
select
census_block_id
,city_id
,sum(gross_sqft) as gross_area
from v2050_parcel_capacity.copy_parcels_king_paa_final_alt
group by census_block_id
,city_id;
alter table v2050_parcel_capacity.blocks_cities_correspondence_working add index(census_block_id), add index(city_id);
# Add columns for each block's total area and the percent of total that particular city_x_block portion represents
alter table v2050_parcel_capacity.blocks_cities_correspondence_working add column total_gross_area bigint(11), add column percent_of_total float;
update v2050_parcel_capacity.blocks_cities_correspondence_working set total_gross_area = 0, percent_of_total = 0;
# copy into the working table the block total area, and calculate % of total
update v2050_parcel_capacity.blocks_cities_correspondence_working a
inner join v2050_parcel_capacity.blocks_parcels_area b
on a.census_block_id = b.census_block_id
set a.total_gross_area = b.total_gross_area;
update v2050_parcel_capacity.blocks_cities_correspondence_working set percent_of_total = gross_area/total_gross_area;
# Plundered code that groups and ranks the block x city portions ordinally
## Adapted from https://stackoverflow.com/questions/532878/how-to-perform-grouped-ranking-in-mysql
drop table if exists v2050_parcel_capacity.blocks_cities_corr_ranks;
create temporary table v2050_parcel_capacity.blocks_cities_corr_ranks
SELECT g1.city_id
, g1.census_block_id
, g1.percent_of_total
, COUNT(*) AS rank
FROM v2050_parcel_capacity.blocks_cities_correspondence_working AS g1
JOIN v2050_parcel_capacity.blocks_cities_correspondence_working AS g2
ON (g2.percent_of_total, g2.city_id) >= (g1.percent_of_total, g1.city_id)
AND g1.census_block_id = g2.census_block_id
GROUP BY g1.city_id
, g1.census_block_id
, g1.percent_of_total
ORDER BY g1.census_block_id
, rank;
alter table v2050_parcel_capacity.blocks_cities_corr_ranks add index(census_block_id), add index(city_id);
# Transfer the ranks from the temp table to the primary working table
alter table v2050_parcel_capacity.blocks_cities_correspondence_working add column rank int(11);
update v2050_parcel_capacity.blocks_cities_correspondence_working set rank=0;
update v2050_parcel_capacity.blocks_cities_correspondence_working a
inner join v2050_parcel_capacity.blocks_cities_corr_ranks b
on a.census_block_id = b.census_block_id
and a.city_id = b.city_id
set a.rank = b.rank;
# Create the final correspondence table by only extracting records with Rank = 1
drop table if exists v2050_parcel_capacity.blocks_cities_correspondence;
create table v2050_parcel_capacity.blocks_cities_correspondence
select census_block_id, city_id
from v2050_parcel_capacity.blocks_cities_correspondence_working
where rank=1;
alter table v2050_parcel_capacity.blocks_cities_correspondence add index(census_block_id), add index(city_id);
# Drop the tables created along the way, no longer needed
drop table if exists v2050_parcel_capacity.blocks_cities_correspondence_working;
drop table if exists v2050_parcel_capacity.blocks_parcels_area;
## QC Check of the only census_block_to lie in 4 different cities.
select * from v2050_parcel_capacity.blocks_cities_corr_ranks where census_block_id = 18383;
-- ====================================
-- ====================================
-- End of the Block to City Corr work
-- ====================================
-- ====================================
-- ====================================
-- ====================================
-- Start of the Block to TOD Corr work
-- ====================================
-- ====================================
# Add a column assigning a boolean for any parcel where tod_id>0 (in an hct area)
alter table v2050_parcel_capacity.copy_parcels_king_paa_final_alt add column tod_in_out_bool int(11);
update v2050_parcel_capacity.copy_parcels_king_paa_final_alt set tod_in_out_bool = 0;
update v2050_parcel_capacity.copy_parcels_king_paa_final_alt set tod_in_out_bool = 1 where tod_id>0;
# Create a reference table, all blocks with their total gross sqft area. Had to use BigInt for the variable due to Int limits
drop table if exists v2050_parcel_capacity.blocks_parcels_area;
create table v2050_parcel_capacity.blocks_parcels_area
SELECT
census_block_id
,sum(gross_sqft) as total_gross_area
,count(parcel_id) as parcels
FROM v2050_parcel_capacity.copy_parcels_king_paa_final_alt
group by census_block_id;
alter table v2050_parcel_capacity.blocks_parcels_area add primary key(census_block_id);
# Sum area by census_block and the tod in or out variable
drop table if exists v2050_parcel_capacity.blocks_tod_correspondence_working;
create table v2050_parcel_capacity.blocks_tod_correspondence_working
select
census_block_id
,tod_in_out_bool
,sum(gross_sqft) as gross_area
from v2050_parcel_capacity.copy_parcels_king_paa_final_alt
group by census_block_id
,tod_in_out_bool;
# Add columns for each block's total area and the percent of total that particular tod_x_block portion represents
alter table v2050_parcel_capacity.blocks_tod_correspondence_working add index(census_block_id), add index(tod_in_out_bool);
alter table v2050_parcel_capacity.blocks_tod_correspondence_working add column total_gross_area bigint(11), add column percent_of_total float;
# copy into the working table the block total area, and calculate % of total
update v2050_parcel_capacity.blocks_tod_correspondence_working set total_gross_area = 0, percent_of_total = 0;
update v2050_parcel_capacity.blocks_tod_correspondence_working a
inner join v2050_parcel_capacity.blocks_parcels_area b
on a.census_block_id = b.census_block_id
set a.total_gross_area = b.total_gross_area;
update v2050_parcel_capacity.blocks_tod_correspondence_working set percent_of_total = gross_area/total_gross_area;
# Plundered code that groups and ranks the block x tod portions ordinally
## Adapted from https://stackoverflow.com/questions/532878/how-to-perform-grouped-ranking-in-mysql
drop table if exists v2050_parcel_capacity.blocks_tod_corr_ranks;
create temporary table v2050_parcel_capacity.blocks_tod_corr_ranks
SELECT g1.tod_in_out_bool
, g1.census_block_id
, g1.percent_of_total
, COUNT(*) AS rank
FROM v2050_parcel_capacity.blocks_tod_correspondence_working AS g1
JOIN v2050_parcel_capacity.blocks_tod_correspondence_working AS g2
ON (g2.percent_of_total, g2.tod_in_out_bool) >= (g1.percent_of_total, g1.tod_in_out_bool)
AND g1.census_block_id = g2.census_block_id
GROUP BY g1.tod_in_out_bool
, g1.census_block_id
, g1.percent_of_total
ORDER BY g1.census_block_id
, rank;
alter table v2050_parcel_capacity.blocks_tod_corr_ranks add index(census_block_id), add index(tod_in_out_bool);
# Transfer the ranks from the temp table to the primary working table
alter table v2050_parcel_capacity.blocks_tod_correspondence_working add column rank int(11);
update v2050_parcel_capacity.blocks_tod_correspondence_working set rank=0;
update v2050_parcel_capacity.blocks_tod_correspondence_working a
inner join v2050_parcel_capacity.blocks_tod_corr_ranks b
on a.census_block_id = b.census_block_id
and a.tod_in_out_bool = b.tod_in_out_bool
set a.rank = b.rank;
# Create the final correspondence table by only extracting records with Rank = 1
drop table if exists v2050_parcel_capacity.blocks_tod_correspondence;
create table v2050_parcel_capacity.blocks_tod_correspondence
select census_block_id, tod_in_out_bool
from v2050_parcel_capacity.blocks_tod_correspondence_working
where rank=1;
alter table v2050_parcel_capacity.blocks_tod_correspondence add index(census_block_id), add index(tod_in_out_bool);
# Drop the tables created along the way, no longer needed
drop table if exists v2050_parcel_capacity.blocks_tod_correspondence_working;
drop table if exists v2050_parcel_capacity.blocks_parcels_area;
## QC Check of a census_block with parcels both in and out of TOD areas.
select * from v2050_parcel_capacity.blocks_tod_corr_ranks where census_block_id = 11;