<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.3">Jekyll</generator><link href="http://sidetrackedmind.com/feed.xml" rel="self" type="application/atom+xml" /><link href="http://sidetrackedmind.com/" rel="alternate" type="text/html" /><updated>2023-11-16T15:08:53+00:00</updated><id>http://sidetrackedmind.com/feed.xml</id><title type="html">sidetrackedmind</title><subtitle>My site</subtitle><author><name>Ben Malnor</name></author><entry><title type="html">Exploring Tiling - Part 1 - Geohash</title><link href="http://sidetrackedmind.com/blog/exploring-geohash/" rel="alternate" type="text/html" title="Exploring Tiling - Part 1 - Geohash" /><published>2019-10-26T00:00:00+00:00</published><updated>2019-10-26T00:00:00+00:00</updated><id>http://sidetrackedmind.com/blog/exploring-geohash</id><content type="html" xml:base="http://sidetrackedmind.com/blog/exploring-geohash/">&lt;h2 id=&quot;introduction---why-tiling&quot;&gt;Introduction - Why Tiling?&lt;/h2&gt;
&lt;p&gt;Let’s say we have data spread throughout Seattle (e.g. housing sales, school grades or ratings, number of restaurants, etc). We want to aggregate that data into easier to understand pieces. Let’s say you started with a polygon outlining the City of Seattle (KC data source). Below are two version of the Seattle outline. One is a screenshot of the raw file in QGIS, the other is a geopandas plot using the following:&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;city_df&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gpd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/python_r_comp/admin_SHP/admin/city.shp&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;fig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subplots&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;figsize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#there are multiple cities King County `city.shp` dataset
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;city_of_interest&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'Seattle'&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;single_city&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;city_df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;city_df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'CITYNAME'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;city_of_interest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;copy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#change coordinate reference systems to be compatible with other datasets
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;single_city&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_crs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'init'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'epsg:4326'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inplace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;single_city&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'none'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
                   &lt;span class=&quot;n&quot;&gt;edgecolor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'black'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
                   &lt;span class=&quot;n&quot;&gt;legend&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                  &lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;single_city&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;annotate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'CITYNAME'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; 
                                           &lt;span class=&quot;n&quot;&gt;xy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;geometry&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;centroid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
                                               &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;geometry&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;centroid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ha&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'center'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;img src=&quot;/assets/images/seattle_boundary_w_basemap.PNG&quot; alt=&quot;w_basemap&quot; /&gt; &lt;img src=&quot;/assets/images/seattle_boundary_no_basemap.PNG&quot; alt=&quot;wo_basemap&quot; /&gt;
