swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Swarm-Support] Input data structure


From: Marcus G. Daniels
Subject: Re: [Swarm-Support] Input data structure
Date: Tue, 23 Mar 2004 14:12:20 -0700
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7b) Gecko/20040316

Eric McLeskey wrote:

I am wondering if it is possible to import spatial data with multiple
attributes per node into a SWARM model?

One way to do it is to load tabular data into R (http://www.r-project.org), reorganize it as desired, and then save a HDF5 file. Here's an example of how this can be done. getX and getY are defined in the file at the end -- the idea is it converts township/range/section on a map into a simple x/y offset (in the variables xpos and ypos). If you have X and Y positions directly in the data you can assign those vectors to xpos and ypos and ignore that. The `row.names' assignment is needed to tell Swarm what the different rows in the table correspond to in X/Y space. It just takes an X vector and a Y vector and paste them together into the form "X,Y", e.g. "0,0", "1,0", etc., filling out the Discrete2d space in Swarm as desired. The two attribute asignments for "type" and "component-type" tell the Swarm HDF5 loader how to associate the data with a type in Swarm, e.g. Discrete2d for the collection and the class "SpatialDensity" for the cells. The `names' assignment is to associate the fields in the data with instance variables. The `gsub' is to tweak the syntax from "foo.bar" to "foo_bar", the latter being more appropriate for instance variable names.

In Swarm, the load of the data can be done using this:

 archiver = [[[HDF5Archiver createBegin: self]
               setPath: "spatialTbDensity.hdf"]
              createEnd];

 ds = [archiver getWithZone: self key: "spatialDensity"];

Once you got the space and request some object out of it using -getObjectAtX:Y:, then you can use VarProbes to get a the fields in the data even if you haven't declared the component-type class.

Here's the R code:

library(hdf5)

source("getXY.R")
spatialDensity <- read.table("spatialDensity.txt", header=TRUE, sep="\t")

xpos <- getX (spatialDensity$RNG, spatialDensity$SEC)
ypos <- getY (spatialDensity$TWN, spatialDensity$SEC)

row.names(spatialDensity) <- paste(xpos,ypos,sep=",")

attr(spatialDensity,"type") <- "Discrete2d"

names(spatialDensity) <- gsub("\\.", "_", names(spatialDensity))

attr(spatialDensity,"component-type") <- "SpatialDensity"

hdf5save("spatialTbDensity.hdf", "spatialDensity")


-----
getXY.R
-----

xvec <- c(5,4,3,2,1,0,
         0,1,2,3,4,5,
         5,4,3,2,1,0,
         0,1,2,3,4,5,
         5,4,3,2,1,0,
         0,1,2,3,4,5)

yvec <- c(5,5,5,5,5,5,
         4,4,4,4,4,4,
         3,3,3,3,3,3,
         2,2,2,2,2,2,
         1,1,1,1,1,1,
         0,0,0,0,0,0)

getX <- function (rng, sec) {
 (rng - 1) * 6 + xvec[sec]
}

getY <- function (twn, sec) {
 (twn - 25) * 6 + yvec[sec]
}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]