Как получить доступ к многомерному элементу списка с помощью цикла FOR в R - PullRequest
0 голосов
/ 28 августа 2018

Я хочу получить доступ к переменной 'bandSpecificMatadata' из многомерного списка в R и создать вектор 'refleanceCoefficient' для моего проекта дистанционного зондирования.

Сначала я смог уменьшить размерность списка, а затем использовал nodes <- get('EarthObservationResult', matadata.list$resultOf) для уточнения списка.

Тогда возникает проблема, когда я пытаюсь создать что-то вроде (bandNumber1 соответствует коэффициенту отражения 2.21e-5), используя цикл FOR.

for(node in nodes[6:9]) {
    bn = get("bandNumber", node)
    if(bn %in% c('1','2','3','4')){
             i = integer(bn)
             coeffs = get("reflectanceCoefficient", node)
             }
       print(coeffs)
    }

который печатает:

[1] "2.21386105481e-05"
[1] "2.31474175457e-05"
[1] "2.60208594123e-05"
[1] "3.83481925626e-05"

Но я хочу вектор с 1, 2, 3, 4 с соответствующими числами. Мне кажется, что номер перезаписывает последний при каждой печати.

Тогда я попробовал:

for(node in nodes[6:9]) {
  n = 1:4
  b[n] = get("bandNumber", node)
  if(b[n] %in% c('1','2','3','4')){
    i = integer(b[n])
    coeffs[i] = get("reflectanceCoefficient", node)
    }

  print(coeffs)
  }

Но получается

