Back to Article

###### 3-5 spatial modeling - environmental correlations

Download Notebook

This script extracts nitrogen, phosphorus, woody vegetation pixel coverage, and mean annual precipitation from google earth engine for correlation

The purpose is to show how the locust outbreak model disentangled the positive correlation between nitrogen and these variables

read the manuscript for further discussion

# Import libraries and initialize google earth engine

    In [1]:

    import ee
    import pandas as pd
    import os
    import time

    # Initialize Earth Engine
    ee.Initialize()

# set relative work directory

    In [3]:

    os.chdir(os.path.abspath(os.path.join(os.getcwd(), os.pardir)))
    os.getcwd()

    '/home/datascience/herbivore_nutrient_interactions'

# Load Australia shape

    In [4]:

    # Load the countries feature collection
    countries = ee.FeatureCollection("USDOS/LSIB_SIMPLE/2017")

    # Filter the feature collection to get the boundary of Australia
    australia = countries.filter(ee.Filter.eq('country_na', 'Australia'))

# Load in rasters

    In [5]:

    # Load the two rasters
    raster1 = ee.ImageCollection('CSIRO/SLGA') \
        .filter(ee.Filter.eq('attribute_code', 'NTO')) \
        .select(['NTO_000_005_EV', 'NTO_005_015_EV']).mean()

    raster2 = ee.ImageCollection("NASA/MEASURES/GFCC/TC/v3") \
        .select("tree_canopy_cover") \
        .filter(ee.Filter.calendarRange(2000, 2017, 'year')).mean()

    raster3 = ee.Image("WORLDCLIM/V1/BIO").select('bio12')

    raster4 = ee.ImageCollection('CSIRO/SLGA') \
        .filter(ee.Filter.eq('attribute_code', 'PTO')) \
        .select(['PTO_000_005_EV', 'PTO_005_015_EV']).mean()

    # Combine the temperature bands from each image into a single image
    temp_bands = raster1.addBands(raster2).addBands(raster3).addBands(raster4)

# sample the australia shapefile

    In [6]:

    # Generate random points
    sample = ee.FeatureCollection.randomPoints(
            region=australia, points=100000, seed=420, maxError=1
    )

    # Sample the the bands using the sample point feature collection.
    imgSamp = temp_bands.sampleRegions(
      collection = sample,
      scale = 30
    )

# For loop to extract data from google earth engine

    In [8]:

    # Set batch size
    batch_size = 500
    total_points = 100000
    df = pd.DataFrame()

    # Process in batches of 500
    for start in range(0, total_points, batch_size):
        # Define the end of the batch
        end = min(start + batch_size, total_points)

        # Get the batch of points
        batch_sample = sample.toList(batch_size, start)
        batch_sample_fc = ee.FeatureCollection(batch_sample)

        # Sample the raster data
        img_samp_batch = temp_bands.sampleRegions(
            collection=batch_sample_fc,
            scale=30
        )

        # Get the result as a list of dictionaries
        try:
            sample_dict = img_samp_batch.getInfo()['features']
            rows = [feature['properties'] for feature in sample_dict]
            df_batch = pd.DataFrame(rows)

            # Append the batch to the main DataFrame
            df = pd.concat([df, df_batch], ignore_index=True)

            print(f"Processed batch {start + 1} to {end}")

            # Add a delay to avoid overwhelming the API
            time.sleep(1)

        except Exception as e:
            print(f"Error processing batch {start + 1} to {end}: {e}")

    # Display the DataFrame
    print(df.head())

    Processed batch 1 to 500
    Processed batch 501 to 1000
    Processed batch 1001 to 1500
    Processed batch 1501 to 2000
    Processed batch 2001 to 2500
    Processed batch 2501 to 3000
    Processed batch 3001 to 3500
    Processed batch 3501 to 4000
    Processed batch 4001 to 4500
    Processed batch 4501 to 5000
    Processed batch 5001 to 5500
    Processed batch 5501 to 6000
    Processed batch 6001 to 6500
    Processed batch 6501 to 7000
    Processed batch 7001 to 7500
    Processed batch 7501 to 8000
    Processed batch 8001 to 8500
    Processed batch 8501 to 9000
    Processed batch 9001 to 9500
    Processed batch 9501 to 10000
    Processed batch 10001 to 10500
    Processed batch 10501 to 11000
    Processed batch 11001 to 11500
    Processed batch 11501 to 12000
    Processed batch 12001 to 12500
    Processed batch 12501 to 13000
    Processed batch 13001 to 13500
    Processed batch 13501 to 14000
    Processed batch 14001 to 14500
    Processed batch 14501 to 15000
    Processed batch 15001 to 15500
    Processed batch 15501 to 16000
    Processed batch 16001 to 16500
    Processed batch 16501 to 17000
    Processed batch 17001 to 17500
    Processed batch 17501 to 18000
    Processed batch 18001 to 18500
    Processed batch 18501 to 19000
    Processed batch 19001 to 19500
    Processed batch 19501 to 20000
    Processed batch 20001 to 20500
    Processed batch 20501 to 21000
    Processed batch 21001 to 21500
    Processed batch 21501 to 22000
    Processed batch 22001 to 22500
    Processed batch 22501 to 23000
    Processed batch 23001 to 23500
    Processed batch 23501 to 24000
    Processed batch 24001 to 24500
    Processed batch 24501 to 25000
    Processed batch 25001 to 25500
    Processed batch 25501 to 26000
    Processed batch 26001 to 26500
    Processed batch 26501 to 27000
    Processed batch 27001 to 27500
    Processed batch 27501 to 28000
    Processed batch 28001 to 28500
    Processed batch 28501 to 29000
    Processed batch 29001 to 29500
    Processed batch 29501 to 30000
    Processed batch 30001 to 30500
    Processed batch 30501 to 31000
    Processed batch 31001 to 31500
    Processed batch 31501 to 32000
    Processed batch 32001 to 32500
    Processed batch 32501 to 33000
    Processed batch 33001 to 33500
    Processed batch 33501 to 34000
    Processed batch 34001 to 34500
    Processed batch 34501 to 35000
    Processed batch 35001 to 35500
    Processed batch 35501 to 36000
    Processed batch 36001 to 36500
    Processed batch 36501 to 37000
    Processed batch 37001 to 37500
    Processed batch 37501 to 38000
    Processed batch 38001 to 38500
    Processed batch 38501 to 39000
    Processed batch 39001 to 39500
    Processed batch 39501 to 40000
    Processed batch 40001 to 40500
    Processed batch 40501 to 41000
    Processed batch 41001 to 41500
    Processed batch 41501 to 42000
    Processed batch 42001 to 42500
    Processed batch 42501 to 43000
    Processed batch 43001 to 43500
    Processed batch 43501 to 44000
    Processed batch 44001 to 44500
    Processed batch 44501 to 45000
    Processed batch 45001 to 45500
    Processed batch 45501 to 46000
    Processed batch 46001 to 46500
    Processed batch 46501 to 47000
    Processed batch 47001 to 47500
    Processed batch 47501 to 48000
    Processed batch 48001 to 48500
    Processed batch 48501 to 49000
    Processed batch 49001 to 49500
    Processed batch 49501 to 50000
    Processed batch 50001 to 50500
    Processed batch 50501 to 51000
    Processed batch 51001 to 51500
    Processed batch 51501 to 52000
    Processed batch 52001 to 52500
    Processed batch 52501 to 53000
    Processed batch 53001 to 53500
    Processed batch 53501 to 54000
    Processed batch 54001 to 54500
    Processed batch 54501 to 55000
    Processed batch 55001 to 55500
    Processed batch 55501 to 56000
    Processed batch 56001 to 56500
    Processed batch 56501 to 57000
    Processed batch 57001 to 57500
    Processed batch 57501 to 58000
    Processed batch 58001 to 58500
    Processed batch 58501 to 59000
    Processed batch 59001 to 59500
    Processed batch 59501 to 60000
    Processed batch 60001 to 60500
    Processed batch 60501 to 61000
    Processed batch 61001 to 61500
    Processed batch 61501 to 62000
    Processed batch 62001 to 62500
    Processed batch 62501 to 63000
    Processed batch 63001 to 63500
    Processed batch 63501 to 64000
    Processed batch 64001 to 64500
    Processed batch 64501 to 65000
    Processed batch 65001 to 65500
    Processed batch 65501 to 66000
    Processed batch 66001 to 66500
    Processed batch 66501 to 67000
    Processed batch 67001 to 67500
    Processed batch 67501 to 68000
    Processed batch 68001 to 68500
    Processed batch 68501 to 69000
    Processed batch 69001 to 69500
    Processed batch 69501 to 70000
    Processed batch 70001 to 70500
    Processed batch 70501 to 71000
    Processed batch 71001 to 71500
    Processed batch 71501 to 72000
    Processed batch 72001 to 72500
    Processed batch 72501 to 73000
    Processed batch 73001 to 73500
    Processed batch 73501 to 74000
    Processed batch 74001 to 74500
    Processed batch 74501 to 75000
    Processed batch 75001 to 75500
    Processed batch 75501 to 76000
    Processed batch 76001 to 76500
    Processed batch 76501 to 77000
    Processed batch 77001 to 77500
    Processed batch 77501 to 78000
    Processed batch 78001 to 78500
    Processed batch 78501 to 79000
    Processed batch 79001 to 79500
    Processed batch 79501 to 80000
    Processed batch 80001 to 80500
    Processed batch 80501 to 81000
    Processed batch 81001 to 81500
    Processed batch 81501 to 82000
    Processed batch 82001 to 82500
    Processed batch 82501 to 83000
    Processed batch 83001 to 83500
    Processed batch 83501 to 84000
    Processed batch 84001 to 84500
    Processed batch 84501 to 85000
    Processed batch 85001 to 85500
    Processed batch 85501 to 86000
    Processed batch 86001 to 86500
    Processed batch 86501 to 87000
    Processed batch 87001 to 87500
    Processed batch 87501 to 88000
    Processed batch 88001 to 88500
    Processed batch 88501 to 89000
    Processed batch 89001 to 89500
    Processed batch 89501 to 90000
    Processed batch 90001 to 90500
    Processed batch 90501 to 91000
    Processed batch 91001 to 91500
    Processed batch 91501 to 92000
    Processed batch 92001 to 92500
    Processed batch 92501 to 93000
    Processed batch 93001 to 93500
    Processed batch 93501 to 94000
    Processed batch 94001 to 94500
    Processed batch 94501 to 95000
    Processed batch 95001 to 95500
    Processed batch 95501 to 96000
    Processed batch 96001 to 96500
    Processed batch 96501 to 97000
    Processed batch 97001 to 97500
    Processed batch 97501 to 98000
    Processed batch 98001 to 98500
    Processed batch 98501 to 99000
    Processed batch 99001 to 99500
    Processed batch 99501 to 100000
       NTO_000_005_EV  NTO_005_015_EV  PTO_000_005_EV  PTO_005_015_EV  bio12  \
    0        0.036264        0.032103        0.017605        0.016432    223
    1        0.031465        0.029525        0.018839        0.018417    163
    2        0.060444        0.055346        0.022199        0.021432   1050
    3        0.147743        0.118303        0.029048        0.026754    850
    4        0.048120        0.042801        0.024990        0.024281    289

       tree_canopy_cover
    0               0.00
    1               0.00
    2              11.00
    3               5.00
    4               2.75

    In [10]:

    df.to_csv('data/processed/spatial_modeling/environmental_correlation_data.csv', index=False)