Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to replicate https://cogeotiff.github.io/rio-tiler/advanced/feature/ #306

Closed
vincentsarago opened this issue Nov 23, 2020 · 5 comments · Fixed by #307
Closed

Comments

@vincentsarago
Copy link
Member

Reading a file for a specific feature seems a bit more natural than using a bbox for some users

We have a way of doing this using cutline

from rio_tiler.io import COGReader
from rio_tiler.utils import create_cutline
from rasterio.features import bounds as featureBounds

feat =     {
    "type": "Feature",
    "properties": {},
    "geometry": {
    "type": "Polygon",
    "coordinates": [
        [
        [-52.6025390625, 73.86761239709705],
        [-52.6025390625, 73.59679245247814],
        [-51.591796875, 73.60299628304274],
        [-51.591796875, 73.90420357134279],
        [-52.4267578125, 74.0437225981325],
        [-52.6025390625, 73.86761239709705]
        ]
    ]
    }
}

# Get BBOX of the polygon
bbox = featureBounds(feat)

# Use COGReader to open and read the dataset
with COGReader("my_tif.tif") as cog:
    # Create WTT Cutline
    cutline = create_cutline(cog.dataset, feat, geometry_crs="epsg:4326")

    # Read part of the data (bbox) and use the cutline to mask the data
    data, mask = cog.part(bbox, vrt_options={'cutline': cutline})

but it will be nicer if we could directly pass the feature to a method (or to the part() method)

from rio_tiler.io import COGReader

feat =     {
    "type": "Feature",
    "properties": {},
    "geometry": {
    "type": "Polygon",
    "coordinates": [
        [
        [-52.6025390625, 73.86761239709705],
        [-52.6025390625, 73.59679245247814],
        [-51.591796875, 73.60299628304274],
        [-51.591796875, 73.90420357134279],
        [-52.4267578125, 74.0437225981325],
        [-52.6025390625, 73.86761239709705]
        ]
    ]
    }
}

with COGReader("my_tif.tif") as cog:
    data, mask = cog.part(None, feature=feat)
@kylebarron
Copy link
Member

I feel like it might be better to define a separate method for this, instead of overloading part. Maybe cog.feature or something

@vincentsarago
Copy link
Member Author

@kylebarron I agree, at first I didn't like feature... but we do have tile() so having a feature method that returns data for features make sense ;-)

@kylebarron
Copy link
Member

Well I'm not sure I like feature either, because it doesn't return a feature, it returns data corresponding to feature input

@vincentsarago
Copy link
Member Author

well tile doesn't return a tile ... it retunrs a data for a tile index input 🤷‍♂️

@kylebarron
Copy link
Member

Yeah I guess you're right

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants