TerraME / terrame

TerraME is a programming environment for spatial dynamical modelling
www.terrame.org
GNU Lesser General Public License v3.0
36 stars 13 forks source link

Load CellularSpace from Tif, NetCdf, and ASCII files #1207

Closed pedro-andrade-inpe closed 8 years ago

pedro-andrade-inpe commented 8 years ago

Allow loading CellularSpaces from Tif, NetCdf, and ASCII files. Depends on #993 and #992.

hguerra commented 8 years ago

The script below uses Tif file to create a CellularSpace.

layerName1 = "Tif_cbers-rgb342"
filePath1 = filePath("cbers_rgb342_crop1.tif", "terralib")

layer = terralib.Layer{
    project = proj,
    name = layerName1,
    file = filePath1
}

cs = CellularSpace{
    project = projName,
    layer = layerName1
}

print("layer:representation "..layer:representation())
print("bands "..layer:bands())
print("#cs.cells: "..#cs.cells)

forEachCell(cs, function(cell)
    print("Type of cell.raster: "..type(cell.raster).."\n")
    print(_Gtme.tostring(cell))
end)

output:

layer:representation raster
bands 3.0
#cs.cells: 1
Type of cell.raster: string

cObj_   userdata
past    vector of size 0
raster  string [
Raster Name......: 
Number of Columns: 875
Number of Rows...: 1009
Number of Bands..: 3
SRID.............: 29182
Resolution in X..: 20
Resolution in Y..: 20
Extent UR........: 546480, 7.94528e+006
Extent LL........: 528980, 7.9251e+006

Band: 0 
  Min Values...: (28,0)
  Max Values...: (118,2.22507e-308)
  Mean Values..: (42.2619,0)
  Std Values...: (7.07111,0)
  Gain values..: (1,0)
  Offset values: (0,0)

Band: 1 
  Min Values...: (35,0)
  Max Values...: (255,2.22507e-308)
  Mean Values..: (154.537,0)
  Std Values...: (38.2849,0)
  Gain values..: (1,0)
  Offset values: (0,0)

Band: 2 
  Min Values...: (50,0)
  Max Values...: (234,2.22507e-308)
  Mean Values..: (76.6987,0)
  Std Values...: (11.1677,0)
  Gain values..: (1,0)
  Offset values: (0,0)

]
x       number [0]
y       number [0]
hguerra commented 8 years ago

An error occurs on Linux server when unit test contains the code below: packages/base/tests/functional/basics/CellularSpace.lua

-- Tif
projName = "tif_cellspace.tview"
title = "Tif Cellular Space"

proj = terralib.Project{
    file = projName,
    clean = true,
    author = author,
    title = title
}

layerName1 = "Tif_PRODES"
filePath1 = filePath("PRODES_5KM.tif", "terralib")

layer = terralib.Layer{
    project = proj,
    name = layerName1,
    file = filePath1
}

cs = CellularSpace{
    project = projName,
    layer = layerName1
}

unitTest:assertEquals(projName, cs.project.file)
unitTest:assertType(cs.layer, "Layer") 

unitTest:assertEquals(proj.title, title) 
unitTest:assertEquals(proj.author, author) 

unitTest:assertEquals(layer.source, "tif") 
unitTest:assertEquals(layer.file, filePath1) 
unitTest:assertEquals(#cs.cells, 1) 

cs = CellularSpace{
    file = filePath1
}

unitTest:assertEquals(#cs.cells, 1) 

forEachCell(cs, function(c)
    unitTest:assertNotNil(c.x) 
    unitTest:assertNotNil(c.y) 
    unitTest:assertNotNil(c.raster) 

    unitTest:assertNil(c.geom) 
    unitTest:assertNil(c.OGR_GEOMETRY) 
end)

if isFile(projName) then
    rmFile(projName)
end

-- NetCDF
projName = "nc_cellspace.tview"
title = "NC Cellular Space"

proj = terralib.Project{
    file = projName,
    clean = true,
    author = author,
    title = title
}

layerName1 = "NC_vegtype2000"
filePath1 = filePath("vegtype_2000.nc", "terralib")

layer = terralib.Layer{
    project = proj,
    name = layerName1,
    file = filePath1
}

cs = CellularSpace{
    project = projName,
    layer = layerName1
}

unitTest:assertEquals(projName, cs.project.file) 
unitTest:assertType(cs.layer, "Layer") 

unitTest:assertEquals(proj.title, title) 
unitTest:assertEquals(proj.author, author) 

unitTest:assertEquals(layer.source, "nc") 
unitTest:assertEquals(layer.file, filePath1) 
unitTest:assertEquals(#cs.cells, 1) 

cs = CellularSpace{
    file = filePath1
}

unitTest:assertEquals(#cs.cells, 1) 

forEachCell(cs, function(c)
    unitTest:assertNotNil(c.x) 
    unitTest:assertNotNil(c.y) 
    unitTest:assertNotNil(c.raster) 

    unitTest:assertNil(c.geom) 
    unitTest:assertNil(c.OGR_GEOMETRY) 
end)

if isFile(projName) then
    rmFile(projName)
end

-- ASC
projName = "asc_cellspace.tview"
title = "Asc Cellular Space"

proj = terralib.Project{
    file = projName,
    clean = true,
    author = author,
    title = title
}

layerName1 = "ASC_biomassa-manaus"
filePath1 = filePath("biomassa-manaus.asc", "terralib")

layer = terralib.Layer{
    project = proj,
    name = layerName1,
    file = filePath1
}

cs = CellularSpace{
    project = projName,
    layer = layerName1
}

unitTest:assertEquals(projName, cs.project.file) 
unitTest:assertType(cs.layer, "Layer") 

unitTest:assertEquals(proj.title, title) 
unitTest:assertEquals(proj.author, author) 

unitTest:assertEquals(layer.source, "asc") 
unitTest:assertEquals(layer.file, filePath1) 
unitTest:assertEquals(#cs.cells, 1) 

cs = CellularSpace{
    file = filePath1
}

unitTest:assertEquals(#cs.cells, 1) 

forEachCell(cs, function(c)
    unitTest:assertNotNil(c.x) 
    unitTest:assertNotNil(c.y) 
    unitTest:assertNotNil(c.raster) 

    unitTest:assertNil(c.geom)
    unitTest:assertNil(c.OGR_GEOMETRY)
end)

if isFile(projName) then
    rmFile(projName)
end

output:

Testing tests/functional/basics/CellularSpace.lua
Testing add
Testing CellularSpace
/home/jenkins/Configs/terrame/status/request.sh: line 80: 21795 Segmentation fault      (core dumped) terrame -color $TERRAME_PACKAGE $TERRAME_RUN_TYPE config.lua 2> /dev/null
Sending build status to GitHub
Removing /home/jenkins/Documents/f8eef72f69a0157b53c5f14d548fdbc5d9e13391
Build step 'Conditional steps (multiple)' marked build as failure
Xvfb stopping
Finished: FAILURE