Error in integer(b[n]) : invalid 'length' argument
In addition: Warning message:
In if (b[n] %in% c("1", "2", "3", "4")) { :
  the condition has length > 1 and only the first element will be used

Как мне это исправить?

Я использовал XML::xmlParse() для анализа xml и matadata.list <- XML::xmlToList() для преобразования данных в список.

Воспроизводимый пример см. Ниже:

dput(matadata.list) structure(list(metaDataProperty = structure(list(EarthObservationMetaData = structure(list( identifier = "20170127_213132_0e0e_3B_AnalyticMS", acquisitionType = "NOMINAL", productType = "L3B", status = "ARCHIVED", downlinkedTo = structure(list( DownlinkInformation = structure(list(acquisitionStation = structure(list( text = "Planet Ground Station Network", .attrs = structure("urn:eop:PS:stationLocation", .Names = "codeSpace")), .Names = c("text", ".attrs")), acquisitionDate = "2017-01-27T21:31:32+00:00"), .Names = c("acquisitionStation", "acquisitionDate"))), .Names = "DownlinkInformation"), archivedIn = structure(list(ArchivingInformation = structure(list( archivingCenter = structure(list(text = "Planet Archive Center", .attrs = structure("urn:eop:PS:stationLocation", .Names = "codeSpace")), .Names = c("text", ".attrs")), archivingDate = "2017-01-27T21:31:32+00:00", archivingIdentifier = structure(list(text = "385180", .attrs = structure("urn:eop:PS:dmsCatalogueId", .Names = "codeSpace")), .Names = c("text", ".attrs"))), .Names = c("archivingCenter", "archivingDate", "archivingIdentifier"))), .Names = "ArchivingInformation"), processing = structure(list(ProcessingInformation = structure(list( processorName = "CMO Processor", processorVersion = "4.1.4", nativeProductFormat = "GeoTIFF"), .Names = c("processorName", "processorVersion", "nativeProductFormat"))), .Names = "ProcessingInformation"), license = structure(list(licenseType = "20160101 - Inc - Single User", resourceLink = structure(c("PL EULA", "https://assets.planet.com/docs/20160101_Inc_SingleUser.txt" ), class = structure("XMLAttributes", package = "XML"), namespaces = structure(c("xlink", "xlink"), .Names = c("http://www.w3.org/1999/xlink", "http://www.w3.org/1999/xlink")), .Names = c("title", "href"))), .Names = c("licenseType", "resourceLink")), versionIsd = "1.0", pixelFormat = "16U"), .Names = c("identifier", "acquisitionType", "productType", "status", "downlinkedTo", "archivedIn", "processing", "license", "versionIsd", "pixelFormat"))), .Names = "EarthObservationMetaData"), validTime = structure(list(TimePeriod = structure(list(beginPosition = "2017-01-27T21:31:32+00:00", endPosition = "2017-01-27T21:31:32+00:00"), .Names = c("beginPosition", "endPosition"))), .Names = "TimePeriod"), using = structure(list( EarthObservationEquipment = structure(list(platform = structure(list( Platform = structure(list(shortName = "PlanetScope", serialIdentifier = "0e0e", orbitType = "LEO-SSO"), .Names = c("shortName", "serialIdentifier", "orbitType"))), .Names = "Platform"), instrument = structure(list(Instrument = structure(list( shortName = "PS2"), .Names = "shortName")), .Names = "Instrument"), sensor = structure(list(Sensor = structure(list(sensorType = "OPTICAL", resolution = structure(list(text = "3.0000", .attrs = structure("m", .Names = "uom")), .Names = c("text", ".attrs")), scanType = "FRAME"), .Names = c("sensorType", "resolution", "scanType"))), .Names = "Sensor"), acquisitionParameters = structure(list(Acquisition = structure(list( orbitDirection = "DESCENDING", incidenceAngle = structure(list( text = "8.072969e-02", .attrs = structure("deg", .Names = "uom")), .Names = c("text", ".attrs")), illuminationAzimuthAngle = structure(list( text = "7.610387e+01", .attrs = structure("deg", .Names = "uom")), .Names = c("text", ".attrs")), illuminationElevationAngle = structure(list( text = "4.649194e+01", .attrs = structure("deg", .Names = "uom")), .Names = c("text", ".attrs")), azimuthAngle = structure(list(text = "1.242074e+01", .attrs = structure("deg", .Names = "uom")), .Names = c("text", ".attrs")), spaceCraftViewAngle = structure(list( text = "5.692807e-02", .attrs = structure("deg", .Names = "uom")), .Names = c("text", ".attrs")), acquisitionDateTime = "2017-01-27T21:31:32+00:00"), .Names = c("orbitDirection", "incidenceAngle", "illuminationAzimuthAngle", "illuminationElevationAngle", "azimuthAngle", "spaceCraftViewAngle", "acquisitionDateTime" ))), .Names = "Acquisition")), .Names = c("platform", "instrument", "sensor", "acquisitionParameters"))), .Names = "EarthObservationEquipment"), target = structure(list(Footprint = structure(list(multiExtentOf = structure(list( MultiSurface = structure(list(surfaceMembers = structure(list( Polygon = structure(list(outerBoundaryIs = structure(list( LinearRing = structure(list(coordinates = "175.446585079397,-37.7068873856657 175.446633607572,-37.7045627724835 175.46731776545,-37.6311749428137 175.468010520596,-37.6311839417076 175.75989021492,-37.6819836599337 175.759889856814,-37.6820051679817 175.739424097003,-37.757826933992 175.739359440859,-37.7578262423109 175.446585079397,-37.7068873856657"), .Names = "coordinates")), .Names = "LinearRing"), .attrs = structure("EPSG:4326", .Names = "srsName")), .Names = c("outerBoundaryIs", ".attrs"))), .Names = "Polygon"), .attrs = structure("EPSG:4326", .Names = "srsName")), .Names = c("surfaceMembers", ".attrs"))), .Names = "MultiSurface"), centerOf = structure(list( Point = structure(list(pos = "175.603162359 -37.6944367036", .attrs = structure("EPSG:4326", .Names = "srsName")), .Names = c("pos", ".attrs"))), .Names = "Point"), geographicLocation = structure(list( topLeft = structure(list(latitude = "-37.6311749428", longitude = "175.446585079"), .Names = c("latitude", "longitude")), topRight = structure(list(latitude = "-37.6311749428", longitude = "175.759890215"), .Names = c("latitude", "longitude")), bottomRight = structure(list(latitude = "-37.757826934", longitude = "175.759890215"), .Names = c("latitude", "longitude")), bottomLeft = structure(list(latitude = "-37.757826934", longitude = "175.446585079"), .Names = c("latitude", "longitude"))), .Names = c("topLeft", "topRight", "bottomRight", "bottomLeft"))), .Names = c("multiExtentOf", "centerOf", "geographicLocation"))), .Names = "Footprint"), resultOf = structure(list( EarthObservationResult = structure(list(product = structure(list( ProductInformation = structure(list(fileName = "20170127_213132_0e0e_3B_AnalyticMS.tif", productFormat = "GeoTIFF", spatialReferenceSystem = structure(list( epsgCode = "32760", geodeticDatum = "WGS_1984", projection = "WGS 84 / UTM zone 60S", projectionZone = "160"), .Names = c("epsgCode", "geodeticDatum", "projection", "projectionZone" )), resamplingKernel = "CC", numRows = "4565", numColumns = "9194", numBands = "4", rowGsd = "3.0", columnGsd = "3.0", radiometricCorrectionApplied = "true", geoCorrectionLevel = "Precision Geocorrection", elevationCorrectionApplied = "FineDEM", atmosphericCorrectionApplied = "false"), .Names = c("fileName", "productFormat", "spatialReferenceSystem", "resamplingKernel", "numRows", "numColumns", "numBands", "rowGsd", "columnGsd", "radiometricCorrectionApplied", "geoCorrectionLevel", "elevationCorrectionApplied", "atmosphericCorrectionApplied" ))), .Names = "ProductInformation"), mask = structure(list( MaskInformation = structure(list(type = "UNUSABLE DATA", format = "RASTER", referenceSystemIdentifier = structure(list( text = "32760", .attrs = structure("EPSG", .Names = "codeSpace")), .Names = c("text", ".attrs")), fileName = "20170127_213132_0e0e_3B_AnalyticMS_DN_udm.tif"), .Names = c("type", "format", "referenceSystemIdentifier", "fileName" ))), .Names = "MaskInformation"), cloudCoverPercentage = structure(list( text = "0.01", .attrs = structure("percentage", .Names = "uom")), .Names = c("text", ".attrs")), cloudCoverPercentageQuotationMode = "AUTOMATIC", unusableDataPercentage = structure(list(text = "0.0", .attrs = structure("percentage", .Names = "uom")), .Names = c("text", ".attrs")), bandSpecificMetadata = structure(list( bandNumber = "1", comment = NULL, radiometricScaleFactor = "0.01", comment = NULL, reflectanceCoefficient = "2.21386105481e-05"), .Names = c("bandNumber", "comment", "radiometricScaleFactor", "comment", "reflectanceCoefficient" )), bandSpecificMetadata = structure(list(bandNumber = "2", comment = NULL, radiometricScaleFactor = "0.01", comment = NULL, reflectanceCoefficient = "2.31474175457e-05"), .Names = c("bandNumber", "comment", "radiometricScaleFactor", "comment", "reflectanceCoefficient" )), bandSpecificMetadata = structure(list(bandNumber = "3", comment = NULL, radiometricScaleFactor = "0.01", comment = NULL, reflectanceCoefficient = "2.60208594123e-05"), .Names = c("bandNumber", "comment", "radiometricScaleFactor", "comment", "reflectanceCoefficient" )), bandSpecificMetadata = structure(list(bandNumber = "4", comment = NULL, radiometricScaleFactor = "0.01", comment = NULL, reflectanceCoefficient = "3.83481925626e-05"), .Names = c("bandNumber", "comment", "radiometricScaleFactor", "comment", "reflectanceCoefficient" ))), .Names = c("product", "mask", "cloudCoverPercentage", "cloudCoverPercentageQuotationMode", "unusableDataPercentage", "bandSpecificMetadata", "bandSpecificMetadata", "bandSpecificMetadata", "bandSpecificMetadata"))), .Names = "EarthObservationResult"), .attrs = structure(c("http://schemas.planet.com/ps/v1/planet_product_metadata_geocorrected_level http://schemas.planet.com/ps/v1/planet_product_metadata_geocorrected_level.xsd", "1.2.1", "1.0"), class = structure("XMLAttributes", package = "XML"), namespaces = structure(c("xsi", "", ""), .Names = c("http://www.w3.org/2001/XMLSchema-instance", "", "")), .Names = c("schemaLocation", "version", "planet_standard_product_version" ))), .Names = c("metaDataProperty", "validTime", "using", "target", "resultOf", ".attrs"))

Ответы [ 2 ]

0 голосов
/ 28 августа 2018

(отредактировано для ответа на обновленный вопрос)
Посмотрите на эти ответы для работы с исходными данными XML: Как проанализировать фрейм данных XML в R

Вы уже проанализировали XML-файл, и теперь у вас есть списки. Я думаю, что пакет purrr (https://purrr.tidyverse.org/) очень помогает в этом случае.

Я предполагаю, что мы знаем путь к EarthObservationResult. Обратите внимание, как мы извлекаем reflectanceCoefficient из всех подузлов и отбрасываем элементы NULL с помощью compact.

library(tidyverse)

nodes <- matadata.list$resultOf$EarthObservationResult

coefff <- nodes %>%
    purrr::map("reflectanceCoefficient") %>%
    purrr::compact() %>%
    purrr::map_dbl(~ as.numeric(.x)) %>%
    purrr::set_names(nm = NULL)

print(coeffs)
#> [1] 2.213861e-05 2.314742e-05 2.602086e-05 3.834819e-05

Создано в 2018-08-28 пакетом Представить (v0.2.0).

0 голосов
/ 28 августа 2018

Поскольку вы не предоставили никаких воспроизводимых данных, следующая попытка может не сработать:

# Initialise vectors:
b <- vector(mode = "character", length = 4)
coeffs <- vector(mode = "character", length = 4)

# Get coefficients
for(i in 6:9) {
     b[i] = get("bandNumber", nodes[[i]])
     coeffs[i] <- ifelse(b[i] %in% 6:9), 
                         get("reflectanceCoefficient", nodes[[i]]),  # Yes cond val
                         NA)                                         # No cond val
     }
coeffs
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...