shapefiles: convert Point (MultiPoint) to Point (PointZ) geometry in R

1.3k views Asked by At

I have a shapefile (mult_point_example.shp) in a multipoint geometry:

# Packages
require(sf)

# get AOI
download.file(
  "https://github.com/Leprechault/trash/raw/main/mult_point_example.zip",
  zip_path <- tempfile(fileext = ".zip")
)
unzip(zip_path, exdir = tempdir())

# Open the files
setwd(tempdir())
my_multi_points <- sf::st_read("mult_point_example.shp") 

#Reading layer `mult_point_example' from data source `C:\Users\fores\AppData\Local\Temp\RtmpmQybFP\mult_point_example.shp' using driver `ESRI Shapefile'
#Simple feature collection with 8 features and 10 fields
#Geometry type: MULTIPOINT
#Dimension:     XYZ
#Bounding box:  xmin: -52.73354 ymin: -19.79479 xmax: -52.72586 ymax: -19.79067
#z_range:       zmin: 0 zmax: 0
#Geodetic CRS:  WGS 84

But, I'd like to convert the multipoint object to point geometry. Please I need some help to convert each point in (my_multi_points) to individuals features.

1

There are 1 answers

1
dieghernan On BEST ANSWER

Use st_cast(., "POINT"). However, note that when you cast to POINT, the features are repeated, i.e. in the case of one MULTIPOINT (one row with several features ) containing 20 points, when you cast to POINT you get 20 rows POINT with the same features than the original MULTIPOINT:

# Packages
require(sf)
#> Loading required package: sf
#> Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1

# get AOI
download.file(
  "https://github.com/Leprechault/trash/raw/main/mult_point_example.zip",
  zip_path <- tempfile(fileext = ".zip")
)
unzip(zip_path, exdir = tempdir())

# Open the files
setwd(tempdir())
my_multi_points <- sf::st_read("mult_point_example.shp", quiet = TRUE) 

my_multi_points
#> Simple feature collection with 8 features and 10 fields
#> Geometry type: MULTIPOINT
#> Dimension:     XYZ
#> Bounding box:  xmin: -52.73354 ymin: -19.79479 xmax: -52.72586 ymax: -19.79067
#> z_range:       zmin: 0 zmax: 0
#> Geodetic CRS:  WGS 84
#>   Talhao               Uso Especies  Yoe       DATA     EventoClas   POINT_X
#> 1    142 Plantio_Comercial     EUUR 2012 2017-06-15        Pequeno -52.72675
#> 2    142 Plantio_Comercial     EUUR 2012 2017-06-09        Pequeno -52.72843
#> 3    142 Plantio_Comercial     EUUR 2012 2017-06-12        Pequeno -52.73070
#> 4    142 Plantio_Comercial     EUUR 2012 2017-06-09 Meio_Quadrante -52.72847
#> 5    142 Plantio_Comercial     EUUR 2012 2017-06-12 Meio_Quadrante -52.73066
#> 6    142 Plantio_Comercial     EUUR 2012 2017-06-09   Um_Quadrante -52.72868
#> 7    142 Plantio_Comercial     EUUR 2012 2017-06-12   Um_Quadrante -52.73065
#> 8    142 Plantio_Comercial     EUUR 2012 2017-06-12      Carreiros -52.73217
#>     POINT_Y Fazenda TS_m2                       geometry
#> 1 -19.79296    Lobo     1 MULTIPOINT Z ((-52.72692 -1...
#> 2 -19.79197    Lobo     1 MULTIPOINT Z ((-52.731 -19....
#> 3 -19.79308    Lobo     1 MULTIPOINT Z ((-52.73354 -1...
#> 4 -19.79192    Lobo     6 MULTIPOINT Z ((-52.73086 -1...
#> 5 -19.79317    Lobo     6 MULTIPOINT Z ((-52.73345 -1...
#> 6 -19.79181    Lobo    10 MULTIPOINT Z ((-52.73075 -1...
#> 7 -19.79280    Lobo    10 MULTIPOINT Z ((-52.73343 -1...
#> 8 -19.79275    Lobo     1 MULTIPOINT Z ((-52.73217 -1...

# SOLUTION: Cast to points
 
my_points <- st_cast(my_multi_points, "POINT")
#> Warning in st_cast.sf(my_multi_points, "POINT"): repeating attributes for all
#> sub-geometries for which they may not be constant

my_points
#> Simple feature collection with 416 features and 10 fields
#> Geometry type: POINT
#> Dimension:     XYZ
#> Bounding box:  xmin: -52.73354 ymin: -19.79479 xmax: -52.72586 ymax: -19.79067
#> Geodetic CRS:  WGS 84
#> First 10 features:
#>     Talhao               Uso Especies  Yoe       DATA EventoClas   POINT_X
#> 1      142 Plantio_Comercial     EUUR 2012 2017-06-15    Pequeno -52.72675
#> 1.1    142 Plantio_Comercial     EUUR 2012 2017-06-15    Pequeno -52.72675
#> 1.2    142 Plantio_Comercial     EUUR 2012 2017-06-15    Pequeno -52.72675
#> 2      142 Plantio_Comercial     EUUR 2012 2017-06-09    Pequeno -52.72843
#> 2.1    142 Plantio_Comercial     EUUR 2012 2017-06-09    Pequeno -52.72843
#> 2.2    142 Plantio_Comercial     EUUR 2012 2017-06-09    Pequeno -52.72843
#> 2.3    142 Plantio_Comercial     EUUR 2012 2017-06-09    Pequeno -52.72843
#> 2.4    142 Plantio_Comercial     EUUR 2012 2017-06-09    Pequeno -52.72843
#> 2.5    142 Plantio_Comercial     EUUR 2012 2017-06-09    Pequeno -52.72843
#> 2.6    142 Plantio_Comercial     EUUR 2012 2017-06-09    Pequeno -52.72843
#>       POINT_Y Fazenda TS_m2                       geometry
#> 1   -19.79296    Lobo     1 POINT Z (-52.72692 -19.7930...
#> 1.1 -19.79296    Lobo     1 POINT Z (-52.72664 -19.7929...
#> 1.2 -19.79296    Lobo     1 POINT Z (-52.72657 -19.7928...
#> 2   -19.79197    Lobo     1   POINT Z (-52.731 -19.7914 0)
#> 2.1 -19.79197    Lobo     1 POINT Z (-52.73098 -19.7912...
#> 2.2 -19.79197    Lobo     1 POINT Z (-52.73075 -19.7914...
#> 2.3 -19.79197    Lobo     1 POINT Z (-52.73059 -19.7913...
#> 2.4 -19.79197    Lobo     1 POINT Z (-52.73052 -19.7920...
#> 2.5 -19.79197    Lobo     1 POINT Z (-52.73047 -19.7909 0)
#> 2.6 -19.79197    Lobo     1 POINT Z (-52.73046 -19.7909...

Created on 2021-05-26 by the reprex package (v2.0.0)