1

I'm trying to classify NAIP imagery for HUC0801 subregion using random forest classifier on GEE.

var water = /* color: #003dd6 */ee.FeatureCollection(
        [ee.Feature(
            ee.Geometry.Point([-89.77880711922072, 35.77516614372526]),
            {
              "LC": 0,
              "system:index": "0"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.6957230127754, 35.79605417549729]),
            {
              "LC": 0,
              "system:index": "1"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.32132424310237, 35.72441548641423]),
            {
              "LC": 0,
              "system:index": "2"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.20658301966641, 35.71620147983874]),
            {
              "LC": 0,
              "system:index": "3"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.17112199599988, 35.69422952164938]),
            {
              "LC": 0,
              "system:index": "4"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.07497245212244, 35.68920813448804]),
            {
              "LC": 0,
              "system:index": "5"
            }),
        ee.Feature(
            ee.Geometry.Point([-88.8463379266921, 35.68473167174959]),
            {
              "LC": 0,
              "system:index": "6"
            }),
        ee.Feature(
            ee.Geometry.Point([-88.84350615349292, 35.68599662392148]),
            {
              "LC": 0,
              "system:index": "7"
            }),
        ee.Feature(
            ee.Geometry.Point([-88.72815052861881, 35.647747724351476]),
            {
              "LC": 0,
              "system:index": "8"
            }),
        ee.Feature(
            ee.Geometry.Point([-88.72727267449217, 35.64407864998295]),
            {
              "LC": 0,
              "system:index": "9"
            }),
        ee.Feature(
            ee.Geometry.Point([-88.72167692150965, 35.63229583998486]),
            {
              "LC": 0,
              "system:index": "10"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.15503144021831, 35.488690451296556]),
            {
              "LC": 0,
              "system:index": "11"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.15864916744249, 35.48621924444361]),
            {
              "LC": 0,
              "system:index": "12"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.20729961346346, 35.50199071525465]),
            {
              "LC": 0,
              "system:index": "13"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.2222590639712, 35.50967786008748]),
            {
              "LC": 0,
              "system:index": "14"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.29663712988093, 35.47670373082221]),
            {
              "LC": 0,
              "system:index": "15"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.35643912829886, 35.45594796661757]),
            {
              "LC": 0,
              "system:index": "16"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.24539255774611, 35.38898565131322]),
            {
              "LC": 0,
              "system:index": "17"
            }),
        ee.Feature(
            ee.Geometry.Point([-88.96370057042756, 36.23184732630431]),
            {
              "LC": 0,
              "system:index": "18"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.0770205752653, 36.27354920070623]),
            {
              "LC": 0,
              "system:index": "19"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.01798461755284, 36.383763269817216]),
            {
              "LC": 0,
              "system:index": "20"
            }),
        ee.Feature(
            ee.Geometry.Point([-88.6345629357191, 36.4454911242644]),
            {
              "LC": 0,
              "system:index": "21"
            }),
        ee.Feature(
            ee.Geometry.Point([-88.4347827664095, 36.43859537979183]),
            {
              "LC": 0,
              "system:index": "22"
            }),
        ee.Feature(
            ee.Geometry.Point([-88.59172848728244, 36.25293981790439]),
            {
              "LC": 0,
              "system:index": "23"
            }),
        ee.Feature(
            ee.Geometry.Point([-88.63267792702267, 36.26079740190436]),
            {
              "LC": 0,
              "system:index": "24"
            })]),
    riparian = /* color: #98ff00 */ee.FeatureCollection(
        [ee.Feature(
            ee.Geometry.Point([-89.44225414985786, 35.53955223803894]),
            {
              "LC": 1,
              "system:index": "0"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.32307495072952, 35.66054547775215]),
            {
              "LC": 1,
              "system:index": "1"
            }),
        ee.Feature(
            ee.Geometry.Point([-88.89819406874301, 36.619508302264094]),
            {
              "LC": 1,
              "system:index": "2"
            }),
        ee.Feature(
            ee.Geometry.Point([-88.70268838236554, 36.44424969773752]),
            {
              "LC": 1,
              "system:index": "3"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.13061438726855, 35.94598128024016]),
            {
              "LC": 1,
              "system:index": "4"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.0092790557817, 35.667661574012776]),
            {
              "LC": 1,
              "system:index": "5"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.04848437911049, 35.38707402518666]),
            {
              "LC": 1,
              "system:index": "6"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.31328921448181, 35.77553921537374]),
            {
              "LC": 1,
              "system:index": "7"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.65070057020814, 35.31036090188487]),
            {
              "LC": 1,
              "system:index": "8"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.40214903746934, 35.05153508702478]),
            {
              "LC": 1,
              "system:index": "9"
            }),
        ee.Feature(
            ee.Geometry.Point([-88.73028482225335, 35.1754873249236]),
            {
              "LC": 1,
              "system:index": "10"
            }),
        ee.Feature(
            ee.Geometry.Point([-88.89441909566804, 35.20694571831696]),
            {
              "LC": 1,
              "system:index": "11"
            }),
        ee.Feature(
            ee.Geometry.Point([-88.92119827047273, 35.2144492934566]),
            {
              "LC": 1,
              "system:index": "12"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.2439111192682, 35.27378436859066]),
            {
              "LC": 1,
              "system:index": "13"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.09345795254362, 35.32586405437563]),
            {
              "LC": 1,
              "system:index": "14"
            }),
        ee.Feature(
            ee.Geometry.Point([-88.77842921934243, 35.58418661604577]),
            {
              "LC": 1,
              "system:index": "15"
            }),
        ee.Feature(
            ee.Geometry.Point([-88.72590113877084, 35.601989406115614]),
            {
              "LC": 1,
              "system:index": "16"
            }),
        ee.Feature(
            ee.Geometry.Point([-88.77490870165946, 36.11009435295949]),
            {
              "LC": 1,
              "system:index": "17"
            }),
        ee.Feature(
            ee.Geometry.Point([-88.9873488707638, 36.161935387195875]),
            {
              "LC": 1,
              "system:index": "18"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.12299577997898, 36.25813745850283]),
            {
              "LC": 1,
              "system:index": "19"
            }),
        ee.Feature(
            ee.Geometry.Point([-88.96090010054154, 36.19987172486153]),
            {
              "LC": 1,
              "system:index": "20"
            }),
        ee.Feature(
            ee.Geometry.Point([-88.9435156596092, 36.19442097074415]),
            {
              "LC": 1,
              "system:index": "21"
            }),
        ee.Feature(
            ee.Geometry.Point([-88.93269049989028, 36.20725993703776]),
            {
              "LC": 1,
              "system:index": "22"
            }),
        ee.Feature(
            ee.Geometry.Point([-88.93266212377455, 36.77440663346662]),
            {
              "LC": 1,
              "system:index": "23"
            }),
        ee.Feature(
            ee.Geometry.Point([-88.85852761929496, 36.73460314872616]),
            {
              "LC": 1,
              "system:index": "24"
            })]),
    vegetation = /* color: #228b1e */ee.FeatureCollection(
        [ee.Feature(
            ee.Geometry.Point([-89.00972387654174, 34.99624601121268]),
            {
              "LC": 2,
              "system:index": "0"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.02030120781365, 34.99166459535381]),
            {
              "LC": 2,
              "system:index": "1"
            }),
        ee.Feature(
            ee.Geometry.Point([-88.95126973444123, 34.978095614654904]),
            {
              "LC": 2,
              "system:index": "2"
            }),
        ee.Feature(
            ee.Geometry.Point([-88.75932997809355, 34.97593145276492]),
            {
              "LC": 2,
              "system:index": "3"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.05920462819516, 34.87317526710538]),
            {
              "LC": 2,
              "system:index": "4"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.09721168373186, 34.876475240764556]),
            {
              "LC": 2,
              "system:index": "5"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.13304777346328, 34.87835308142907]),
            {
              "LC": 2,
              "system:index": "6"
            }),
        ee.Feature(
            ee.Geometry.Point([-88.74076386349184, 34.86535119064159]),
            {
              "LC": 2,
              "system:index": "7"
            }),
        ee.Feature(
            ee.Geometry.Point([-88.6942920695147, 34.86336149181434]),
            {
              "LC": 2,
              "system:index": "8"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.53991702659775, 35.479212337751214]),
            {
              "LC": 2,
              "system:index": "9"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.82465295693675, 35.44904387104237]),
            {
              "LC": 2,
              "system:index": "10"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.58449857558239, 35.09529727355934]),
            {
              "LC": 2,
              "system:index": "11"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.52239979733876, 35.085921631725064]),
            {
              "LC": 2,
              "system:index": "12"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.54037037429323, 35.10504593816796]),
            {
              "LC": 2,
              "system:index": "13"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.50518278100186, 35.126754727816426]),
            {
              "LC": 2,
              "system:index": "14"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.3829101912007, 35.153035290963494]),
            {
              "LC": 2,
              "system:index": "15"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.38770690158888, 35.28401335739074]),
            {
              "LC": 2,
              "system:index": "16"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.30334116564514, 35.33411376088241]),
            {
              "LC": 2,
              "system:index": "17"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.1992577599112, 35.39379891257597]),
            {
              "LC": 2,
              "system:index": "18"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.33871917771734, 35.57786641990827]),
            {
              "LC": 2,
              "system:index": "19"
            }),
        ee.Feature(
            ee.Geometry.Point([-88.68642305486082, 35.83061874446985]),
            {
              "LC": 2,
              "system:index": "20"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.28156972169462, 35.152688933593474]),
            {
              "LC": 2,
              "system:index": "21"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.29251464699644, 35.13583223935476]),
            {
              "LC": 2,
              "system:index": "22"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.10420211647886, 35.15674661163777]),
            {
              "LC": 2,
              "system:index": "23"
            }),
        ee.Feature(
            ee.Geometry.Point([-89.02023701673915, 35.16316081861669]),
            {
              "LC": 2,
              "system:index": "24"
            })]);