&lt;br /&gt;
&lt;em&gt;Above (left) Seattle boundary with a basemap and (right) Seattle boundary without a basemap from geopandas&lt;/em&gt;
&lt;br /&gt;
&lt;br /&gt;
A good starting point for aggregating data across Seattle would be to separate the Seattle polygon into into neighborhoods based on the King County neighborhood dataset (dataset source). The neighborhood dataset looks like this:&lt;br /&gt;
&lt;img src=&quot;/assets/images/seattle_neighborhoods.PNG&quot; alt=&quot;seattle_neighborhoods&quot; /&gt;
&lt;br /&gt;
You could easily aggregate data by neighborhood and the neighborhood names would be recognizable by the end user of your analysis. But, a major drawback is the inconsistent nature of political or social boundaries like neighborhood. Neighorhood boundaries provided by King County can change and so can the names. Some sub-neighborhood groups can develop or recede.&lt;br /&gt; A more consistent political/social boundary is the Census Bureau Blocks or Block Groups (gov website link). US Census Bureau tries very hard to maintain consistency with Blocks and Block Groups so year-over-year analysis should be straightforward &lt;em&gt;and&lt;/em&gt; you can incorporate Census results into your data analysis. This is a pretty promising approach. The drawbacks with using Blocks or Block Groups is that they were created to a) split up inhabitable land (i.e. there are no Groups on lakes or rivers), and b) they have been optimized for population surveying not necessarily for the type of analysis you’re performing. The last drawback to using any social or political boundary is that you don’t have a consistent spatial or geometrical relationship between neighbors.&lt;br /&gt; In order to get consistent neighbor relationships, we’ll have to set our sights on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tiling&lt;/code&gt; methods and tile geometries.&lt;br /&gt;&lt;/p&gt;
&lt;h2 id=&quot;tiling&quot;&gt;Tiling&lt;/h2&gt;
&lt;p&gt;In general, there are three geometries that easily tile: rectangle, triangle, and hexagon.&lt;br /&gt;
&lt;img src=&quot;/assets/images/tiling_uber.PNG&quot; alt=&quot;tiling&quot; /&gt;
&lt;em&gt;Grid layouts from uber’s &lt;a href=&quot;https://uber.github.io/h3/#/documentation/overview/use-cases&quot;&gt;H3- hexagon tiling documentation&lt;/a&gt;&lt;/em&gt;&lt;br /&gt;
&lt;br /&gt;
You can see from the Uber graphic above, there are some unique benefits to hexagons that I’ll cover in another post - for now, we’re focusing on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rectangles&lt;/code&gt;. One way to create a consistent rectangle grid is to use a &lt;a href=&quot;https://en.wikipedia.org/wiki/Geohash&quot;&gt;geohash grid&lt;/a&gt;. A helpful website for understanding the levels of geohashing is this &lt;a href=&quot;https://www.movable-type.co.uk/scripts/geohash.html&quot;&gt;interactive-geohash&lt;/a&gt;.
&lt;br /&gt;
Here’s a static image from that site of the earth broken down into geohashes: &lt;br /&gt;
&lt;img src=&quot;/assets/images/geohash.jpg&quot; width=&quot;350&quot; /&gt;&lt;br /&gt;
As you can see from the image above, there are various scales of geohash rectangles, created in order to cover the whole globe. Each rectangle grid can be broken into smaller rectangles. From the image, you can see the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;g&lt;/code&gt; rectangle is broken up into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gh&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gk&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gs&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gu&lt;/code&gt;, etc. There’s a ton of information on the &lt;a href=&quot;https://en.wikipedia.org/wiki/Geohash&quot;&gt;wikipedia page&lt;/a&gt; about the specifics of making the grid and the geohash characters but I want to focus on two benefits for breaking up large datasets:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;consistency&lt;/li&gt;
&lt;li&gt;not spatial&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;consistency&quot;&gt;Consistency&lt;/h2&gt;
&lt;p&gt;Creating a grid from a geohash library like Python’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geolib&lt;/code&gt; will always return the same geohash and boundaries given the same 3 inputs:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;latitude&lt;/li&gt;
&lt;li&gt;longitude&lt;/li&gt;
&lt;li&gt;precision (or scale)&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;geolib&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;geohash&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;geohash&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;geohash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;latitude&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;longitude&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;precision&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Every geohash has a pre-defined bounding box. For instance:&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;latitude&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;47.64495849609375&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;longitude&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;122.3712158203125&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;precision&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;test_geohash&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;geohash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;latitude&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;longitude&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;precision&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#geohash = 'c22zp3'
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bounds&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;geohash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bounds&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;upperleft_geohash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#bounds = Bounds(sw=SouthWest(lat=47.6422119140625,
#                           lon=-122.376708984375), 
#                ne=NorthEast(lat=47.647705078125, 
#                            lon=-122.36572265625))
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Once you pick a scale the geohash rectangle grid will be &lt;em&gt;consistent&lt;/em&gt; no matter what data you’re using as the input. Here’s a close up of the geohash grid in Seattle with a precision of 6. The rectangle &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;c22zp3&lt;/code&gt; is highlighted in red.
&lt;br /&gt;
&lt;img src=&quot;/assets/images/seattle_geohash_zoom_in_v2.PNG&quot; alt=&quot;seattle_geohash_zoom_in&quot; /&gt;
&lt;br /&gt;
Any latitude and longitude that falls into the geohash rectangles bounding box will &lt;em&gt;always&lt;/em&gt; be encoded with the geohash &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;c22zp3&lt;/code&gt; at precision=6.
&lt;br /&gt;
Here’s a picture of the geohash grid on top of the Seattle boundary polygon (below).&lt;br /&gt;
&lt;img src=&quot;/assets/images/seattle_full_geohash_grid.png&quot; alt=&quot;seattle_full_geohash_grid&quot; /&gt;
&lt;br /&gt;
To illustrate the importance of the &lt;em&gt;consistency&lt;/em&gt; with a geohash grid. I’ll show you another way to create a grid. Instead of using geohash rectangles, you can break the Seattle polygon into pieces using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;open street map&lt;/code&gt; Python library’s function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;quadrant_cut_geometry&lt;/code&gt; (h.t. blob reference for showing me this option). The below code snippet breaks the Seattle polygon into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;quadrat_width=0.01&lt;/code&gt; which is ~1000 m in ESPG:4326 (aka WGS84, which has units in degrees).
&lt;br /&gt;&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# make the geometry a multipolygon if it's not already
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;geometry&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;single_city&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'geometry'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iloc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;geometry&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Polygon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;geometry&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MultiPolygon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;geometry&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;geometry_cut&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ox&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;quadrat_cut_geometry&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;geometry&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
                                        &lt;span class=&quot;n&quot;&gt;quadrat_width&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.01&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;seattle_cut&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gpd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GeoDataFrame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;crs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'init'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'4326'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
                 &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;geometry&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;geom&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;geom&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;geometry_cut&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;geoms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;The image below is created by just plotting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;seattle_cut&lt;/code&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;seattle_cut.plot()&lt;/code&gt;
