Problem running application in R

1

When you try to run the script below:

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)

returns the following error:

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
    
asked by anonymous 28.07.2016 / 23:26

1 answer

0

To help someone, I added this line above field = Area and it worked as I needed it:

 Area = readOGR(dirname(Area), gsub('.shp','',basename(Area)))
    
29.07.2016 / 14:04