Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vincentsarago/6521bbaf07d2c5128c98e1fd7b1d4275 to your computer and use it in GitHub Desktop.
Save vincentsarago/6521bbaf07d2c5128c98e1fd7b1d4275 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [],
"source": [
"cogeo = '/Users/vincentsarago/627750_6437750_web2.tif'\n",
"\n",
"import rasterio\n",
"from shapely.geometry import shape\n",
"\n",
"import json\n",
"import click\n",
"\n",
"import rasterio\n",
"from rasterio.rio import options\n",
"from rasterio.warp import transform_bounds\n",
"from rasterio.windows import Window\n",
"\n",
"from supermercado.burntiles import tile_extrema\n",
"from mercantile import feature\n",
"\n",
"from rio_cogeo import utils"
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'driver': 'GTiff', 'dtype': 'uint8', 'nodata': None, 'width': 7424, 'height': 7424, 'count': 3, 'crs': CRS({'init': 'epsg:3857'}), 'transform': Affine(0.2985821417389694, 0.0, 231757.06976065438,\n",
" 0.0, -0.2985821417384833, 5627141.148298103), 'blockxsize': 512, 'blockysize': 512, 'tiled': True, 'compress': 'jpeg', 'interleave': 'pixel', 'photometric': 'ycbcr'}\n"
]
}
],
"source": [
"w = (0, 512, 0, 512)\n",
"\n",
"with rasterio.open(cogeo) as src:\n",
" print(src.profile)\n",
" coff, wd, roff, ht = w\n",
" wind = Window(coff, roff, wd, ht)\n",
" bounds = src.window_bounds(wind)\n",
" wgs84_bounds = list(\n",
" transform_bounds(\n",
" *[src.crs, \"epsg:4326\"] + list(bounds), densify_pts=21\n",
" )\n",
" )\n",
"\n",
" w, s, e, n = (round(v, 15) for v in wgs84_bounds)\n",
"\n",
" geom = {\n",
" 'type': 'Polygon',\n",
" 'coordinates': [[\n",
" [w, s],\n",
" [w, n],\n",
" [e, n],\n",
" [e, s],\n",
" [w, s]]]}\n",
" feat = {\n",
" 'type': 'Feature',\n",
" 'geometry': geom,\n",
" 'properties': {}}"
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'type': 'Polygon', 'coordinates': [[[2.0819091796875, 45.03471477868859], [2.0819091796875, 45.035687477951846], [2.083282470703124, 45.035687477951846], [2.083282470703124, 45.03471477868859], [2.0819091796875, 45.03471477868859]]]}\n"
]
}
],
"source": [
"print(feat['geometry'])"
]
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {},
"outputs": [],
"source": [
"extrema = tile_extrema(wgs84_bounds, 18)"
]
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {},
"outputs": [],
"source": [
"up_left_tile = [extrema['x']['min'], extrema['y']['min'], 18]"
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'type': 'Polygon', 'coordinates': [[[2.0819091796875, 45.03568524531632], [2.0819091796875, 45.03665569548622], [2.083282470703125, 45.03665569548622], [2.083282470703125, 45.03568524531632], [2.0819091796875, 45.03568524531632]]]}\n"
]
}
],
"source": [
"merc_feat = feature(up_left_tile, precision=15)\n",
"print(merc_feat['geometry'])"
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {},
"outputs": [],
"source": [
"sm = shape(merc_feat['geometry'])\n",
"s = shape(feat['geometry'])"
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 92,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s.almost_equals(sm)"
]
},
{
"cell_type": "code",
"execution_count": 93,
"metadata": {},
"outputs": [],
"source": [
"diff = s.difference(sm)"
]
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.3327331008306367e-06"
]
},
"execution_count": 94,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"diff.area"
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"internal tile: (2.0819091796875, 45.03471477868859, 2.083282470703124, 45.035687477951846)\n",
"mercator tile: (2.0819091796875, 45.03568524531632, 2.083282470703125, 45.03665569548622)\n"
]
}
],
"source": [
"print('internal tile: ' + str(s.bounds))\n",
"print('mercator tile: ' + str(sm.bounds))"
]
},
{
"cell_type": "code",
"execution_count": 96,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.0\n"
]
}
],
"source": [
"print(s.bounds[0] - sm.bounds[0])"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-0.0009704666277343676\n"
]
}
],
"source": [
"print(s.bounds[1] - sm.bounds[1])"
]
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-8.881784197001252e-16\n"
]
}
],
"source": [
"print(s.bounds[2] - sm.bounds[2])"
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-0.0009682175343712629\n"
]
}
],
"source": [
"print(s.bounds[3] - sm.bounds[3])"
]
},
{
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment