Git Product home page Git Product logo

Comments (5)

giswqs avatar giswqs commented on June 19, 2024

Have you tried using the zonal_stats function?
https://geemap.org/notebooks/12_zonal_statistics/

from geemap.

Remolkeman avatar Remolkeman commented on June 19, 2024

Hi!

Following your suggestion, I tried the zonal_stats function, and the following are the results

No for loop:

import ee
import geemap
import os

ee.Authenticate()
ee.Initialize()

def calculateNDVI(image):
    ndvi = image.normalizedDifference(["B8", "B4"])
    return ndvi.rename("NDVI")

geometry = ee.Geometry.Polygon(
    [
        [
            [-6.310153895594499, 36.89932544542902],
            [-6.30912392733278, 36.89637395041979],
            [-6.308093959071061, 36.892461327607315],
            [-6.304574900843522, 36.89294183594877],
            [-6.298480921961686, 36.894108772184815],
            [-6.293674403406999, 36.895618898477814],
            [-6.294618540980241, 36.89726627490264],
            [-6.297536784388444, 36.903855425007976],
            [-6.301399165369889, 36.90275727283513],
            [-6.305519038416764, 36.90207091970124],
            [-6.310411387659928, 36.90124728779126],
        ]
    ]
)

collection = (
    ee.ImageCollection("COPERNICUS/S2_HARMONIZED")
    .filterBounds(geometry)
    .filterDate("2015-01-01", "2022-01-15")
    .filterMetadata("CLOUDY_PIXEL_PERCENTAGE", "less_than", 10)
)

ndviCollection = collection.map(calculateNDVI)

geometry_feature = ee.FeatureCollection(geometry)
scale = 10

out_ndvi_stats = os.path.join(os.path.expanduser("~"), "Downloads", "ndvi_stats.csv")
geemap.zonal_stats(ndviCollection, geometry_feature, out_ndvi_stats, stat_type='MEAN', scale=scale)

geemap.create_download_link(out_ndvi_stats)


In this case, it does output a csv with the mean values properly and not empty.

"
Computing statistics ...
Generating URL ...
Downloading data from https://earthengine.googleapis.com/v1/projects/earthengine-legacy/tables/34324237bcb4dd798c95c943ae4842e8-3f0985e16b05496e4c0d4373a875a4c0:getFeatures
Please wait ...
Data downloaded to C:\Users\user\Downloads\ndvi_stats.csv
"

However, when using a for loop, it does not work properly.

import ee
import geemap
import os

ee.Authenticate()
ee.Initialize()

def calculateNDVI(image):
    ndvi = image.normalizedDifference(["B8", "B4"])
    return ndvi.rename("NDVI")

geometry = ee.Geometry.Polygon(
    [
        [
            [-6.310153895594499, 36.89932544542902],
            [-6.30912392733278, 36.89637395041979],
            [-6.308093959071061, 36.892461327607315],
            [-6.304574900843522, 36.89294183594877],
            [-6.298480921961686, 36.894108772184815],
            [-6.293674403406999, 36.895618898477814],
            [-6.294618540980241, 36.89726627490264],
            [-6.297536784388444, 36.903855425007976],
            [-6.301399165369889, 36.90275727283513],
            [-6.305519038416764, 36.90207091970124],
            [-6.310411387659928, 36.90124728779126],
        ]
    ]
)

collection = (
    ee.ImageCollection("COPERNICUS/S2_HARMONIZED")
    .filterBounds(geometry)
    .filterDate("2015-01-01", "2022-01-15")
    .filterMetadata("CLOUDY_PIXEL_PERCENTAGE", "less_than", 10)
)

ndviCollection = collection.map(calculateNDVI)


geometry_feature = ee.FeatureCollection(geometry)


scale = 10

out_dir = os.path.join(os.path.expanduser("~"), "Downloads")

if not os.path.exists(out_dir):
    os.makedirs(out_dir)

for i, image in enumerate(ndviCollection.toList(ndviCollection.size())):
    image = ee.Image(image)
    out_ndvi_stats = os.path.join(out_dir, f"ndvi_stats_{i}.csv")
    geemap.zonal_stats(image, geometry_feature, out_ndvi_stats, stat_type='MEAN', scale=scale)

"
ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))"
}
"
What I am looking for is to extract the ndvi value of each pixel in each image of the collection inside the designed geometry. As I could check, this function returns mean, std, sum, etc. It seems that something is wrong in the for loop, and I might be failing to properly program it.

from geemap.

giswqs avatar giswqs commented on June 19, 2024

Use collection.toBands() instead of a for loop.

import ee
import geemap
import os

ee.Authenticate()
ee.Initialize()

def calculateNDVI(image):
    ndvi = image.normalizedDifference(["B8", "B4"])
    return ndvi.rename("NDVI")

geometry = ee.Geometry.Polygon(
    [
        [
            [-6.310153895594499, 36.89932544542902],
            [-6.30912392733278, 36.89637395041979],
            [-6.308093959071061, 36.892461327607315],
            [-6.304574900843522, 36.89294183594877],
            [-6.298480921961686, 36.894108772184815],
            [-6.293674403406999, 36.895618898477814],
            [-6.294618540980241, 36.89726627490264],
            [-6.297536784388444, 36.903855425007976],
            [-6.301399165369889, 36.90275727283513],
            [-6.305519038416764, 36.90207091970124],
            [-6.310411387659928, 36.90124728779126],
        ]
    ]
)

collection = (
    ee.ImageCollection("COPERNICUS/S2_HARMONIZED")
    .filterBounds(geometry)
    .filterDate("2015-01-01", "2022-01-15")
    .filterMetadata("CLOUDY_PIXEL_PERCENTAGE", "less_than", 10)
)

ndviCollection = collection.map(calculateNDVI)


geometry_feature = ee.FeatureCollection(geometry)


scale = 10

out_dir = os.path.join(os.path.expanduser("~"), "Downloads")

if not os.path.exists(out_dir):
    os.makedirs(out_dir)

image = ndviCollection.toBands()

out_ndvi_stats = "ndvi.csv"
geemap.zonal_stats(image, geometry_feature, out_ndvi_stats, stat_type='MEAN', scale=scale)

from geemap.

Remolkeman avatar Remolkeman commented on June 19, 2024

Hi Qiusheng Wu!

It is very likely that I have not properly explained the problem I am facing. To that end, I want to give you some context to help with your understanding:

I am currently doing a final master thesis, in which I am applying common remote sensing techniques such as NDVI, NDMI, SAVI..... on several delimited areas within the Guadalquivir estuary(Spain). Thus far, I have created a time series that represents the mean value of the indices. What I am currently trying to do is to use each satellite image within these time series and represent the value of each pixel for NDVI, NDMI, etc. in a histogram, and thus check the distribution of the data outside a mean. For example, imagine a Sentinel-2 image for the date 25/12/2015. In this case, I want to know the NDVI value of each pixel in that defined area, represent it in a histogram, and check the distribution of this data for that particular day of that year. In the case of my study, I would have approximately 2000+ images between the areas. Therefore, I want to apply a for loop that uses these NDVI pixel values and represent them in a histogram for each of these images and, if possible, save the NDVI(I write NDVI, but it would be for each index) pixel values in a csv. In short, I am not looking for mean values, but the pixel values.

When I tried to apply this loop, I encountered the problem reported above, returning an empty output for using the loop.

from geemap.

Remolkeman avatar Remolkeman commented on June 19, 2024

up!

from geemap.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.