question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DOC: Can I perform operations on the geocube pixels during rasterization? Count, Mean, etc.

See original GitHub issue

Hi this is my first foray into geocube. Thanks for your work here, clearly a good idea.

I’d like to add an example usage that I believe should be possible, but not just quite getting right.

Is it possible to count the number of polygons in a cell, or perform operations on the geocube pixels during rasterization? Imagine a geopandas dataframe with many polygons. For each cell, perform some numpy operation (like rasterstats does) on the incoming polygons. I can perform pandas like operation after creating a geocube, but it doesn’t preserve the spatial object. I feel like the group_by argument is in this direction, but could not succeed.

Something like

#Turn each set of predictions into a raster
import geopandas as gpd
from shapely.geometry import Polygon
from geocube.api.core import make_geocube

#a numeric column to count
g["mask"] = 1

cube = make_geocube(vector_data=g, resolution=(-50, 50), function=mask.count())

To create a heatmap of number of polygons in each cell.

Here is a notebook to help illustrate use case (sorry the geocube svg doesn’t play well with ipython on github).

https://github.com/weecology/NEON_crown_maps/blob/master/notebooks/Create a raster of polygon counts.ipynb

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
davidbrochartcommented, Jul 29, 2022

Fixed by passing all_touched=True:

rasterize_function=partial(rasterize_image, merge_alg=MergeAlg.add, all_touched=True)
1reaction
bw4szcommented, Jun 19, 2020

yup. that did it. I’m away for the weekend, but i’ll try to fork and submit a notebook PR to add as a resource on monday. Thanks.

On Thu, Jun 18, 2020 at 8:12 PM Alan D. Snow notifications@github.com wrote:

The second one is correct. I suggest doing cube.mask.plot()

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/corteva/geocube/issues/28#issuecomment-646411054, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJHBLB6QF7EZKAYMWMTV5DRXLJSNANCNFSM4OCEMGIA .

– Ben Weinstein, Ph.D. Postdoctoral Fellow University of Florida http://benweinstein.weebly.com/

Read more comments on GitHub >

github_iconTop Results From Across the Web

Source code for geocube.rasterize
coding: utf-8 -- """ This module contains tools for rasterizing vector data. ... If True, all pixels touched by geometries will be burned...
Read more >
Rasterization: a Practical Implementation (An ... - Scratchapixel
We first project the 3D vertices making up triangles onto the screen using perspective projection. Then, we loop over all pixel in the...
Read more >
Software Modules on the Faster Cluster
ABAQUS, 'Finite Element Analysis software for modeling, visualization and best-in-class implicit and explicit dynamics FEA.' ; ABINIT ...
Read more >
How to use geocube with option -at (all_touched=TRUE)
First, I want to rasterize roads separately to make them look better by using the option "all_touched"=True. And then I want to merge...
Read more >
Towards Predictive Rendering in Virtual Reality - CORE
in the future and do not represent the current state of Virtual Reality ... many effects (e.g., spectral rendering, polarization, diffraction, etc.) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found