我可以使用 const setHighlight = (selMod: string) => {
const newCars = cars.map(car => ({
...car,
highlight: car.model === selMod
}));
setCars(newCars);
};
但是,如何在sf R中将点转换为宽度x km和高度y km的矩形多边形?
答案 0 :(得分:1)
尝试此操作,尽管您必须小心投影的单位。由于您没有提供更多数据,因此我使用了一个自定义示例。基本上,答案是在您的点周围创建一个具有所需尺寸的bbox
:
library(sf)
#> Warning: package 'sf' was built under R version 3.6.3
#> Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
nc <- st_read(system.file("shape/nc.shp", package = "sf"))[1,]
#> Reading layer `nc' from data source `C:\Users\q31407\Documents\R\win-library\3.6\sf\shape\nc.shp' using driver `ESRI Shapefile'
#> Simple feature collection with 100 features and 14 fields
#> geometry type: MULTIPOLYGON
#> dimension: XY
#> bbox: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> epsg (SRID): 4267
#> proj4string: +proj=longlat +datum=NAD27 +no_defs
# Project to get coods in "m", example 3857, could be any
nc <- st_geometry(st_transform(nc,3857))
point <- st_centroid(nc)
point
#> Geometry set for 1 feature
#> geometry type: POINT
#> dimension: XY
#> bbox: xmin: -9072327 ymin: 4360181 xmax: -9072327 ymax: 4360181
#> epsg (SRID): 3857
#> proj4string: +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs
#> POINT (-9072327 4360181)
#Important: coordinates in m, adjust the units on function
# Helper funct
rect_around_point <- function(x,xsize,ysize){
bbox <- st_bbox(x)
bbox <- bbox +c(xsize/2,ysize/2,-xsize/2,-ysize/2)
return(st_as_sfc(bbox))
}
rect <- rect_around_point(point,10000,20000)
rect
#> Geometry set for 1 feature
#> geometry type: POLYGON
#> dimension: XY
#> bbox: xmin: -9077327 ymin: 4350181 xmax: -9067327 ymax: 4370181
#> epsg (SRID): 3857
#> proj4string: +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs
#> POLYGON ((-9067327 4370181, -9077327 4370181, -...
plot(rect, axes=TRUE)
plot(point, add=TRUE)
由reprex package(v0.3.0)于2020-03-20创建