Skip to content

Commit

Permalink
Merge pull request #2289 from avancinirodrigo/master
Browse files Browse the repository at this point in the history
Optimizing check and fix layer geometries
  • Loading branch information
pedro-andrade-inpe authored Mar 11, 2019
2 parents 5eb4e39 + 17b4dfe commit 5dc9be1
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 45 deletions.
1 change: 1 addition & 0 deletions packages/gis/data/test/cs_cerrado_clip_29101.cpg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UTF-8
Binary file added packages/gis/data/test/cs_cerrado_clip_29101.dbf
Binary file not shown.
1 change: 1 addition & 0 deletions packages/gis/data/test/cs_cerrado_clip_29101.prj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PROJCS["SAD69_Brazil_Polyconic",GEOGCS["GCS_SAD69",DATUM["D_South_American_1969",SPHEROID["GRS_1967_Truncated",6378160,298.25]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Polyconic"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-54],PARAMETER["false_easting",5000000],PARAMETER["false_northing",10000000],UNIT["Meter",1]]
1 change: 1 addition & 0 deletions packages/gis/data/test/cs_cerrado_clip_29101.qpj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PROJCS["SAD69 / Brazil Polyconic",GEOGCS["SAD69",DATUM["South_American_Datum_1969",SPHEROID["GRS 1967 Modified",6378160,298.25,AUTHORITY["EPSG","7050"]],TOWGS84[-66.87,4.37,-38.52,0,0,0,0],AUTHORITY["EPSG","6618"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4618"]],PROJECTION["Polyconic"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-54],PARAMETER["false_easting",5000000],PARAMETER["false_northing",10000000],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["X",EAST],AXIS["Y",NORTH],AUTHORITY["EPSG","29101"]]
Binary file added packages/gis/data/test/cs_cerrado_clip_29101.shp
Binary file not shown.
Binary file added packages/gis/data/test/cs_cerrado_clip_29101.shx
Binary file not shown.
Binary file not shown.
62 changes: 18 additions & 44 deletions packages/gis/lua/TerraLib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -722,9 +722,9 @@ local function createProgressViewer(msg)
end
end

local function finalizeProgressViewer(viewerId)
local function finalizeProgressViewer() --(viewerId)
if progress then
binding.te.common.ProgressManager.getInstance():removeViewer(viewerId) -- SKIP
binding.te.common.ProgressManager.getInstance():clearAll() -- SKIP TODO: removeViewer(viewerId) seems not working
end
end

Expand Down Expand Up @@ -3630,51 +3630,25 @@ TerraLib_ = {
loadProject(project, project.file)

local layer = project.layers[layerName]
local dsInfo = binding.te.da.DataSourceInfoManager.getInstance():getDsInfo(layer:getDataSourceId())
local ds = makeAndOpenDataSource(dsInfo:getConnInfo(), dsInfo:getType())
local dseName = layer:getDataSetName()
local dse = ds:getDataSet(dseName)
local dst = ds:getDataSetType(dseName)
local oids = binding.te.da.ObjectIdSet():clone()

local pk = dst:getPrimaryKey()
local pkName
if pk then
pkName = pk:getPropertyName(0)
local pkPos = dst:getPropertyPosition(pkName)
local pkType = pk:getProperty(pkPos):getType()
oids:addProperty(pkName, pkPos, pkType)
end
local viewerId = createProgressViewer("Checking '"..layerName.."' geometries")
local geomError = binding.CheckLayerGeometries(layer)
finalizeProgressViewer(viewerId)

if dst:hasGeom() then
local gp = binding.GetFirstGeomProperty(dst)
local gname = gp:getName()
local gpos = getPropertyPosition(dse, gname)

dse:moveBeforeFirst()
while dse:moveNext() do
local geom = dse:getGeom(gpos)
local err = binding.TopologyValidationError()
if not binding.CheckValidity(geom, err) then
if pk then
local oid = binding.te.da.ObjectId():clone()
oid:addValue(dse:getValue(pkName):clone())
oids:add(oid)
table.insert(problems, {pk = {name = pkName, value = dse:getAsString(pkName)},
error = err.m_message,
coord = {x = err.m_coordinate:getX(), y = err.m_coordinate:getY()}})
else
table.insert(problems, {error = err.m_message, --SKIP
coord = {x = err.m_coordinate:getX(), y = err.m_coordinate:getY()}}) --SKIP
end
end
end
local problemsInfo = geomError:getProblems()

if fix and (#problems > 0) then
local viewerId = createProgressViewer("Fixing '"..layerName.."' geometries")
fixErrorMsg = binding.te.vp.MakeGeometryValid.makeValid(layer, dseName, oids)
finalizeProgressViewer(viewerId)
end
for i = 0, getn(problemsInfo) - 1 do
local problem = problemsInfo[i]
table.insert(problems, {pk = {name = problem.propertyName, value = problem.propertyValue},
error = problem.errorMsg,
coord = {x = problem.coordX, y = problem.coordY}})
end

if fix and (#problems > 0) then
viewerId = createProgressViewer("Fixing '"..layerName.."' geometries")
fixErrorMsg = binding.te.vp.MakeGeometryValid.makeValid(layer,
layer:getDataSetName(), geomError.objectIdSet)
finalizeProgressViewer(viewerId)
end

releaseProject(project)
Expand Down
2 changes: 1 addition & 1 deletion packages/gis/tests/functional/basic/Project.lua
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ return {
directory = packageInfo("gis").data.."test"
}

unitTest:assertEquals(getn(proj.layers), 23)
unitTest:assertEquals(getn(proj.layers), 25)
file:deleteIfExists()

local version = ""
Expand Down
51 changes: 51 additions & 0 deletions packages/gis/tests/tif/basic/Layer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -552,8 +552,59 @@ return {
proj.file:delete()
end

local coverageWithDummy = function()
local csFile = filePath("test/cs_cerrado_clip_29101.shp", "gis")
csFile:copy(currentDir())

local proj = Project{
file = "layer_fill_shp_basic.tview",
clean = true
}

local prodes = Layer {
project = proj,
name = "Prodes",
file = filePath("test/prodes_cerrado_clip_nodata_100_proj_29101.tif", "gis"),
epsg = 29101
}

local cl = Layer {
project = proj,
name = "Cells",
file = "cs_cerrado_clip_29101.shp"
}

cl:fill{
operation = "coverage",
layer = prodes.name,
attribute = "cov1",
progress = false
}

cl:fill{
operation = "coverage",
layer = prodes.name,
attribute = "cov2",
dummy = 1000,
progress = false
}

local cs = CellularSpace{
project = proj,
layer = cl.name
}

local cell = cs:get("3")
unitTest:assertNil(cell.cov1_100)
unitTest:assertEquals(cell.cov2_100, 0.65175164758932, 1e-14)

cl:delete()
proj.file:delete()
end

unitTest:assert(allSupportedOperation)
unitTest:assert(coverageTotalArea)
unitTest:assert(coverageWithDummy)
end,
representation = function(unitTest)
local projName = "layer_fill_tiff_repr.tview"
Expand Down
82 changes: 82 additions & 0 deletions packages/gis/tests/tif/basic/TerraLib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,89 @@ return {
proj.file:delete()
end

local coverageWithDummy = function()
local proj = {
file = "attrfill-tif-basic.tview",
title = "TerraLib Tests",
author = "Avancini Rodrigo"
}

File(proj.file):deleteIfExists()
TerraLib().createProject(proj, {})

local csFile = filePath("test/cs_cerrado_clip_29101.shp", "gis")
csFile:copy(currentDir())

local l1Name = "Cells"
local l1File = File("cs_cerrado_clip_29101.shp")
TerraLib().addShpLayer(proj, l1Name, l1File)

local l1Info = TerraLib().getLayerInfo(proj, l1Name)

local l2Name = "Prodes"
local l2File = filePath("test/prodes_cerrado_clip_nodata_100_proj_29101.tif", "gis")
TerraLib().addGdalLayer(proj, l2Name, l2File, l1Info.srid)

local l3Name = "cells_cov_dummy"
local l3File = File(l3Name..".shp")
local operation = "coverage"
local attribute = "cov"
local select = 0
local default = 0
local pixel = false
local nodata = TerraLib().getDummyValue(proj, l2Name, 0)

l3File:deleteIfExists()
TerraLib().attributeFill{
project = proj,
from = l2Name,
to = l1Name,
out = l3Name,
attribute = attribute,
operation = operation,
select = select,
area = area,
default = default,
repr = "raster",
pixel = pixel,
nodata = nodata
}

local l3Set = TerraLib().getDataSet{project = proj, layer = l3Name}

unitTest:assertNil(l3Set[0].cov_100)

local l4Name = "cells_cov_nodummy"
local l4File = File(l4Name..".shp")

l4File:deleteIfExists()
TerraLib().attributeFill{
project = proj,
from = l2Name,
to = l1Name,
out = l4Name,
attribute = attribute,
operation = operation,
select = select,
area = area,
default = default,
repr = "raster",
pixel = pixel,
nodata = 1000
}

local l4Set = TerraLib().getDataSet{project = proj, layer = l4Name}

unitTest:assertEquals(l4Set[0].cov_100, 0.90114464099896, 1e-14)

l1File:delete()
l3File:delete()
l4File:delete()
proj.file:delete()
end

unitTest:assert(coverageTotalArea)
unitTest:assert(coverageWithDummy)
end
}

0 comments on commit 5dc9be1

Please sign in to comment.