Skip to content

Instantly share code, notes, and snippets.

@hunterowens
Created October 17, 2018 04:24
Show Gist options
  • Save hunterowens/3851a97f84ce292874f18f0247ee8c61 to your computer and use it in GitHub Desktop.
Save hunterowens/3851a97f84ce292874f18f0247ee8c61 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 99,
"metadata": {},
"outputs": [],
"source": [
"import shapely \n",
"from shapely.geometry import box\n",
"import geopandas as gpd\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"!mv /Users/hunterowens/Downloads/DRP_COUNTY_BOUNDARY.zip data/"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Archive: data/DRP_COUNTY_BOUNDARY.zip\r\n",
" inflating: DRP_COUNTY_BOUNDARY.prj \r\n",
" inflating: DRP_COUNTY_BOUNDARY.sbn \r\n",
" inflating: DRP_COUNTY_BOUNDARY.sbx \r\n",
" inflating: DRP_COUNTY_BOUNDARY.shp \r\n",
" inflating: DRP_COUNTY_BOUNDARY.shp.xml \r\n",
" inflating: DRP_COUNTY_BOUNDARY.shx \r\n",
" inflating: DRP_COUNTY_BOUNDARY.dbf \r\n"
]
}
],
"source": [
"!unzip data/DRP_COUNTY_BOUNDARY.zip"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"county = gpd.read_file('./data/DRP_COUNTY_BOUNDARY')"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"county = county.to_crs({'init': 'epsg:4326'}) # convert CRS "
]
},
{
"cell_type": "code",
"execution_count": 107,
"metadata": {},
"outputs": [],
"source": [
"from shapely.geometry import box\n",
"\n",
"def subbox(geometry, splits):\n",
" \"\"\"\n",
" subboxes a bounding box of a geometry \n",
" param\n",
" :geometry - Shapely Geometry\n",
" :splits - number of splits/division\n",
" \n",
" \"\"\"\n",
" bounds = geometry.bounds\n",
" # print(bounds)\n",
" xmin = float(bounds[0])\n",
" xmax = float(bounds[2])\n",
" ymin = float(bounds[1])\n",
" ymax = float(bounds[3])\n",
" x_step = (xmax - xmin) / splits\n",
" y_step = (ymax - ymin) / splits\n",
" boxes = []\n",
" for i_x in range(0, splits):\n",
" next_ix = i_x + 1\n",
" for i_y in range(0, splits):\n",
" next_iy = i_y + 1\n",
" b = box(xmin + i_x * x_step, ymin + i_y * y_step,\n",
" xmin + next_ix * x_step, ymin + next_iy * y_step)\n",
" boxes.append(b)\n",
" return(boxes)"
]
},
{
"cell_type": "code",
"execution_count": 109,
"metadata": {},
"outputs": [],
"source": [
"boxes = subbox(county[:1].geometry[0], 100)"
]
},
{
"cell_type": "code",
"execution_count": 122,
"metadata": {},
"outputs": [],
"source": [
"gpd.GeoDataFrame(geometry=boxes).to_file('./boxes.geojson', driver='GeoJSON')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment