[R-br] RES: RES: Criar um convex hull para um objeto shapefile [RESOLVIDO]

Mauro Sznelwar sznelwar em uol.com.br
Terça Janeiro 26 23:37:22 BRST 2016


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 em 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 em lines[[1]])

my.coords <- NULL

for (i in 1:length(cnt em lines)) {
       for (j in 1:length(cnt em lines[[i]]))
            df <- data.frame(cnt em lines[[i]]@Lines[[j]]@coords,
ID=cnt em 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 em yahoo.com.br
         alexandre.santos em 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 em 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 em 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 em lines[[1]])
>
> my.coords <- NULL
>
> for (i in 1:length(cnt em lines)) {
>        for (j in 1:length(cnt em lines[[i]]))
>             df <- data.frame(cnt em lines[[i]]@Lines[[j]]@coords,
> ID=cnt em 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 em yahoo.com.br
>           alexandre.santos em 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 em data
>>
>> #Criação do shapefile para ilustrar o problema
>>
>> writeOGR(srdf, getwd(), 'POLY', 'ESRI Shapefile')
> _______________________________________________
> R-br mailing list
> R-br em 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 em 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 em 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



Mais detalhes sobre a lista de discussão R-br