1

Ao Tentar executar o script abaixo:

Area="C:\Users\user\Desktop\shape.shp"
Pasta=""


library(raster)
library(rgdal)
library(rgeos)
library(maptools)
long2UTM <- function(long) {(floor((long + 180)/6) %% 60) + 1}

calc_bpt = function(pols) {
coords=coordinates(pols)
for(i in seq_len(length(pols))) {
p=pols[i,]
init=0
estep=sqrt(gArea(p)/pi)/10
repeat {
repeat {
r = init + estep
p2 = gBuffer(p, width = -r)
if( is.null(p2)||gArea(p2) <= 0 )
estep = estep/2 else break
}
areas=sapply(p2@polygons[[1]]@Polygons, function(x) x@area)
if(length(areas) > 1) {
ind.max = which.max(areas)
p2 = SpatialPolygons(list(Polygons(list(p2@polygons[[1]]@Polygons
[ind.max][[1]]),
ID="middle")), proj4string=CRS(proj4string(p2)))
}
if( gContains(p, gConvexHull(p2)) ) break else init=init+estep
}
coords[i,] = coordinates(p2)
}
coords
}

uniquepol <- function(pol,fast=TRUE){
if(fast){
areapol = gArea(pol,byid=TRUE)
endpol = pol[!duplicated(areapol),]
}else{
for(i in 1:length(pol)){
subpol = pol[i,]
if(i==1){
endpol<-subpol
}else{
duplic = gEquals(endpol,subpol, byid = TRUE)
if(sum(duplic)==0){
endpol = spRbind(subpol, endpol)
}
}
}
}
endpol
}

gCorrigir = function(pol) {
field <- pol

fields <- gSimplify(field, tol=0.1, topologyPreserve=TRUE)

field <-SpatialPolygonsDataFrame(fields,field@data)

validade<-suppressWarnings(gIsValid(field))

fieldarea<-gArea(field, byid=TRUE)
field<-field[fieldarea>400,]

field<-uniquepol(field)

if(!validade|length(field)>1){

gI<- sum(gIntersects(field,field, byid = TRUE))

if(!validade|gI>length(field)){

fieldbuf<-gBuffer(field,width=- 0.05,byid=TRUE)

validade<-suppressWarnings(gIsValid(fieldbuf))

gI<- sum(gIntersects(fieldbuf,fieldbuf, byid = TRUE))

if(!validade|gI>length(field)){


fieldarea<-gArea(field, byid=TRUE)
field<-field[order(fieldarea,decreasing=TRUE),]

field<-field[!apply(gCovers(field,field, byid = TRUE), 2, function(x) all
(x)|sum(x)>2),]

sppts<- SpatialPoints(calc_bpt(field),projutm)

fieldsplit<-gSimplify(field, tol=1, topologyPreserve=TRUE)
fieldsplit<-gBuffer(fieldsplit,width=- 0.05,byid=TRUE)
k=1
for(k in 1:(length(field)+1)){
if(k==1){
fieldpartt<-fieldsplit[1,]
}else{
if(length(fieldpartt)==length(field)){break}
for(j in 1:length(fieldsplit)){
fieldpart<-fieldsplit[j,]
gI<- gIntersects(fieldpart,fieldpartt, byid = TRUE)
if(sum(gI)==0){
fieldpartt<-spRbind(fieldpart, fieldpartt)
if(length(fieldpartt)==length(field)){break}
}
}
if(sum(is.na(over(sppts,fieldpartt)))==0){break}
fieldsplit<- gDifference( fieldsplit,fieldpartt, byid=TRUE)
fieldsplit<-uniquepol(fieldsplit)
fieldsplit<-gBuffer(fieldsplit,width=- 0.05,byid=TRUE)
}
}


fieldpoly<-gBuffer(fieldpartt,width=- 0.05,byid=TRUE)
df1<-data.frame(over(SpatialPoints(calc_bpt(fieldpoly),projutm),field))
ids<-sapply(slot(fieldpoly, "polygons"), slot, "ID")
row.names(df1)<-ids
field <-SpatialPolygonsDataFrame(fieldpoly,df1)


field <-field[rowSums(!is.na(field@data))!=0,]

validade<-suppressWarnings(gIsValid(fieldbuf))

gI<- sum(gIntersects(field,field, byid = TRUE))

if(!validade|gI>length(field)){
field<-("Erro de geometria muito grave")
break
}
}else{
df1<-data.frame(over(SpatialPoints(calc_bpt(fieldbuf),projutm),field))
ids<-sapply(slot(fieldbuf, "polygons"), slot, "ID")
row.names(df1)<-ids
field <-SpatialPolygonsDataFrame(fieldbuf,df1)
}
}
}
field
}


projlat <-CRS("+proj=longlat +datum=WGS84 +ellps=WGS84")
first<-firstt<-TRUE
breakFlag <- FALSE


field = Area
classfield = class(field)

retorna o seguinte erro:

projorig = proj4string(field)

Error in (function (classes, fdef, mtable)  : 

unable to find an inherited method for function 'proj4string' for
signature '"character"'
Calls: proj4string -> <Anonymous>
Execução interrompida
4
  • Não saberia dizer onde está o seu problema olhando assim direto. O erro está dizendo que você está chamando a função proj4string em um objeto de classe character, que não é suportado por ela. Tente rodar linha por linha e veja onde isso está acontecendo... 28/07/2016 às 21:39
  • mas é o o projorig está tentando receber, se observar é proj4string (proj for string) estou transformando em string. 28/07/2016 às 22:13
  • 1
    O problema é que seu input está errado e não está sendo reconhecido pela função. É praticamente impossível te ajudar com o código assim, por três motivos: 1. Não é reprodutível. 2. É grande demais, o ideal é um exemplo mínimo. 3. Você copiou do console, isso dificulta a vida de quem vai (ou iria) tentar rodar o mesmo código.
    – Molx
    29/07/2016 às 1:52
  • 1
    Obg pelas dicas Molx, vou melhorar mas próximas perguntas :) 29/07/2016 às 12:04

1 Resposta 1

0

Para ajudar alguém, Adicionei essa linha acima do field = Area e funcionou como eu precisava:

 Area = readOGR(dirname(Area), gsub('.shp','',basename(Area)))

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .