Comments (3)
Version 2, adding multilinestring as valid input type.
from shapely.geometry.linestring import LineString
from shapely.geometry.multilinestring import MultiLineString
from typing import Iterator, Union
def sequential_deduplication(func: Iterator[str]) -> Iterator[str]:
'''
Decorator that doesn't permit two consecutive items to be the same
'''
def inner(*args):
iterable = func(*args)
last = None
while (cell := next(iterable, None)) is not None:
if cell != last:
yield cell
last = cell
return inner
@sequential_deduplication
def h3polyline(line: Union[LineString, MultiLineString], resolution: int) -> Iterator[str]:
'''
Iterator yeilding H3 cells representing a (multi)line,
retaining order and self-intersections
'''
if line.geom_type == 'MultiLineString':
# Recurse after getting component linestrings from the multiline
for l in map(lambda geom: h3polyline(geom, resolution), line.geoms):
yield from l
else:
coords = zip(line.coords, line.coords[1:])
while (vertex_pair := next(coords, None)) is not None:
i, j = vertex_pair
a = h3.geo_to_h3(*i[::-1], resolution)
b = h3.geo_to_h3(*j[::-1], resolution)
yield from h3.h3_line(a, b) # inclusive of a and b
from h3-pandas.
I was just working on an H3 demo for a GIS forum, and wanted to demonstrate this. Came up with:
from shapely.geometry.linestring import LineString
from typing import Iterator
def sequential_deduplication(func: Iterator[str]) -> Iterator[str]:
'''
Decorator that doesn't permit two consecutive items to be the same
'''
def inner(*args):
iterable = func(*args)
last = None
while (cell := next(iterable, None)) is not None:
if cell != last:
yield cell
last = cell
return inner
@sequential_deduplication # prevent consecutive repetition, esp. at low res
def h3polyline(line: LineString, resolution: int) -> Iterator[str]:
'''
Iterator yielding H3 cells representing a line,
retaining order and any self-intersections
'''
coords = zip(line.coords, line.coords[1:])
while (vertex_pair := next(coords, None)) is not None:
i, j = vertex_pair
a = h3.geo_to_h3(*i[::-1], resolution)
b = h3.geo_to_h3(*j[::-1], resolution)
yield from h3.h3_line(a, b) # inclusive of a and b
Just thought it might help someone. I'm not sure there's any particular advantage doing this with generators, unless/until h3.h3_line
is itself a generator.
Not sure what is supposed to happen with repeated cells, but intuitively I thought it made sense to attempt to retain order and self-intersections rather than to just return an unordered set.
Doesn't consider MultiLineStrings.
from h3-pandas.
Closed by #24 released as 0.2.6 🎉
from h3-pandas.
Related Issues (18)
- ENH: Support vectorized operations
- CRS Warning for k_ring_smoothing when using k param HOT 5
- support h3 v4.0.0 HOT 1
- h3_to_parent at resolution 0 labels it as direct parent HOT 1
- how do the weights for k_ring_smoothing work
- Consider integrating different geospatial indexing systems HOT 1
- handle nulls in `k_ring_smoothing`
- GeoDataFrame has no attribute 'h3' only when multiprocessing using JobLib HOT 2
- Q: Multicore support? HOT 4
- 13 and higher H3-resolution HOT 3
- Implement h3.compact and uncompact HOT 2
- geopandas integration, support arbitrary geometry column name
- ValueError using `polyfill` + `h3_to_geo_boundary` HOT 2
- ENH: Implement a "finer `polyfill`" HOT 3
- ENH: `polyfill_resample` distributes numeric values appropriately
- ENH: Support other H3 APIs
- ENH: Implement `h3_to_children`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from h3-pandas.