st_centroid()不在Lambert-93 shapefile上的多边形内

时间:2019-02-16 14:05:27

标签: r sf

我有这个问题。我有一个来自the IGN institute的shapefile。我想根据特征来聚合shapefile,然后绘制出所得多边形的质心。

library(sf)
library(dplyr)
# load a shapefile and plot
sh = st_read("~/Downloads/CONTOURS-IRIS_2-1__SHP__FRA_2018-06-08/CONTOURS-IRIS/1_DONNEES_LIVRAISON_2018-06-00105/CONTOURS-IRIS_2-1_SHP_LAMB93_FXX-2017/CONTOURS-IRIS.shp",stringsAsFactors=FALSE)

# paris
p = sh %>%
  mutate(INSEE_N = as.integer(INSEE_COM)-75100) %>%
  filter((INSEE_N < 21) & (INSEE_N > 0))

# aggregate and plot centroid
p_arr = p %>%
  group_by(INSEE_COM) %>%
  summarise(n = n()) %>%
  select(n)

plot(p_arr)
plot(st_geometry(st_centroid(p_arr)), pch = 3, col = 'red', add = TRUE)

enter image description here

我很惊讶那些质心没有包含在它们各自的多边形中。如您所见,我还没有接触过CRS规范,我只是阅读了shapefile。我在这里做错了什么?

修改

顺便说一句,在WGS84中,我根本看不到质心:

p_wgs = st_transform(p,4326)
p_arr_wgs = p_wgs %>%
  group_by(INSEE_COM) %>%
  summarise(n = n()) %>%
  select(n)

plot(p_arr_wgs,axes=TRUE)
plot(st_geometry(st_centroid(p_arr_wgs)), pch = 3, col = 'red', add = TRUE)
Warning messages:
1: In st_centroid.sf(p_arr_wgs) :
  st_centroid assumes attributes are constant over geometries of x
2: In st_centroid.sfc(st_geometry(x), of_largest_polygon = of_largest_polygon) :
  st_centroid does not give correct centroids for longitude/latitude data

enter image description here

我看到了警告。但是为什么不呢?数据看起来不太远?我希望至少看到某物

> st_geometry(st_centroid(p_arr_wgs))
Geometry set for 20 features 
geometry type:  POINT
dimension:      XY
bbox:           xmin: 2.261954 ymin: 48.82841 xmax: 2.421378 ymax: 48.89257
epsg (SRID):    4326
proj4string:    +proj=longlat +datum=WGS84 +no_defs
First 5 geometries:
POINT (2.336403 48.86258)
POINT (2.342896 48.86828)
POINT (2.360002 48.86287)
POINT (2.357606 48.85435)

2 个答案:

答案 0 :(得分:1)

不确定ypour代码有什么问题,但是下面的代码对我来说很好。

library(sf)
library(dplyr)
library(mapview)
paris <- st_read("./SO-answers/CONTOURS-IRIS_2-1__SHP__FRA_2018-06-08/CONTOURS-IRIS/1_DONNEES_LIVRAISON_2018-06-00105/CONTOURS-IRIS_2-1_SHP_LAMB93_FXX-2017/CONTOURS-IRIS.shp",
                 stringsAsFactors = FALSE)

p_arr <- paris %>%
  mutate(INSEE_N = as.integer(INSEE_COM)-75100) %>%
  filter((INSEE_N < 21) & (INSEE_N > 0)) %>%
  group_by(INSEE_COM) %>%
  summarise()

mapview( list( p_arr, st_centroid( p_arr ) ) )

enter image description here

答案 1 :(得分:0)

糟糕,请稍等。这行得通!

function curl($url, $post_array=false){ 
        $handle = curl_init();
        if (FALSE === $handle) 
            throw new Exception('failed to CURL initialize; '. __FILE__);
        curl_setopt($handle, CURLOPT_URL, $url);
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
        if($post_array) {
            curl_setopt($handle, CURLOPT_POST, 1 );
            curl_setopt($handle, CURLOPT_POSTFIELDS, http_build_query($post_array) );
        } 
        curl_setopt($handle,CURLOPT_HTTPHEADER,array('X-HTTP-Method-Override: GET'));
        $response = curl_exec($handle);
        return $response;
}


var_dump ( curl("https://www.googleapis.com/language/translate/v2", ['key'=>$key, 'q'=> array("hello", "world"), 'source'=>"en", 'target'=>'ru']   ) );

enter image description here

所以我只能在纯plot(st_geometry(p_arr)) plot(st_geometry(st_centroid(p_arr)), pch = 3, col = 'red', add = TRUE) 对象上执行此操作,即没有重叠功能吗?