将点连接在一起以创建单线和映射输出

时间:2019-08-22 10:51:06

标签: r sf tmap

我有以下数据:

dt1 <- data.table(
  code=c("A00111", "A00112","A00113","A00211","A00212","A00213","A00214","A00311","A00312","A00472"),
  x=c(325147,323095,596020,257409,241206,248371,261076,595218,596678,597678),
  y=c(286151,284740,335814,079727,084266,078283,062045,333889,337836,339836),
  point_id=c("P01","P02","P03","P04","P05","P06","P07","P08","P09","P10"))

sf1 <- st_as_sf(dt1, coords = c("x","y"), crs=27700, na.fail=FALSE)

我想创建一条基于point_id将点连接在一起的直线(因此,将P01到P02到P03等)作为新的sf对象。然后,我想通过将tmap附加到类似以下内容的方式来可视化此行:

tmap_mode("view")
map <- tm_shape(sf1) + tm_dots()
map

2 个答案:

答案 0 :(得分:1)

如果总结<?php function array_left_right (&$array, &$counter = 1) { if (is_array($array)) { $array['left'] = $counter++; foreach($array as $array_key=>&$array_item) { array_left_right($array_item, $counter); } $array['right'] = $counter++; } } $fred = array(array(array('a', 'b'), array('c', 'd'), array('e', 'f'), array('g', 'h')),array(array('aa', 'ab'), array('ac', 'ad'), array('ae', 'af'), array('ag', 'ah'))); array_left_right($fred); print_r($fred); ,则会得到sf1对象。 MULTIPOINT告诉R不要“联合”而是“组合”它们,这保留了点的顺序。因此,您需要按ID预先订购。

最后,您将do_union = F投射到MULTIPOINT

LINESTRING

答案 1 :(得分:1)

我一直在开发sfheaders库来帮助解决这类问题

## not yet on cran, so install from github
devtools::install_github("dcooley/sfheaders")

library(sfheaders)
library(sf)

## given a data.frame, call sf_linestring() and it will return an sf object of LINESTRINGS
sf <- sf_linestring(
  obj = dt1
  , x = "x"
  , y = "y"
)
sf::st_crs( sf ) <- 27700 

sf
# Simple feature collection with 1 feature and 1 field
# geometry type:  LINESTRING
# dimension:      XY
# bbox:           xmin: 241206 ymin: 62045 xmax: 597678 ymax: 339836
# epsg (SRID):    27700
# proj4string:    +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +towgs84=446.448,-125.157,542.06,0.15,0.247,0.842,-20.489 +units=m +no_defs
# id                       geometry
# 1  1 LINESTRING (325147 286151, ...
相关问题