使用SF / MapView连接两组坐标以创建线

时间:2019-03-15 16:37:04

标签: r sf r-mapview

我有一个数据集,其中在一个位置(Blong,Blat)捕获的鸟又在另一个位置(Elong,Elat)再次遇到了一只鸟。这些坐标是经/纬度格式,我想用一条线连接捕获和遇到的位置,并在mapview中绘制它们。

在下面的数据中,每一行都是一只单独的鸟,它具有捕捉/遇到的坐标以及它所属的飞行路线(我想用它来为mapview中的线条着色。

apply plugin: 'com.android.application'

def gitCommits = 'git rev-list --all --count'.execute([], rootDir).text.trim().toInteger()

def timestamp = new Date().getTime()

android {
    compileSdkVersion 28
    buildToolsVersion '28.0.3'
    defaultConfig {
        applicationId "org.app.appname"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode gitCommits
        versionName "5.0"

        resConfigs "en", "ru", "tr", "uk"
        buildConfigField "boolean", "SELFUPDATE_ENABLED", "false"
        buildConfigField "String", "SELFUPDATE_URL", "\"http://randomwebsite.link/v.0.3/get/openmanga/version\""
        buildConfigField "String", "SYNC_URL", "\"http://random.random.com/api/v1\""
        buildConfigField "long", "TIMESTAMP", "${timestamp}L"
    }

    signingConfigs {
        debug {
            storeFile file("debug.jks")
            storePassword "develop"
            keyAlias "develop"
            keyPassword "develop"
        }
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            setProperty("archivesBaseName", "OpenManga-v${defaultConfig.versionName}")
            resValue "string", "app_name", "OpenManga"
        }

        debug {
            zipAlignEnabled true
            applicationIdSuffix ".debug"
            versionNameSuffix="a"
            signingConfig signingConfigs.debug
            resValue "string", "app_name", "OpenManga Debug"
        }

        fdroid {
            initWith release
            buildConfigField "boolean", "SELFUPDATE_ENABLED", "false"
            versionNameSuffix="-fdroid"
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    lintOptions {
        disable 'MissingTranslation'
        abortOnError false
    }
}

ext {
    supportLib = '28.0.0'
}

dependencies {
    implementation "com.android.support:design:28.0.0"
    implementation "com.android.support:support-v13:28.0.0"
    implementation "com.android.support:recyclerview-v7:28.0.0"
    implementation "com.android.support:cardview-v7:28.0.0"
    implementation "com.android.support:exifinterface:28.0.0"

    implementation 'com.squareup.okhttp3:okhttp:3.11.0'
    implementation 'com.squareup.duktape:duktape-android:1.3.0'
    implementation 'info.guardianproject.netcipher:netcipher:2.0.0-beta1'
    implementation 'info.guardianproject.netcipher:netcipher-okhttp3:2.0.0-alpha1'
    implementation 'org.jsoup:jsoup:1.11.3'
    implementation 'com.davemorrissey.labs:subsampling-scale-image-view:3.10.0'
    implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
}

看一下数据:

dat <- structure(list(Blong = c(-75.58333, -76.08333, -81.08333, -94.25, 
    -75.41667, -99.41667, -77.41667, -116.08333, -89.58333, -77.58333
    ), Blat = c(37.58333, 40.58333, 42.75, 41.91667, 38.25, 28.25, 
    38.91667, 43.58333, 44.25, 38.91667), Elong = c(-65.91667, -75.75, 
    -80.58333, -95.41667, -73.58333, -89.41667, -77.58333, -116.41667, 
    -96.41667, -77.41667), Elat = c(45.91667, 40.58333, 42.75, 29.75, 
    45.58333, 48.25, 38.75, 43.58333, 34.08333, 38.91667), Flyway = structure(c(2L, 
    2L, 2L, 1L, 2L, 2L, 2L, 3L, 2L, 2L), .Label = c("Central", "Eastern", 
    "West"), class = "factor")), .Names = c("Blong", "Blat", "Elong", 
    "Elat", "Flyway"), row.names = c(NA, -10L), class = c("tbl_df", 
    "tbl", "data.frame"))

我看了一些例子,但还没有找到一个看起来像我的数据集的例子。

1 个答案:

答案 0 :(得分:2)

棘手的事情是从坐标对中以宽格式创建有效的LINESTRING对象。 sf期望矩阵行中的线串坐标。这是一种可行的方法。 sfc对象的sf列是一个列表,因此在这里我们使用lapply遍历您提供的数据行。

library(sf)
library(mapview)

b = dat[, c("Blong", "Blat")]
names(b) = c("long", "lat")
e = dat[, c("Elong", "Elat")]
names(e) = c("long", "lat")

dat$geometry = do.call(
  "c", 
  lapply(seq(nrow(b)), function(i) {
    st_sfc(
      st_linestring(
        as.matrix(
          rbind(b[i, ], e[i, ])
        )
      ),
      crs = 4326
    )
  }))

dat_sf = st_as_sf(dat)

mapview(dat_sf, zcol = "Flyway")