&lt;br /&gt;
&lt;img src=&quot;/assets/images/polygon_cut_grid.png&quot; width=&quot;350&quot; /&gt;&lt;br /&gt;
&lt;br /&gt;
You can see that the grid seems similar to geohashing rectangles and it has the benefit of being very customized for your polygon or area of interest. The customization comes at the cost of consistency and it can have some buggy effects near the corners (as you can see in the picture above). Similar to neighborhoods, let’s say King County decides to modify the shape of the Seattle city boundary polygon. If that happens, the resultant polygon cut grid will be different. Or if you decide to stick with the original grid, to compare a previous year’s analysis, you may mis-represent areas when you aggregate or group results.&lt;br /&gt;&lt;/p&gt;

&lt;h2 id=&quot;not-spatial&quot;&gt;Not Spatial&lt;/h2&gt;
&lt;p&gt;One subtle benefit of using a geohash libary is that you can perform &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;point-in-polygon&lt;/code&gt;-like operations without converting your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;latitude&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;longitude&lt;/code&gt; values into discrete &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Point&lt;/code&gt; geometries. Let’s say you had two tables: housing_sales, and schools.&lt;br /&gt;&lt;/p&gt;
&lt;h3 id=&quot;housing_sales&quot;&gt;housing_sales&lt;/h3&gt;
&lt;table style=&quot;width:50%&quot;&gt;
  &lt;tr&gt;
    &lt;th&gt;property_id&lt;/th&gt;
    &lt;th&gt;address&lt;/th&gt;
    &lt;th&gt;latitude&lt;/th&gt;
    &lt;th&gt;longitude&lt;/th&gt;
    &lt;th&gt;sale_price&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;544381&lt;/td&gt;
    &lt;td&gt;123 fun lane&lt;/td&gt;
    &lt;td&gt;47.642211&lt;/td&gt;
    &lt;td&gt;-122.376708&lt;/td&gt;
    &lt;td&gt;1200000&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;

&lt;h3 id=&quot;schools&quot;&gt;schools&lt;/h3&gt;
&lt;table style=&quot;width:50%&quot;&gt;
  &lt;tr&gt;
    &lt;th&gt;school_id&lt;/th&gt;
    &lt;th&gt;address&lt;/th&gt;
    &lt;th&gt;latitude&lt;/th&gt;
    &lt;th&gt;longitude&lt;/th&gt;
    &lt;th&gt;school_grade&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;54&lt;/td&gt;
    &lt;td&gt;55 learning way&lt;/td&gt;
    &lt;td&gt;47.645121&lt;/td&gt;
    &lt;td&gt;-122.376223&lt;/td&gt;
    &lt;td&gt;87&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;

