Criar um convex hull para um objeto shapefile
Prezados Listeiros, Eu gostaria de criar um convex hull que envolvesse 4 polígonos, representados por um objeto oriundo da leitura de um readShapeLines(), sem ter que transformar o objeto em SpatialPolygonsDataFrame, isso é possível? Meu CRM: #Pacotes require(rgdal) require(maptools) #------------------------------------------------------------------------------- #Crio 4 polígonos sr <- SpatialPolygons(list( Polygons(list(Polygon(cbind(c(180114, 180553, 181127, 181477, 181294, 181007, 180409, 180162, 180114), c(332349, 332057, 332342, 333250, 333558, 333676, 332618, 332413, 332349)))),'1'), Polygons(list(Polygon(cbind(c(180042, 180545, 180553, 180314, 179955, 179142, 179437, 179524, 179979, 180042), c(332373, 332026, 331426, 330889, 330683, 331133, 331623, 332152, 332357, 332373)))),'2'), Polygons(list(Polygon(cbind(c(179110, 179907, 180433, 180712, 180752, 180329, 179875, 179668, 179572, 179269, 178879, 178600, 178544, 179046, 179110), c(331086, 330620, 330494, 330265, 330075, 330233, 330336, 330004, 329783, 329665, 329720, 329933, 330478, 331062, 331086)))),'3'), Polygons(list(Polygon(cbind(c(180304, 180403,179632,179420,180304), c(332791, 333204, 333635, 333058, 332791)))),'4'))) plot(sr) #Converto em polígono espacial srdf=SpatialPolygonsDataFrame(sr, data.frame(row.names=c('1','2','3','4'), PIDS=1:4)) srdf@data #Criação do shapefile para ilustrar o problema writeOGR(srdf, getwd(), 'POLY', 'ESRI Shapefile') #Leitura do shapefile contorno_line_X <- readShapeLines ("POLY.shp") #Plot plot(contorno_line_X) #Tentativa de criar o convex hull df.data = as.data.frame(contorno_line_X) ch <- chull(df.data) lines(ch, col="red") ##Não funciona Obrigado, -- ====================================================================== Alexandre dos Santos Proteção Florestal IFMT - Instituto Federal de Educação, Ciência e Tecnologia de Mato Grosso Campus Cáceres Caixa Postal 244 Avenida dos Ramires, s/n Bairro: Distrito Industrial Cáceres - MT CEP: 78.200-000 Fone: (+55) 65 8132-8112 (TIM) (+55) 65 9686-6970 (VIVO) e-mails:alexandresantosbr@yahoo.com.br alexandre.santos@cas.ifmt.edu.br Lattes: http://lattes.cnpq.br/1360403201088680 OrcID: orcid.org/0000-0001-8232-6722 Researchgate: https://www.researchgate.net/profile/Alexandre_Santos10 LinkedIn: https://br.linkedin.com/in/alexandre-dos-santos-87961635 ======================================================================
Alexandre, boa noite! Acredito que problema ocorra porque as.data.frame() não converte um objeto SpatialLines* do mesmo modo que um SpatialPoints* ou SpatialPolygons*. Uma opção é extrair e concatenar as coordenadas dos pontos das linhas. ### <code r> setwd("d:\\temp") require(rgdal) require(maptools) #Leitura do shapefile cnt <- readShapeLines("POLY.shp") plot(cnt, col=3) as.data.frame(cnt) # PIDS # 0 1 # 1 2 # 2 3 # 3 4 str(cnt, max=3) str(cnt@lines[[1]]) my.coords <- NULL for (i in 1:length(cnt@lines)) { for (j in 1:length(cnt@lines[[i]])) df <- data.frame(cnt@lines[[i]]@Lines[[j]]@coords, ID=cnt@lines [[i]]@ID) my.coords <- rbind(mycoords, df) } my.coords ch <- chull(my.coords) my.hull <- my.coords[ch,] points(my.hull, col="red") lines(rbind(my.hull, my.hull[1,])) ### </code> ================================================ Éder Comunello PhD Student in Agricultural Systems Engineering (USP/Esalq) Brazilian Agricultural Research Corporation (Embrapa) Dourados, MS, Brazil [22 16.5'S, 54 49.0'W] Em 25 de janeiro de 2016 16:17, ASANTOS <alexandresantosbr@yahoo.com.br> escreveu:
Prezados Listeiros,
Eu gostaria de criar um convex hull que envolvesse 4 polígonos, representados por um objeto oriundo da leitura de um readShapeLines(), sem ter que transformar o objeto em SpatialPolygonsDataFrame, isso é possível?
Meu CRM:
#Pacotes
require(rgdal) require(maptools)
#------------------------------------------------------------------------------- #Crio 4 polígonos
sr <- SpatialPolygons(list( Polygons(list(Polygon(cbind(c(180114, 180553, 181127, 181477, 181294, 181007, 180409, 180162, 180114), c(332349, 332057, 332342, 333250, 333558, 333676, 332618, 332413, 332349)))),'1'), Polygons(list(Polygon(cbind(c(180042, 180545, 180553, 180314, 179955, 179142, 179437, 179524, 179979, 180042), c(332373, 332026, 331426, 330889, 330683, 331133, 331623, 332152, 332357, 332373)))),'2'), Polygons(list(Polygon(cbind(c(179110, 179907, 180433, 180712, 180752, 180329, 179875, 179668, 179572, 179269, 178879, 178600, 178544, 179046, 179110), c(331086, 330620, 330494, 330265, 330075, 330233, 330336, 330004, 329783, 329665, 329720, 329933, 330478, 331062, 331086)))),'3'), Polygons(list(Polygon(cbind(c(180304, 180403,179632,179420,180304), c(332791, 333204, 333635, 333058, 332791)))),'4'))) plot(sr)
#Converto em polígono espacial
srdf=SpatialPolygonsDataFrame(sr, data.frame(row.names=c('1','2','3','4'), PIDS=1:4)) srdf@data
#Criação do shapefile para ilustrar o problema
writeOGR(srdf, getwd(), 'POLY', 'ESRI Shapefile')
#Leitura do shapefile
contorno_line_X <- readShapeLines ("POLY.shp")
#Plot
plot(contorno_line_X)
#Tentativa de criar o convex hull
df.data = as.data.frame(contorno_line_X) ch <- chull(df.data) lines(ch, col="red") ##Não funciona
Obrigado,
-- ====================================================================== Alexandre dos Santos Proteção Florestal IFMT - Instituto Federal de Educação, Ciência e Tecnologia de Mato Grosso Campus Cáceres Caixa Postal 244 Avenida dos Ramires, s/n Bairro: Distrito Industrial Cáceres - MT CEP: 78.200-000 Fone: (+55) 65 8132-8112 (TIM) (+55) 65 9686-6970 (VIVO) e-mails:alexandresantosbr@yahoo.com.br alexandre.santos@cas.ifmt.edu.br Lattes: http://lattes.cnpq.br/1360403201088680 OrcID: orcid.org/0000-0001-8232-6722 Researchgate: https://www.researchgate.net/profile/Alexandre_Santos10 LinkedIn: https://br.linkedin.com/in/alexandre-dos-santos-87961635 ======================================================================
_______________________________________________ R-br mailing list R-br@listas.c3sl.ufpr.br https://listas.inf.ufpr.br/cgi-bin/mailman/listinfo/r-br Leia o guia de postagem (http://www.leg.ufpr.br/r-br-guia) e fornea cdigo mnimo reproduzvel.
Fantástico Éder!! Na verdade só tinha um errinho em um mycoords que era my.coords, Muito obrigado pela solução. Segue problema resolvido: ### <code r> require(rgdal) require(maptools) #------------------------------------------------------------------------------- #Crio 4 polígonos sr <- SpatialPolygons(list( Polygons(list(Polygon(cbind(c(180114, 180553, 181127, 181477, 181294, 181007, 180409, 180162, 180114), c(332349, 332057, 332342, 333250, 333558, 333676, 332618, 332413, 332349)))),'1'), Polygons(list(Polygon(cbind(c(180042, 180545, 180553, 180314, 179955, 179142, 179437, 179524, 179979, 180042), c(332373, 332026, 331426, 330889, 330683, 331133, 331623, 332152, 332357, 332373)))),'2'), Polygons(list(Polygon(cbind(c(179110, 179907, 180433, 180712, 180752, 180329, 179875, 179668, 179572, 179269, 178879, 178600, 178544, 179046, 179110), c(331086, 330620, 330494, 330265, 330075, 330233, 330336, 330004, 329783, 329665, 329720, 329933, 330478, 331062, 331086)))),'3'), Polygons(list(Polygon(cbind(c(180304, 180403,179632,179420,180304), c(332791, 333204, 333635, 333058, 332791)))),'4'))) plot(sr) #Converto em polígono espacial srdf=SpatialPolygonsDataFrame(sr, data.frame(row.names=c('1','2','3','4'), PIDS=1:4)) srdf@data #Criação do shapefile para ilustrar o problema writeOGR(srdf, getwd(), 'POLY', 'ESRI Shapefile') #Leitura do shapefile cnt <- readShapeLines("POLY.shp") plot(cnt, col=1) as.data.frame(cnt) # PIDS # 0 1 # 1 2 # 2 3 # 3 4 str(cnt, max=3) str(cnt@lines[[1]]) my.coords <- NULL for (i in 1:length(cnt@lines)) { for (j in 1:length(cnt@lines[[i]])) df <- data.frame(cnt@lines[[i]]@Lines[[j]]@coords, ID=cnt@lines[[i]]@ID) my.coords <- rbind(my.coords, df) } my.coords ch <- chull(my.coords) my.hull <- my.coords[ch,] points(my.hull, col="red") lines(rbind(my.hull, my.hull[1,])) ### </code> -- ====================================================================== Alexandre dos Santos Proteção Florestal IFMT - Instituto Federal de Educação, Ciência e Tecnologia de Mato Grosso Campus Cáceres Caixa Postal 244 Avenida dos Ramires, s/n Bairro: Distrito Industrial Cáceres - MT CEP: 78.200-000 Fone: (+55) 65 8132-8112 (TIM) (+55) 65 9686-6970 (VIVO) e-mails:alexandresantosbr@yahoo.com.br alexandre.santos@cas.ifmt.edu.br Lattes: http://lattes.cnpq.br/1360403201088680 OrcID: orcid.org/0000-0001-8232-6722 Researchgate: https://www.researchgate.net/profile/Alexandre_Santos10 LinkedIn: https://br.linkedin.com/in/alexandre-dos-santos-87961635 ====================================================================== Em 25/01/2016 21:04, Éder Comunello escreveu:
#------------------------------------------------------------------------------- #Crio 4 polígonos
sr <- SpatialPolygons(list( Polygons(list(Polygon(cbind(c(180114, 180553, 181127, 181477, 181294, 181007, 180409, 180162, 180114), c(332349, 332057, 332342, 333250, 333558, 333676, 332618, 332413, 332349)))),'1'), Polygons(list(Polygon(cbind(c(180042, 180545, 180553, 180314, 179955, 179142, 179437, 179524, 179979, 180042), c(332373, 332026, 331426, 330889, 330683, 331133, 331623, 332152, 332357, 332373)))),'2'), Polygons(list(Polygon(cbind(c(179110, 179907, 180433, 180712, 180752, 180329, 179875, 179668, 179572, 179269, 178879, 178600, 178544, 179046, 179110), c(331086, 330620, 330494, 330265, 330075, 330233, 330336, 330004, 329783, 329665, 329720, 329933, 330478, 331062, 331086)))),'3'), Polygons(list(Polygon(cbind(c(180304, 180403,179632,179420,180304), c(332791, 333204, 333635, 333058, 332791)))),'4'))) plot(sr)
#Converto em polígono espacial
srdf=SpatialPolygonsDataFrame(sr, data.frame(row.names=c('1','2','3','4'), PIDS=1:4)) srdf@data
#Criação do shapefile para ilustrar o problema
writeOGR(srdf, getwd(), 'POLY', 'ESRI Shapefile')
Não consegui rodar, veja o que aconteceu aqui
> srdf=SpatialPolygonsDataFrame(sr,
+ data.frame(row.names=c('1','2','3','4'), PIDS=1:4)) srdf@data
Error: unexpected symbol in:
"srdf=SpatialPolygonsDataFrame(sr,
data.frame(row.names=c('1','2','3','4'), PIDS=1:4)) srdf"
>
Fantástico Éder!!
Na verdade só tinha um errinho em um mycoords que era my.coords,
Muito obrigado pela solução.
Segue problema resolvido:
### <code r>
require(rgdal)
require(maptools)
#-------------------------------------------------------------------------------
#Crio 4 polígonos
sr <- SpatialPolygons(list(
Polygons(list(Polygon(cbind(c(180114, 180553, 181127, 181477, 181294,
181007, 180409,
180162, 180114), c(332349, 332057, 332342, 333250, 333558, 333676,
332618, 332413, 332349)))),'1'),
Polygons(list(Polygon(cbind(c(180042, 180545, 180553, 180314, 179955,
179142, 179437,
179524, 179979, 180042), c(332373, 332026, 331426, 330889, 330683,
331133, 331623, 332152, 332357, 332373)))),'2'),
Polygons(list(Polygon(cbind(c(179110, 179907, 180433, 180712, 180752,
180329, 179875,
179668, 179572, 179269, 178879, 178600, 178544, 179046, 179110),
c(331086, 330620, 330494, 330265, 330075, 330233, 330336, 330004,
329783, 329665, 329720, 329933, 330478, 331062, 331086)))),'3'),
Polygons(list(Polygon(cbind(c(180304, 180403,179632,179420,180304),
c(332791, 333204, 333635, 333058, 332791)))),'4')))
plot(sr)
#Converto em polígono espacial
srdf=SpatialPolygonsDataFrame(sr,
data.frame(row.names=c('1','2','3','4'), PIDS=1:4))
srdf@data
#Criação do shapefile para ilustrar o problema
writeOGR(srdf, getwd(), 'POLY', 'ESRI Shapefile')
#Leitura do shapefile
cnt <- readShapeLines("POLY.shp")
plot(cnt, col=1)
as.data.frame(cnt)
# PIDS
# 0 1
# 1 2
# 2 3
# 3 4
str(cnt, max=3)
str(cnt@lines[[1]])
my.coords <- NULL
for (i in 1:length(cnt@lines)) {
for (j in 1:length(cnt@lines[[i]]))
df <- data.frame(cnt@lines[[i]]@Lines[[j]]@coords,
ID=cnt@lines[[i]]@ID)
my.coords <- rbind(my.coords, df)
}
my.coords
ch <- chull(my.coords)
my.hull <- my.coords[ch,]
points(my.hull, col="red")
lines(rbind(my.hull, my.hull[1,]))
### </code>
--
======================================================================
Alexandre dos Santos
Proteção Florestal
IFMT - Instituto Federal de Educação, Ciência e Tecnologia de Mato Grosso
Campus Cáceres
Caixa Postal 244
Avenida dos Ramires, s/n
Bairro: Distrito Industrial
Cáceres - MT CEP: 78.200-000
Fone: (+55) 65 8132-8112 (TIM) (+55) 65 9686-6970 (VIVO)
e-mails:alexandresantosbr@yahoo.com.br
alexandre.santos@cas.ifmt.edu.br
Lattes: http://lattes.cnpq.br/1360403201088680
OrcID: orcid.org/0000-0001-8232-6722
Researchgate: https://www.researchgate.net/profile/Alexandre_Santos10
LinkedIn: https://br.linkedin.com/in/alexandre-dos-santos-87961635
======================================================================
Em 25/01/2016 21:04, Éder Comunello escreveu:
> #-------------------------------------------------------------------------------
> #Crio 4 polígonos
>
> sr <- SpatialPolygons(list(
> Polygons(list(Polygon(cbind(c(180114, 180553, 181127, 181477, 181294,
> 181007, 180409,
> 180162, 180114), c(332349, 332057, 332342, 333250, 333558, 333676,
> 332618, 332413, 332349)))),'1'),
> Polygons(list(Polygon(cbind(c(180042, 180545, 180553, 180314, 179955,
> 179142, 179437,
> 179524, 179979, 180042), c(332373, 332026, 331426, 330889, 330683,
> 331133, 331623, 332152, 332357, 332373)))),'2'),
> Polygons(list(Polygon(cbind(c(179110, 179907, 180433, 180712, 180752,
> 180329, 179875,
> 179668, 179572, 179269, 178879, 178600, 178544, 179046, 179110),
> c(331086, 330620, 330494, 330265, 330075, 330233, 330336, 330004,
> 329783, 329665, 329720, 329933, 330478, 331062, 331086)))),'3'),
> Polygons(list(Polygon(cbind(c(180304, 180403,179632,179420,180304),
> c(332791, 333204, 333635, 333058, 332791)))),'4')))
> plot(sr)
>
> #Converto em polígono espacial
>
> srdf=SpatialPolygonsDataFrame(sr,
> data.frame(row.names=c('1','2','3','4'), PIDS=1:4))
> srdf@data
>
> #Criação do shapefile para ilustrar o problema
>
> writeOGR(srdf, getwd(), 'POLY', 'ESRI Shapefile')
_______________________________________________
R-br mailing list
R-br@listas.c3sl.ufpr.br
https://listas.inf.ufpr.br/cgi-bin/mailman/listinfo/r-br
Leia o guia de postagem (http://www.leg.ufpr.br/r-br-guia) e fornea cdigo mnimo reproduzvel.
---
Este email foi escaneado pelo Avast antivírus.
https://www.avast.com/antivirus
Mauro,
O problema esta na separação existente nessa sentença, tente
fazer:
srdf=SpatialPolygonsDataFrame(sr,data.frame(row.names=c('1','2','3','4'), PIDS=1:4))
Solução completa conferida:
### <code r>
require(rgdal)
require(maptools)
#-------------------------------------------------------------------------------
#Crio 4 polígonos
sr <- SpatialPolygons(list(
Polygons(list(Polygon(cbind(c(180114, 180553, 181127, 181477, 181294,
181007, 180409,
180162, 180114), c(332349, 332057, 332342, 333250, 333558, 333676,
332618, 332413, 332349)))),'1'),
Polygons(list(Polygon(cbind(c(180042, 180545, 180553, 180314, 179955,
179142, 179437,
179524, 179979, 180042), c(332373, 332026, 331426, 330889, 330683,
331133, 331623, 332152, 332357, 332373)))),'2'),
Polygons(list(Polygon(cbind(c(179110, 179907, 180433, 180712, 180752,
180329, 179875,
179668, 179572, 179269, 178879, 178600, 178544, 179046, 179110),
c(331086, 330620, 330494, 330265, 330075, 330233, 330336, 330004,
329783, 329665, 329720, 329933, 330478, 331062, 331086)))),'3'),
Polygons(list(Polygon(cbind(c(180304, 180403,179632,179420,180304),
c(332791, 333204, 333635, 333058, 332791)))),'4')))
plot(sr)
#Converto em polígono espacial
srdf=SpatialPolygonsDataFrame(sr,data.frame(row.names=c('1','2','3','4'), PIDS=1:4))
srdf@data
#Criação do shapefile para ilustrar o problema
writeOGR(srdf, getwd(), 'POLY001', 'ESRI Shapefile')
#Leitura do shapefile
cnt <- readShapeLines("POLY001.shp")
plot(cnt, col=1)
as.data.frame(cnt)
# PIDS
# 0 1
# 1 2
# 2 3
# 3 4
str(cnt, max=3)
str(cnt@lines[[1]])
my.coords <- NULL
for (i in 1:length(cnt@lines)) {
for (j in 1:length(cnt@lines[[i]]))
df <- data.frame(cnt@lines[[i]]@Lines[[j]]@coords,
ID=cnt@lines[[i]]@ID)
my.coords <- rbind(my.coords, df)
}
my.coords
ch <- chull(my.coords)
my.hull <- my.coords[ch,]
points(my.hull, col="red")
lines(rbind(my.hull, my.hull[1,]))
### </code>
--
======================================================================
Alexandre dos Santos
Proteção Florestal
IFMT - Instituto Federal de Educação, Ciência e Tecnologia de Mato Grosso
Campus Cáceres
Caixa Postal 244
Avenida dos Ramires, s/n
Bairro: Distrito Industrial
Cáceres - MT CEP: 78.200-000
Fone: (+55) 65 8132-8112 (TIM) (+55) 65 9686-6970 (VIVO)
e-mails:alexandresantosbr@yahoo.com.br
alexandre.santos@cas.ifmt.edu.br
Lattes: http://lattes.cnpq.br/1360403201088680
OrcID: orcid.org/0000-0001-8232-6722
Researchgate: https://www.researchgate.net/profile/Alexandre_Santos10
LinkedIn: https://br.linkedin.com/in/alexandre-dos-santos-87961635
======================================================================
Em 26/01/2016 21:32, Mauro Sznelwar escreveu:
> Não consegui rodar, veja o que aconteceu aqui
>
>> srdf=SpatialPolygonsDataFrame(sr,
> + data.frame(row.names=c('1','2','3','4'), PIDS=1:4)) srdf@data
> Error: unexpected symbol in:
> "srdf=SpatialPolygonsDataFrame(sr,
> data.frame(row.names=c('1','2','3','4'), PIDS=1:4)) srdf"
>
> Fantástico Éder!!
>
> Na verdade só tinha um errinho em um mycoords que era my.coords,
>
> Muito obrigado pela solução.
>
>
> Segue problema resolvido:
>
> ### <code r>
> require(rgdal)
> require(maptools)
>
> #-------------------------------------------------------------------------------
> #Crio 4 polígonos
>
> sr <- SpatialPolygons(list(
> Polygons(list(Polygon(cbind(c(180114, 180553, 181127, 181477, 181294,
> 181007, 180409,
> 180162, 180114), c(332349, 332057, 332342, 333250, 333558, 333676,
> 332618, 332413, 332349)))),'1'),
> Polygons(list(Polygon(cbind(c(180042, 180545, 180553, 180314, 179955,
> 179142, 179437,
> 179524, 179979, 180042), c(332373, 332026, 331426, 330889, 330683,
> 331133, 331623, 332152, 332357, 332373)))),'2'),
> Polygons(list(Polygon(cbind(c(179110, 179907, 180433, 180712, 180752,
> 180329, 179875,
> 179668, 179572, 179269, 178879, 178600, 178544, 179046, 179110),
> c(331086, 330620, 330494, 330265, 330075, 330233, 330336, 330004,
> 329783, 329665, 329720, 329933, 330478, 331062, 331086)))),'3'),
> Polygons(list(Polygon(cbind(c(180304, 180403,179632,179420,180304),
> c(332791, 333204, 333635, 333058, 332791)))),'4')))
> plot(sr)
>
> #Converto em polígono espacial
>
> srdf=SpatialPolygonsDataFrame(sr,
> data.frame(row.names=c('1','2','3','4'), PIDS=1:4))
> srdf@data
>
> #Criação do shapefile para ilustrar o problema
>
> writeOGR(srdf, getwd(), 'POLY', 'ESRI Shapefile')
>
> #Leitura do shapefile
> cnt <- readShapeLines("POLY.shp")
> plot(cnt, col=1)
>
> as.data.frame(cnt)
> # PIDS
> # 0 1
> # 1 2
> # 2 3
> # 3 4
>
> str(cnt, max=3)
> str(cnt@lines[[1]])
>
> my.coords <- NULL
>
> for (i in 1:length(cnt@lines)) {
> for (j in 1:length(cnt@lines[[i]]))
> df <- data.frame(cnt@lines[[i]]@Lines[[j]]@coords,
> ID=cnt@lines[[i]]@ID)
> my.coords <- rbind(my.coords, df)
> }
>
> my.coords
>
> ch <- chull(my.coords)
> my.hull <- my.coords[ch,]
> points(my.hull, col="red")
> lines(rbind(my.hull, my.hull[1,]))
> ### </code>
>
> --
> ======================================================================
> Alexandre dos Santos
> Proteção Florestal
> IFMT - Instituto Federal de Educação, Ciência e Tecnologia de Mato Grosso
> Campus Cáceres
> Caixa Postal 244
> Avenida dos Ramires, s/n
> Bairro: Distrito Industrial
> Cáceres - MT CEP: 78.200-000
> Fone: (+55) 65 8132-8112 (TIM) (+55) 65 9686-6970 (VIVO)
> e-mails:alexandresantosbr@yahoo.com.br
> alexandre.santos@cas.ifmt.edu.br
> Lattes: http://lattes.cnpq.br/1360403201088680
> OrcID: orcid.org/0000-0001-8232-6722
> Researchgate: https://www.researchgate.net/profile/Alexandre_Santos10
> LinkedIn: https://br.linkedin.com/in/alexandre-dos-santos-87961635
> ======================================================================
>
> Em 25/01/2016 21:04, Éder Comunello escreveu:
>> #-------------------------------------------------------------------------------
>> #Crio 4 polígonos
>>
>> sr <- SpatialPolygons(list(
>> Polygons(list(Polygon(cbind(c(180114, 180553, 181127, 181477, 181294,
>> 181007, 180409,
>> 180162, 180114), c(332349, 332057, 332342, 333250, 333558, 333676,
>> 332618, 332413, 332349)))),'1'),
>> Polygons(list(Polygon(cbind(c(180042, 180545, 180553, 180314, 179955,
>> 179142, 179437,
>> 179524, 179979, 180042), c(332373, 332026, 331426, 330889, 330683,
>> 331133, 331623, 332152, 332357, 332373)))),'2'),
>> Polygons(list(Polygon(cbind(c(179110, 179907, 180433, 180712, 180752,
>> 180329, 179875,
>> 179668, 179572, 179269, 178879, 178600, 178544, 179046, 179110),
>> c(331086, 330620, 330494, 330265, 330075, 330233, 330336, 330004,
>> 329783, 329665, 329720, 329933, 330478, 331062, 331086)))),'3'),
>> Polygons(list(Polygon(cbind(c(180304, 180403,179632,179420,180304),
>> c(332791, 333204, 333635, 333058, 332791)))),'4')))
>> plot(sr)
>>
>> #Converto em polígono espacial
>>
>> srdf=SpatialPolygonsDataFrame(sr,
>> data.frame(row.names=c('1','2','3','4'), PIDS=1:4))
>> srdf@data
>>
>> #Criação do shapefile para ilustrar o problema
>>
>> writeOGR(srdf, getwd(), 'POLY', 'ESRI Shapefile')
> _______________________________________________
> R-br mailing list
> R-br@listas.c3sl.ufpr.br
> https://listas.inf.ufpr.br/cgi-bin/mailman/listinfo/r-br
> Leia o guia de postagem (http://www.leg.ufpr.br/r-br-guia) e fornea cdigo mnimo reproduzvel.
>
>
> ---
> Este email foi escaneado pelo Avast antivírus.
> https://www.avast.com/antivirus
>
> _______________________________________________
> R-br mailing list
> R-br@listas.c3sl.ufpr.br
> https://listas.inf.ufpr.br/cgi-bin/mailman/listinfo/r-br
> Leia o guia de postagem (http://www.leg.ufpr.br/r-br-guia) e fornea cdigo mnimo reproduzvel.
Muito obrigado, funcionou!
Mauro,
O problema esta na separação existente nessa sentença, tente
fazer:
srdf=SpatialPolygonsDataFrame(sr,data.frame(row.names=c('1','2','3','4'), PIDS=1:4))
Solução completa conferida:
### <code r>
require(rgdal)
require(maptools)
#-------------------------------------------------------------------------------
#Crio 4 polígonos
sr <- SpatialPolygons(list(
Polygons(list(Polygon(cbind(c(180114, 180553, 181127, 181477, 181294,
181007, 180409,
180162, 180114), c(332349, 332057, 332342, 333250, 333558, 333676,
332618, 332413, 332349)))),'1'),
Polygons(list(Polygon(cbind(c(180042, 180545, 180553, 180314, 179955,
179142, 179437,
179524, 179979, 180042), c(332373, 332026, 331426, 330889, 330683,
331133, 331623, 332152, 332357, 332373)))),'2'),
Polygons(list(Polygon(cbind(c(179110, 179907, 180433, 180712, 180752,
180329, 179875,
179668, 179572, 179269, 178879, 178600, 178544, 179046, 179110),
c(331086, 330620, 330494, 330265, 330075, 330233, 330336, 330004,
329783, 329665, 329720, 329933, 330478, 331062, 331086)))),'3'),
Polygons(list(Polygon(cbind(c(180304, 180403,179632,179420,180304),
c(332791, 333204, 333635, 333058, 332791)))),'4')))
plot(sr)
#Converto em polígono espacial
srdf=SpatialPolygonsDataFrame(sr,data.frame(row.names=c('1','2','3','4'), PIDS=1:4))
srdf@data
#Criação do shapefile para ilustrar o problema
writeOGR(srdf, getwd(), 'POLY001', 'ESRI Shapefile')
#Leitura do shapefile
cnt <- readShapeLines("POLY001.shp")
plot(cnt, col=1)
as.data.frame(cnt)
# PIDS
# 0 1
# 1 2
# 2 3
# 3 4
str(cnt, max=3)
str(cnt@lines[[1]])
my.coords <- NULL
for (i in 1:length(cnt@lines)) {
for (j in 1:length(cnt@lines[[i]]))
df <- data.frame(cnt@lines[[i]]@Lines[[j]]@coords,
ID=cnt@lines[[i]]@ID)
my.coords <- rbind(my.coords, df)
}
my.coords
ch <- chull(my.coords)
my.hull <- my.coords[ch,]
points(my.hull, col="red")
lines(rbind(my.hull, my.hull[1,]))
### </code>
--
======================================================================
Alexandre dos Santos
Proteção Florestal
IFMT - Instituto Federal de Educação, Ciência e Tecnologia de Mato Grosso
Campus Cáceres
Caixa Postal 244
Avenida dos Ramires, s/n
Bairro: Distrito Industrial
Cáceres - MT CEP: 78.200-000
Fone: (+55) 65 8132-8112 (TIM) (+55) 65 9686-6970 (VIVO)
e-mails:alexandresantosbr@yahoo.com.br
alexandre.santos@cas.ifmt.edu.br
Lattes: http://lattes.cnpq.br/1360403201088680
OrcID: orcid.org/0000-0001-8232-6722
Researchgate: https://www.researchgate.net/profile/Alexandre_Santos10
LinkedIn: https://br.linkedin.com/in/alexandre-dos-santos-87961635
======================================================================
Em 26/01/2016 21:32, Mauro Sznelwar escreveu:
> Não consegui rodar, veja o que aconteceu aqui
>
>> srdf=SpatialPolygonsDataFrame(sr,
> + data.frame(row.names=c('1','2','3','4'), PIDS=1:4)) srdf@data
> Error: unexpected symbol in:
> "srdf=SpatialPolygonsDataFrame(sr,
> data.frame(row.names=c('1','2','3','4'), PIDS=1:4)) srdf"
>
> Fantástico Éder!!
>
> Na verdade só tinha um errinho em um mycoords que era my.coords,
>
> Muito obrigado pela solução.
>
>
> Segue problema resolvido:
>
> ### <code r>
> require(rgdal)
> require(maptools)
>
> #-------------------------------------------------------------------------------
> #Crio 4 polígonos
>
> sr <- SpatialPolygons(list(
> Polygons(list(Polygon(cbind(c(180114, 180553, 181127, 181477, 181294,
> 181007, 180409,
> 180162, 180114), c(332349, 332057, 332342, 333250, 333558, 333676,
> 332618, 332413, 332349)))),'1'),
> Polygons(list(Polygon(cbind(c(180042, 180545, 180553, 180314, 179955,
> 179142, 179437,
> 179524, 179979, 180042), c(332373, 332026, 331426, 330889, 330683,
> 331133, 331623, 332152, 332357, 332373)))),'2'),
> Polygons(list(Polygon(cbind(c(179110, 179907, 180433, 180712, 180752,
> 180329, 179875,
> 179668, 179572, 179269, 178879, 178600, 178544, 179046, 179110),
> c(331086, 330620, 330494, 330265, 330075, 330233, 330336, 330004,
> 329783, 329665, 329720, 329933, 330478, 331062, 331086)))),'3'),
> Polygons(list(Polygon(cbind(c(180304, 180403,179632,179420,180304),
> c(332791, 333204, 333635, 333058, 332791)))),'4')))
> plot(sr)
>
> #Converto em polígono espacial
>
> srdf=SpatialPolygonsDataFrame(sr,
> data.frame(row.names=c('1','2','3','4'), PIDS=1:4))
> srdf@data
>
> #Criação do shapefile para ilustrar o problema
>
> writeOGR(srdf, getwd(), 'POLY', 'ESRI Shapefile')
>
> #Leitura do shapefile
> cnt <- readShapeLines("POLY.shp")
> plot(cnt, col=1)
>
> as.data.frame(cnt)
> # PIDS
> # 0 1
> # 1 2
> # 2 3
> # 3 4
>
> str(cnt, max=3)
> str(cnt@lines[[1]])
>
> my.coords <- NULL
>
> for (i in 1:length(cnt@lines)) {
> for (j in 1:length(cnt@lines[[i]]))
> df <- data.frame(cnt@lines[[i]]@Lines[[j]]@coords,
> ID=cnt@lines[[i]]@ID)
> my.coords <- rbind(my.coords, df)
> }
>
> my.coords
>
> ch <- chull(my.coords)
> my.hull <- my.coords[ch,]
> points(my.hull, col="red")
> lines(rbind(my.hull, my.hull[1,]))
> ### </code>
>
> --
> ======================================================================
> Alexandre dos Santos
> Proteção Florestal
> IFMT - Instituto Federal de Educação, Ciência e Tecnologia de Mato Grosso
> Campus Cáceres
> Caixa Postal 244
> Avenida dos Ramires, s/n
> Bairro: Distrito Industrial
> Cáceres - MT CEP: 78.200-000
> Fone: (+55) 65 8132-8112 (TIM) (+55) 65 9686-6970 (VIVO)
> e-mails:alexandresantosbr@yahoo.com.br
> alexandre.santos@cas.ifmt.edu.br
> Lattes: http://lattes.cnpq.br/1360403201088680
> OrcID: orcid.org/0000-0001-8232-6722
> Researchgate: https://www.researchgate.net/profile/Alexandre_Santos10
> LinkedIn: https://br.linkedin.com/in/alexandre-dos-santos-87961635
> ======================================================================
>
> Em 25/01/2016 21:04, Éder Comunello escreveu:
>> #-------------------------------------------------------------------------------
>> #Crio 4 polígonos
>>
>> sr <- SpatialPolygons(list(
>> Polygons(list(Polygon(cbind(c(180114, 180553, 181127, 181477, 181294,
>> 181007, 180409,
>> 180162, 180114), c(332349, 332057, 332342, 333250, 333558, 333676,
>> 332618, 332413, 332349)))),'1'),
>> Polygons(list(Polygon(cbind(c(180042, 180545, 180553, 180314, 179955,
>> 179142, 179437,
>> 179524, 179979, 180042), c(332373, 332026, 331426, 330889, 330683,
>> 331133, 331623, 332152, 332357, 332373)))),'2'),
>> Polygons(list(Polygon(cbind(c(179110, 179907, 180433, 180712, 180752,
>> 180329, 179875,
>> 179668, 179572, 179269, 178879, 178600, 178544, 179046, 179110),
>> c(331086, 330620, 330494, 330265, 330075, 330233, 330336, 330004,
>> 329783, 329665, 329720, 329933, 330478, 331062, 331086)))),'3'),
>> Polygons(list(Polygon(cbind(c(180304, 180403,179632,179420,180304),
>> c(332791, 333204, 333635, 333058, 332791)))),'4')))
>> plot(sr)
>>
>> #Converto em polígono espacial
>>
>> srdf=SpatialPolygonsDataFrame(sr,
>> data.frame(row.names=c('1','2','3','4'), PIDS=1:4))
>> srdf@data
>>
>> #Criação do shapefile para ilustrar o problema
>>
>> writeOGR(srdf, getwd(), 'POLY', 'ESRI Shapefile')
> _______________________________________________
> R-br mailing list
> R-br@listas.c3sl.ufpr.br
> https://listas.inf.ufpr.br/cgi-bin/mailman/listinfo/r-br
> Leia o guia de postagem (http://www.leg.ufpr.br/r-br-guia) e fornea cdigo mnimo reproduzvel.
>
>
> ---
> Este email foi escaneado pelo Avast antivírus.
> https://www.avast.com/antivirus
>
> _______________________________________________
> R-br mailing list
> R-br@listas.c3sl.ufpr.br
> https://listas.inf.ufpr.br/cgi-bin/mailman/listinfo/r-br
> Leia o guia de postagem (http://www.leg.ufpr.br/r-br-guia) e fornea cdigo mnimo reproduzvel.
_______________________________________________
R-br mailing list
R-br@listas.c3sl.ufpr.br
https://listas.inf.ufpr.br/cgi-bin/mailman/listinfo/r-br
Leia o guia de postagem (http://www.leg.ufpr.br/r-br-guia) e fornea cdigo mnimo reproduzvel.
---
Este email foi escaneado pelo Avast antivírus.
https://www.avast.com/antivirus
participantes (3)
-
ASANTOS -
Mauro Sznelwar -
Éder Comunello