//////////////////////////////////////////////////////////////////////////

 // Create the HUC geometry (study area)
var geometry = ee.FeatureCollection('USGS/WBD/2017/HUC04')
    .filter("huc4 == '0801'"); // 08010210 (Wolf)


// Function for clipping image
var clipcol = function(image) {
  var clipimage = image.clip(geometry)
  return clipimage
}

// Import of NAIP-Orthofoto
var dataset = ee.ImageCollection('USDA/NAIP/DOQQ')
                  .filter(ee.Filter.date('2018-01-01', '2018-12-31'))
                  .filterBounds(geometry)
                  .map(clipcol)

// 2nd image              
var listOfImages = dataset.toList(dataset.size());
var orthofoto = ee.Image(listOfImages.get(1))

// True Color Visualisation
var trueColor = dataset.select(['R', 'G', 'B']);
var trueColorVis = {
  min: 0.0,
  max: 255.0,
};
Map.addLayer(trueColor, trueColorVis, 'Orthofoto');

// Band selection and merge of the 3 FeatureCollection
var bands = ['R', 'G', 'B', 'N'];
var merged_collection = water.merge(riparian).merge(vegetation)

// Sample Regions 
var training = orthofoto.sampleRegions({
  collection: merged_collection,
  properties: ['landcover'],
  scale: 1
})

var classifier = ee.Classifier.smileRandomForest(10).train({
  features: training,
  classProperty: 'landcover',
  inputProperties: bands
})

var classified = dataset.select(bands).classify(classifier)
Map.addLayer(classified, {min:0, max:2, palette: ['green','red','blue']},
"classified image")

(Please scroll past all the point geometries, my code is at the bottom of the code block) This is what I have in my code. The error I'm getting is "Line 75: dataset.select(...).classify is not a function" which corresponds to this line;

var classifier = ee.Classifier.smileRandomForest(10).train({
  features: training,
  classProperty: 'landcover',
  inputProperties: bands
})
3
  • 1
    water, riparian and vegetation objects are not available. Please share them as public assets or include its definition in the script. Jun 20, 2022 at 17:41
  • Thank you, those are just training sample points I have created. I'm happy to share, but not sure how I can embed them to the script, or share them individually. I'll update as soon as I figure it out.
    – puredata
    Jun 20, 2022 at 17:59
  • I think I was able to add them to my script. Please let me know if it doesn't work.
    – puredata
    Jun 20, 2022 at 18:51

1 Answer 1

3

The problem is that you are trying to apply certain ee.Image methods to ee.ImageCollection objects, so the error message is telling you exactly that (... is not a function). The first step to fix the script should be mosaicking or reducing the image collection to obtain an ee.Image object (here I used a median reducer). After doing this, you can create the training dataset according to this image, using the merged_collection ee.FeatureCollection. The property in your FeatureCollection is named 'LC', not 'landcover', so also I added that change to your script. Finally, you just need to make the predictions on the complete orthofoto image to obtain your classification. Here are the only changes I made to your script:

var orthofoto = dataset.median();

var training = orthofoto.sampleRegions({
  collection: merged_collection,
  properties: ['LC'],
  scale: 1
})

var classified = orthofoto.classify(classifier)
3
  • Thanks a lot, may I ask you how were you able to check the property? (landcover vs LC). When I made the changes, I ended up getting the error "Cannot read properties of null (reading 'wpa')" . Would you have any suggestions to investigate this error?
    – puredata
    Jun 20, 2022 at 21:28
  • 1
    In the script defining the training points location (water object) you can see that each feature has only two properties: 'LC' and 'system:index'. Since the latter is like an ID for each feature, I assumed 'LC' was the property for landcover. Regarding the error you mention, I would check if the object, from which certain properties are being read, exists or is not empty. Jun 20, 2022 at 22:12
  • 1
    Thanks a lot for your time and help Jonathan, much appreciated!
    – puredata
    Jun 20, 2022 at 22:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.