Last active
January 8, 2021 20:04
-
-
Save GeoWill/a51b685111a8891642318ea7fb6cc894 to your computer and use it in GitHub Desktop.
Overly complicated way of "pick a postcode that is a) inside the (multi-)polygon and b) far away from edges"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with | |
-- n as (select 'Highland' as name), | |
-- n as (select 'Na h-Eileanan an Iar' as name), | |
n as (select 'City of London' as name), | |
-- n as (select 'Bristol City Council' as name), | |
buffered as ( | |
select | |
n.name, oo.common_name, f.factor, st_buffer(st_simplify(largest_poly.geom, 0.01), -f.factor) as geom, largest_poly.geom as orig_geom | |
from | |
n, organisations_organisation oo | |
join organisations_organisationgeography og | |
on oo.id = og.organisation_id, | |
-- This next bit gets the largest part of a multipolygon, to make the buffer factor more sane. See Na h-Eileanan an Iar | |
lateral ( | |
select | |
(st_dump(og.geography)).geom as geom | |
order by | |
st_area((st_dump(og.geography)).geom) desc | |
limit 1 ) as largest_poly, | |
-- This bit takes a scale of the average of the bounding box edge lengths as a starting point to makeup a factor to buffer by | |
lateral ( | |
select | |
0.02*(st_xmax(largest_poly.geom)-st_xmin(largest_poly.geom) + st_ymax(largest_poly.geom)-st_ymin(largest_poly.geom)) as factor | |
where | |
oo.common_name = n.name) as f | |
where | |
oo.common_name = n.name | |
) | |
select | |
b.common_name, | |
b.factor, | |
uguo.pcds, | |
uguo."location", | |
b.geom | |
-- b.orig_geom | |
from | |
buffered b | |
join uk_geo_utils_onspd uguo on | |
st_within(uguo."location", b.geom) | |
limit 1 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment