1
While Trying 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
I wouldn’t know where your problem is looking so straight. The error is saying that you are calling the function
proj4string
in a class objectcharacter
, which is not supported by it. Try running row by row and see where this is happening...– Daniel Falbel
but it is the projorig is trying to receive, if observing is proj4string (proj for string) I am turning into string.
– Guilherme Lima
The problem is that your input is wrong and is not being recognized by the function. It is practically impossible to help you with the code like this, for three reasons: 1. It is not reproducible. 2. It is too big, the ideal is a minimal example. 3. You copied from the console, this makes life difficult for those who will (or would) try to run the same code.
– Molx
Obg by Molx tips, I will improve but next questions :)
– Guilherme Lima