&lt;p&gt;You can find the geohash of each record, given the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;latitude&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;longitude&lt;/code&gt; columns.&lt;/p&gt;

&lt;h3 id=&quot;housing_sales_w_geohash&quot;&gt;housing_sales_w_geohash&lt;/h3&gt;
&lt;table style=&quot;width:60%&quot;&gt;
  &lt;tr&gt;
    &lt;th&gt;property_id&lt;/th&gt;
    &lt;th&gt;address&lt;/th&gt;
    &lt;th&gt;latitude&lt;/th&gt;
    &lt;th&gt;longitude&lt;/th&gt;
    &lt;th&gt;sale_price&lt;/th&gt;
    &lt;th&gt;geohash_id&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;544381&lt;/td&gt;
    &lt;td&gt;123 fun lane&lt;/td&gt;
    &lt;td&gt;47.642211&lt;/td&gt;
    &lt;td&gt;-122.376708&lt;/td&gt;
    &lt;td&gt;1200000&lt;/td&gt;
    &lt;td&gt;c22zp3&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;

&lt;h3 id=&quot;schools_w_geohash&quot;&gt;schools_w_geohash&lt;/h3&gt;
&lt;table style=&quot;width:60%&quot;&gt;
  &lt;tr&gt;
    &lt;th&gt;school_id&lt;/th&gt;
    &lt;th&gt;address&lt;/th&gt;
    &lt;th&gt;latitude&lt;/th&gt;
    &lt;th&gt;longitude&lt;/th&gt;
    &lt;th&gt;school_grade&lt;/th&gt;
    &lt;th&gt;geohash_id&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;54&lt;/td&gt;
    &lt;td&gt;55 learning way&lt;/td&gt;
    &lt;td&gt;47.645121&lt;/td&gt;
    &lt;td&gt;-122.376223&lt;/td&gt;
    &lt;td&gt;87&lt;/td&gt;
    &lt;td&gt;c22zp3&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;

&lt;p&gt;The above is a fictious example but you get the idea. Once you have a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geohash_id&lt;/code&gt; column in each table, you can quickly summarize housing sales within each schools &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geohash_grid&lt;/code&gt;, or find the school(s) in a particular property’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geohash_grid&lt;/code&gt;. The method comes with limitations. For instance, a property could be on the edge of one geohash_grid and a school could be on the other side of that edge in another grid. We would miss that school even though, spatially, it’s very close. That issue, leads us naturally to the need to create &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;relationships&lt;/code&gt; on the grid in order to calculate a grid’s neighbor. We’ll explore &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;neighbor&lt;/code&gt; analysis in another post.&lt;/p&gt;

&lt;p&gt;**Note to self when to discuss the fact that these polygon cuts do not totally “touch” and therefore we need to do XX to them before we can use neighbor analysis like Queen/Rook.&lt;/p&gt;</content><author><name>Ben Malnor</name></author><category term="blog" /><category term="spatial" /><summary type="html">Introduction - Why Tiling? Let’s say we have data spread throughout Seattle (e.g. housing sales, school grades or ratings, number of restaurants, etc). We want to aggregate that data into easier to understand pieces. Let’s say you started with a polygon outlining the City of Seattle (KC data source). Below are two version of the Seattle outline. One is a screenshot of the raw file in QGIS, the other is a geopandas plot using the following: ```python city_df = gpd.read_file(“/python_r_comp/admin_SHP/admin/city.shp”) fig, ax = plt.subplots(figsize=(5,12)) #there are multiple cities King County city.shp dataset city_of_interest = ‘Seattle’ single_city = city_df[city_df[‘CITYNAME’]==city_of_interest].copy() #change coordinate reference systems to be compatible with other datasets single_city.to_crs({‘init’: ‘epsg:4326’}, inplace=True) single_city.plot(color=’none’, edgecolor=’black’, legend=True, ax=ax)</summary></entry></feed>