H3Pandas module¶
H3Pandas
¶
Source code in vgridpandas/h3pandas.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | |
h32geo(h3_col=None, fix_antimeridian=None)
¶
Add geometry with H3 geometry to the DataFrame.
Source code in vgridpandas/h3pandas.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | |
h3bin(resolution, stats='count', numeric_col=None, category_col=None, lat_col='lat', lon_col='lon', fix_antimeridian=None)
¶
Bin points into h3 cells and compute statistics.
Source code in vgridpandas/h3pandas.py
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | |
latlon2h3(resolution, lat_col='lat', lon_col='lon', set_index=False)
¶
Adds H3 index to (Geo)DataFrame.
pd.DataFrame: uses lat_col and lon_col (default lat and lon)
gpd.GeoDataFrame: uses geometry
Assumes coordinates in epsg=4326.
Parameters¶
resolution : int H3 resolution lat_col : str Name of the latitude column (if used), default 'lat' lon_col : str Name of the longitude column (if used), default 'lon' set_index : bool If True, the column with H3 ID is set as index, default False
Returns¶
(Geo)DataFrame with H3 ID added
See Also¶
geo2h3_aggregate : Extended API method that aggregates points by H3 id
Examples¶
df = pd.DataFrame({'lat': [50, 51], 'lon':[14, 15]}) df.h3.latlon2h3(8) lat lon h3 881e309739fffff 50 14 881e2659c3fffff 51 15
df.h3.latlon2h3(8, set_index=False) lat lon h3 0 50 14 881e309739fffff 1 51 15 881e2659c3fffff
gdf = gpd.GeoDataFrame({'val': [5, 1]}, geometry=gpd.points_from_xy(x=[14, 15], y=(50, 51))) gdf.h3.latlon2h3(8) val geometry h3 881e309739fffff 5 POINT (14.00000 50.00000) 881e2659c3fffff 1 POINT (15.00000 51.00000)
Source code in vgridpandas/h3pandas.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | |
linetrace(resolution, explode=False)
¶
An H3 cell representation of a (Multi)LineString traced along its vertices.
Parameters¶
resolution : int H3 resolution explode : bool If True, will explode the resulting list vertically. All other columns' values are copied. Default: False
Returns¶
(Geo)DataFrame with H3 cells with centroids within the input polygons.
Examples¶
from shapely.geometry import LineString gdf = gpd.GeoDataFrame(geometry=[LineString([[0, 0], [1, 0], [1, 1]])]) gdf.h3.linetrace(4) geometry h3_linetrace 0 LINESTRING (0.00000 0.00000, 1.00000 0.00000, ... [83754efffffffff, 83754cfffffffff, 837541fffff... # noqa E501 gdf.h3.linetrace(4, explode=True) geometry h3_linetrace 0 LINESTRING (0.00000 0.00000, 1.00000 0.00000, ... 83754efffffffff 0 LINESTRING (0.00000 0.00000, 1.00000 0.00000, ... 83754cfffffffff 0 LINESTRING (0.00000 0.00000, 1.00000 0.00000, ... 837541fffffffff
Source code in vgridpandas/h3pandas.py
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | |
polyfill(resolution, predicate=None, compact=False, explode=False, fix_antimeridian=None)
¶
Parameters¶
resolution : int H3 resolution predicate : str, optional Spatial predicate to apply ('intersect', 'within', 'centroid_within', 'largest_overlap') compact : bool, optional Enable H3 compact mode explode : bool If True, will explode the resulting list vertically. All other columns' values are copied. Default: False fix_antimeridian : str, optional Antimeridian fix: 'shift', 'shift_balanced', 'shift_west', 'shift_east', or 'split'
Source code in vgridpandas/h3pandas.py
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |
linetrace(geometry, resolution)
¶
h3.polyfill equivalent for shapely (Multi)LineString.
Cells may repeat at self-intersections or shared vertices.
Parameters¶
geometry : LineString or MultiLineString Line to trace with H3 cells resolution : int H3 resolution of the tracing cells
Returns¶
Set of H3 IDs
Raises¶
TypeError if geometry is not a LineString or a MultiLineString
Source code in vgridpandas/h3pandas.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
poly2h3(geometry, resolution, predicate=None, compact=False, fix_antimeridian=None)
¶
Convert polygon geometries (Polygon, MultiPolygon) to H3 grid cells.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resolution
|
int
|
H3 resolution level [0..15] |
required |
geometry
|
Polygon or MultiPolygon
|
Polygon geometry to convert |
required |
predicate
|
str
|
Spatial predicate to apply ('intersect', 'within', 'centroid_within', 'largest_overlap') |
None
|
compact
|
bool
|
Enable H3 compact mode |
False
|
fix_antimeridian
|
str
|
'shift', 'shift_balanced', 'shift_west', 'shift_east', or 'split' |
None
|
Example
from shapely.geometry import Polygon poly = Polygon([(-122.5, 37.7), (-122.3, 37.7), (-122.3, 37.9), (-122.5, 37.9)]) cells = poly2h3(poly, 10, predicate="intersect", compact=True) len(cells) > 0 True
Source code in vgridpandas/h3pandas.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
polyfill_row(geometry, resolution, predicate=None, compact=False, fix_antimeridian=None)
¶
Return cell ids covering a single row geometry.
Source code in vgridpandas/h3pandas.py
105 106 107 108 109 110 111 112 113 114 115 116 117 | |