[GRASS-user] Natural breaks classification

Hi,

I have a DTM raster map and I want to classify it in 5 classes by means of natural break method (http://en.wikipedia.org/wiki/Jenks_natural_breaks_optimization). Is it possible with GRASS.

Thanks,

Salvatore Mellino

Hi Salvatore,

On 16/05/2012 17:38, Salvatore Mellino wrote:

Hi,

I have a DTM raster map and I want to classify it in 5 classes by means of natural break method (http://en.wikipedia.org/wiki/Jenks_natural_breaks_optimization). Is it possible with GRASS.

I usually do that via the R package classInt
http://cran.r-project.org/web/packages/classInt/index.html

which requires using the GRASS-R interface. For a general overview of the integration, see
http://grass.osgeo.org/wiki/R_statistics

Kind regards,

Luigi

Thanks,

I'm trying but I receive an error.

1. In R I started the libraries spgarass6 and classInt
2. I imported the DTM in R using DTM <- readRAST6("DTM@mapset", ignore.stderr=TRUE)
3. I used the command DTM_class <- classIntervals(DTM, n=5, style="fisher") and I received the error

Error in classIntervals(DTM, n = 5, style = "fisher") :
  var is not numeric

Any suggestion?

Salvatore

Il giorno 16/mag/2012, alle ore 17:58, Luigi Ponti ha scritto:

Hi Salvatore,

On 16/05/2012 17:38, Salvatore Mellino wrote:

Hi,

I have a DTM raster map and I want to classify it in 5 classes by means of natural break method (http://en.wikipedia.org/wiki/Jenks_natural_breaks_optimization). Is it possible with GRASS.

I usually do that via the R package classInt
http://cran.r-project.org/web/packages/classInt/index.html

which requires using the GRASS-R interface. For a general overview of the integration, see
http://grass.osgeo.org/wiki/R_statistics

Kind regards,

Luigi

On 16/05/2012 20:55, Salvatore Mellino wrote:

I'm trying but I receive an error.

1. In R I started the libraries spgarass6 and classInt
2. I imported the DTM in R using DTM<- readRAST6("DTM@mapset", ignore.stderr=TRUE)
3. I used the command DTM_class<- classIntervals(DTM, n=5, style="fisher") and I received the error

Error in classIntervals(DTM, n = 5, style = "fisher") :
   var is not numeric

I do not recall exactly, but it may be that classIntervals does not have a method for spatial data frames (i.e. the output of readRAST6). One thing I would try to get the breaks anyway, is a regular R data.frame, for example:

DTM.df <- as.data.frame(DTM)
DTM_class <- classIntervals(DTM.df, n=5, style="fisher")

Hope this helps,

Luigi

On 16/05/2012 21:10, Luigi Ponti wrote:

On 16/05/2012 20:55, Salvatore Mellino wrote:

I'm trying but I receive an error.

1. In R I started the libraries spgarass6 and classInt
2. I imported the DTM in R using DTM<- readRAST6("DTM@mapset", ignore.stderr=TRUE)
3. I used the command DTM_class<- classIntervals(DTM, n=5, style="fisher") and I received the error

Error in classIntervals(DTM, n = 5, style = "fisher") :
   var is not numeric

I do not recall exactly, but it may be that classIntervals does not have a method for spatial data frames (i.e. the output of readRAST6). One thing I would try to get the breaks anyway, is a regular R data.frame, for example:

DTM.df <- as.data.frame(DTM)
DTM_class <- classIntervals(DTM.df, n=5, style="fisher")

I am replying to myself as I may have omitted part of the answer -- my apologies. Start the same way:

     DTM.df <- as.data.frame(DTM)

then get variable names:

     names(deltaYieldVector.df)

then use the variable of interest (altitude, I guess):

     DTM_class <- classIntervals(DTM.df$varOfInterest, n=5, style="fisher")

This should work; we were feeding a data.frame as input to classIntervals, whereas a variable is needed (e.g. one of the variables in the data.frame).

Let me know how it goes.

Kind regards,

Luigi

Hi,

thanks a lot. Now the process started, but the operation DTM_class ← classIntervals(DTM.df$varOfInterest, n=5, style=“fisher” takes too long; it was started by hours but does not end. Is it normal?

Salvatore

Il giorno 16/mag/2012, alle ore 22:04, Luigi Ponti ha scritto:

On 16/05/2012 21:10, Luigi Ponti wrote:

On 16/05/2012 20:55, Salvatore Mellino wrote:

I’m trying but I receive an error.

  1. In R I started the libraries spgarass6 and classInt
  1. I imported the DTM in R using DTM<- readRAST6(“DTM@mapset”, ignore.stderr=TRUE)
  1. I used the command DTM_class<- classIntervals(DTM, n=5, style=“fisher”) and I received the error

Error in classIntervals(DTM, n = 5, style = “fisher”) :

var is not numeric

I do not recall exactly, but it may be that classIntervals does not have a method for spatial data frames (i.e. the output of readRAST6). One thing I would try to get the breaks anyway, is a regular R data.frame, for example:

DTM.df ← as.data.frame(DTM)

DTM_class ← classIntervals(DTM.df, n=5, style=“fisher”)

I am replying to myself as I may have omitted part of the answer – my apologies. Start the same way:

DTM.df ← as.data.frame(DTM)

then get variable names:

names(deltaYieldVector.df)

then use the variable of interest (altitude, I guess):

DTM_class ← classIntervals(DTM.df$varOfInterest, n=5, style=“fisher”)

This should work; we were feeding a data.frame as input to classIntervals, whereas a variable is needed (e.g. one of the variables in the data.frame).

Let me know how it goes.

Kind regards,

Luigi

Since all Spatial*DataFrame objects behave link data.frame objects, you can
say:

DTM<- readRAST6("DTM@mapset", ignore.stderr=TRUE)
DTM_class <- classIntervals(DTM.df$varOfInterest, n=5, style="fisher")

However, if nrow(DTM) is large, you may find that the "natural breaks"
classifications take a lot of time, because the number of possible
alternative classifications to compare is very large. If you can reasonably
represent the distribution of your variable of interest by a sample, do
that, but add back the maximum and minimum values. Of course, in this case
you may get different class intervals for each sample.

In spearfish, I see:

library(spgrass6)
spear <- readRAST6(c("geology", "elevation.dem"), cat=c(TRUE, FALSE),

+ useGDAL=TRUE)

library(classInt)
system.time(CI0 <- classIntervals(spear$elevation.dem, n=5,
style="fisher"))

    user system elapsed
1151.136 0.053 1153.775

nrow(spear)

[1] 294978

CI0

style: fisher
  [1066,1221.5) [1221.5,1338.5) [1338.5,1472.5) [1472.5,1608.5)
[1608.5,1840]
          93972 60419 59562 45228
33136

set.seed(1)

system.time(CI1 <- classIntervals(c(sample(na.omit(spear$elevation.dem),
500),

+ range(na.omit(spear$elevation.dem))), n=5, style="fisher"))
   user system elapsed
  0.017 0.002 0.019

CI1

style: fisher
  [1066,1215.5) [1215.5,1322.5) [1322.5,1450) [1450,1591)
[1591,1840]
            159 109 86 78
70

table(findInterval(na.omit(spear$elevation.dem), CI0$brks,
all.inside=TRUE))

    1 2 3 4 5
88747 59755 57077 47726 39012

Or for more precision:

set.seed(1)
var_na <- na.omit(spear$elevation.dem)
mm_var <- range(var_na)
out <- vector(mode="list", length=100)
system.time(for (i in seq(along=out)) out[[i]] <-
classIntervals(c(sample(var_na, 500), mm_var), n=5, style="fisher"))

   user system elapsed
  0.312 0.000 0.316

mbrks <- sapply(out, "[[", "brks")
str(mbrks)

num [1:6, 1:100] 1066 1216 1322 1450 1591 ...

apply(mbrks, 1, mean)

[1] 1066.000 1218.920 1337.745 1473.920 1612.390 1840.000

which gives a result very like the one using the complete data set but
taking over three orders of magnitude longer, using 100 samples of 500
raster cells each.

Hope this helps,

Roger

Salvatore Mellino wrote

Hi,

thanks a lot. Now the process started, but the operation DTM_class <-
classIntervals(DTM.df$varOfInterest, n=5, style="fisher" takes too long;
it was started by hours but does not end. Is it normal?

Salvatore

Il giorno 16/mag/2012, alle ore 22:04, Luigi Ponti ha scritto:

On 16/05/2012 21:10, Luigi Ponti wrote:

On 16/05/2012 20:55, Salvatore Mellino wrote:

I'm trying but I receive an error.

1. In R I started the libraries spgarass6 and classInt
2. I imported the DTM in R using DTM<-
readRAST6("DTM@mapset", ignore.stderr=TRUE)
3. I used the command DTM_class<- classIntervals(DTM,
n=5, style="fisher") and I received the error

Error in classIntervals(DTM, n = 5, style = "fisher") :
  var is not numeric

I do not recall exactly, but it may be that classIntervals does not have
a method for spatial data frames (i.e. the output of readRAST6). One
thing I would try to get the breaks anyway, is a regular R data.frame,
for example:

DTM.df <- as.data.frame(DTM)
DTM_class <- classIntervals(DTM.df, n=5, style="fisher")

I am replying to myself as I may have omitted part of the answer -- my
apologies. Start the same way:

   DTM.df <- as.data.frame(DTM)

then get variable names:

   names(deltaYieldVector.df)

then use the variable of interest (altitude, I guess):

   DTM_class <- classIntervals(DTM.df$varOfInterest, n=5, style="fisher")

This should work; we were feeding a data.frame as input to
classIntervals, whereas a variable is needed (e.g. one of the variables
in the data.frame).

Let me know how it goes.

Kind regards,

Luigi

_______________________________________________
grass-user mailing list
grass-user@.osgeo
http://lists.osgeo.org/mailman/listinfo/grass-user

--
View this message in context: http://osgeo-org.1560.n6.nabble.com/Natural-breaks-classification-tp4975148p4975552.html
Sent from the Grass - Users mailing list archive at Nabble.com.

Dear all,

as i’m not very used used to work on vector files i don’t know how to manage a (probably) simple problem.

i’m working on some shapefiles to be imported in Grass, but before doing so i should modify the dbf table. When trying to open it (Ubuntu 12.04), i got the message

(in italian)

errore generale
errore generale di I/O

i wasn’t able to pen the table even by command line (sudo) nor found any hidden lock file in the directory.
Any suggestion?

Thanks a lot
Augusto

Is your error produced by libreOffice?

I can guess that the problem is because you are trying to open a .dbf file with libreoffice calc, that is the default application in Ubuntu 12.04 to open this sort of files.

To open .dbf files you should use libreoffice base. this application not is included in Ubuntu, you must to install the packages previously.

sudo apt-get install libreoffice-base

I hope this help you… I had the same problem when I updated my Ubuntu version.

Regards,

John Ortiz
Smithsonian Tropical Research Institute
Panama


Date: Fri, 18 May 2012 17:07:46 +0200
From: ricerca@palombini.net
To: grass-user@lists.osgeo.org
Subject: [GRASS-user] opening dbf tables

Dear all,

as i’m not very used used to work on vector files i don’t know how to manage a (probably) simple problem.

i’m working on some shapefiles to be imported in Grass, but before doing so i should modify the dbf table. When trying to open it (Ubuntu 12.04), i got the message

(in italian)

errore generale
errore generale di I/O

i wasn’t able to pen the table even by command line (sudo) nor found any hidden lock file in the directory.
Any suggestion?

Thanks a lot
Augusto

_______________________________________________ grass-user mailing list grass-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/grass-user

Why would you not want to open the dbh file in Libreoffice calc? It works fine for me. Unless you are working with datasets of over 1 miljoen records, the maximum rows in Calc.

I have not clue about the error message, but what if you try to open it directly from the Open file menu in LibreOffice (or any other software you prefer)?

An alternative option might be opening the shapefile in QGIS. In QGIS it is easy to make changes in your attribute table (the dbf file). And if you need to change columns (delete, move, rename), you can use the Table manager plugin for QGIS. As a bonus, you can directly import the file afterwards in your GRASS database from within QGIS using the GRASS toolbox in QGIS.

Cheers,

Paulo

On 18/05/2012 11:30, Roger Bivand wrote:

Since all Spatial*DataFrame objects behave link data.frame objects, you can
say:

DTM<- readRAST6("DTM@mapset", ignore.stderr=TRUE)
DTM_class<- classIntervals(DTM.df$varOfInterest, n=5, style="fisher")

I apologize about my imprecise statement that a Spatial*DataFrame object would require to be treated as.data.frame in order to be fed to classIntervals(). I recalled that some R functions complained about SpatialGridDataFrame objects but it may just be my faulty memory.

Thanks for a great post.

Kind regards,

Luigi

On 19/05/12 09:29, Paulo van Breugel wrote:

  Why would you not want to open the dbh file in Libreoffice calc? It
works fine for me. Unless you are working with datasets of over 1
miljoen records, the maximum rows in Calc.

I have not clue about the error message, but what if you try to open it
directly from the Open file menu in LibreOffice (or any other software
you prefer)?

ISTR that you cannot open / write dbf files from LibreOffice Calc if LibreOffice Base is not installed.

Moritz