Last active
April 12, 2021 07:54
-
-
Save geoHeil/b5b74887e20e4b659d4bb693a700a402 to your computer and use it in GitHub Desktop.
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
%pylab inline | |
import pandas as pd | |
import geopandas as gp | |
import seaborn as sns; sns.set() | |
import numpy as np | |
from h3 import h3 | |
from shapely.ops import unary_union | |
# input is 4326 | |
df = gp.read_file('/Users/geoheil/Downloads/gadm36_AUT_gpkg/gadm36_AUT.gpkg', driver='GPKG') | |
# fixup https://gis.stackexchange.com/questions/311931/geopandas-equivalent-of-st-makepolygon/312004#312004 | |
polygons = df.geometry.apply(lambda x: list(x))[0] | |
polygons.append(polygons[0].intersection(polygons[1]).buffer(0.0001)) | |
combined = [unary_union(polygons)] | |
df.geometry = combined | |
gj = gp.GeoSeries([df.geometry[0]]).__geo_interface__ | |
geoJson = gj['features'][0]['geometry'] | |
size = 7 | |
hexagons = pd.DataFrame(h3.polyfill(geoJson, size), columns=['hexagons']) | |
hexagons.head() | |
from shapely.geometry.polygon import Polygon | |
hexagons['geometry'] = hexagons.hexagons.apply(lambda hex: Polygon(h3.h3_to_geo_boundary(hex))) | |
hexagons['centeroid'] = hexagons.hexagons.apply(lambda hex: h3.h3_to_geo(hex)) | |
crs = {'init': 'epsg:4326'} | |
hexagons = gp.GeoDataFrame(hexagons, crs=crs, geometry=hexagons.geometry) | |
hexagons['x_long'] = hexagons['centeroid'].apply(lambda x: x[0]) | |
hexagons['y_lat'] = hexagons['centeroid'].apply(lambda x: x[1]) | |
hexagons['value'] = 1 | |
hexagons = hexagons.drop(['centeroid'], axis=1) | |
display(hexagons.head()) | |
hexagons.to_csv(f'at_hex_{size}.csv', index=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment