[GRASS-dev] different region if looking using ctypes or g.region

Dear devs,

I got different results if I use ctypes or the module g.region, I wrote:

import ctypes 
import grass.lib.gis as libgis 
from grass import script 

SEP = '\n\n' 

c_region = ctypes.pointer(libgis.Cell_head()) 

## read current region 
libgis.G_get_set_window(c_region) 

print('original') 
print(c_region.contents.ns_res) 
print(c_region.contents.rows) 
print(c_region.contents.ew_res) 
print(c_region.contents.cols) 
print(SEP) 

## change resolution 
c_region.contents.ns_res = 100. 
c_region.contents.ew_res = 100. 

## adjust 
libgis.G_adjust_Cell_head(c_region, 0, 0) 

print('changed') 
print(c_region.contents.ns_res) 
print(c_region.contents.rows) 
print(c_region.contents.ew_res) 
print(c_region.contents.cols) 
print(SEP) 

## save current 
libgis.G_set_window(c_region) 

print(script.read_command("g.region", flags="p")) 
print(SEP) 

## read current region 
libgis.G_get_set_window(c_region) 
print('after') 
print(c_region.contents.ns_res) 
print(c_region.contents.rows) 
print(c_region.contents.ew_res) 
print(c_region.contents.cols) 
print(SEP)

The output is:

original
10.0
1350
10.0
1500

changed
100.0
135
100.0
150

projection: 99 (Lambert Conformal Conic)
zone:       0
datum:      nad83
ellipsoid:  a=6378137 es=0.006694380022900787
north:      228500
south:      215000
west:       630000
east:       645000
nsres:      10
ewres:      10
rows:       1350
cols:       1500
cells:      2025000

after
100.0
135
100.0
150

Any Ideas?

Pietro

Hi,

I’m glad you opened that topic. If you change region in C or using ctypes in Python, it is affecting only the current process. While g.region reads/writes the file with region but obviously will not change your process region in Python (when using ctypes). And then there are the temporary regions. To design an API for it is a challenge. I think I even wrote something about it to documentation but it needs to be completely rewritten to be actually readable.

Vaclav

···

On Thu, Feb 27, 2014 at 5:02 PM, Pietro <peter.zamb@gmail.com> wrote:

Dear devs,

I got different results if I use ctypes or the module g.region, I wrote:

import ctypes 
import grass.lib.gis as libgis 
from grass import script 

SEP = '\n\n' 

c_region = ctypes.pointer(libgis.Cell_head()) 

## read current region 
libgis.G_get_set_window(c_region) 

print('original') 
print(c_region.contents.ns_res) 
print(c_region.contents.rows) 
print(c_region.contents.ew_res) 
print(c_region.contents.cols) 
print(SEP) 

## change resolution 
c_region.contents.ns_res = 100. 
c_region.contents.ew_res = 100. 

## adjust 
libgis.G_adjust_Cell_head(c_region, 0, 0) 

print('changed') 
print(c_region.contents.ns_res) 
print(c_region.contents.rows) 
print(c_region.contents.ew_res) 
print(c_region.contents.cols) 
print(SEP) 

## save current 
libgis.G_set_window(c_region) 

print(script.read_command("g.region", flags="p")) 
print(SEP) 

## read current region 
libgis.G_get_set_window(c_region) 
print('after') 
print(c_region.contents.ns_res) 
print(c_region.contents.rows) 
print(c_region.contents.ew_res) 
print(c_region.contents.cols) 
print(SEP)

The output is:

original
10.0
1350
10.0
1500

changed
100.0
135
100.0
150

projection: 99 (Lambert Conformal Conic)
zone:       0
datum:      nad83
ellipsoid:  a=6378137 es=0.006694380022900787
north:      228500
south:      215000
west:       630000
east:       645000
nsres:      10
ewres:      10
rows:       1350
cols:       1500
cells:      2025000

after
100.0
135
100.0
150

Any Ideas?

Pietro


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

Pietro wrote:

libgis.G_set_window(c_region)

G_set_window() changes the region for the current process. To modify
the WIND file[1], use G_put_window().

[1] or the named region specified by the WIND_OVERRIDE environment
variable, if that is in effect.

--
Glynn Clements <glynn@gclements.plus.com>