Skip to content

Commit

Permalink
Working on #8.
Browse files Browse the repository at this point in the history
Added a get_WKB/set_WKB pair to cmf::upslope::Cell
using the new type cmf::bytestring, a typedef of std::string. time.i
sets swig typemaps to translate bytestring to PyBytes. If shapely
is present __init__.py adds dynamically a geometry property to the
cmf.Cell class.
  • Loading branch information
philippkraft committed Jan 2, 2018
1 parent acb0f46 commit 7b03912
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ PROJECT_NAME = cmf
# could be handy for archiving the generated documentation or if some version
# control system is used.

PROJECT_NUMBER = 1.1.0
PROJECT_NUMBER = 1.1.1a1

# Using the PROJECT_BRIEF tag one can provide an optional one line description
# for a project that appears at the top of each page and should give viewer a
Expand Down
30 changes: 29 additions & 1 deletion cmf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
from .describe import describe
from .stopwatch import StopWatch

__version__ = '1.1.0'
__version__ = '1.1.1a1'

from .cmf_core import connect_cells_with_flux as __ccwf


def connect_cells_with_flux(cells, connection, start_at_layer=0):
"""Connects all cells in cells (sequence or generator) with a flux connection
connection is an subclass of cmf.FluxConnection which exposes the cell_connector callable
Expand All @@ -37,3 +38,30 @@ def connect_cells_with_flux(cells, connection, start_at_layer=0):
else:
raise TypeError("flux_connection does not implement the cell_connector protocol")


def __add_geometry_property():

# If shapely is available, add a geometry property to Cell, that consists of a shapely geometry
try:
from shapely.wkb import loads as __load_wkb
from shapely.geos import WKBReadingError as WKBReadingError
def get_geometry(c):
try:
return __load_wkb(c.get_WKB())
except (TypeError, WKBReadingError):
return None

def set_geometry(c, geom):
c.set_WKB(geom.wkb)

def del_geometry(c):
c.set_WKB(b'')

prop = property(get_geometry, set_geometry, del_geometry, 'Geometry of the cell')
setattr(Cell, 'geometry', prop)

except ImportError:
__load_wkb = None


__add_geometry_property()
12 changes: 12 additions & 0 deletions cmf/cmf_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7360,6 +7360,16 @@ def leave_wetness(self, *args, **kwargs):

Id = _swig_property(_cmf_core.Cell_Id_get, _cmf_core.Cell_Id_set)

def get_WKB(self, *args, **kwargs):
"""get_WKB(Cell self) -> cmf::bytestring"""
return _cmf_core.Cell_get_WKB(self, *args, **kwargs)


def set_WKB(self, *args, **kwargs):
"""set_WKB(Cell self, cmf::bytestring wkb)"""
return _cmf_core.Cell_set_WKB(self, *args, **kwargs)


def get_weather(self, *args, **kwargs):
"""
get_weather(Cell self, Time t) -> Weather
Expand Down Expand Up @@ -7553,6 +7563,8 @@ def __repr__(self):
Cell.surface_water_coverage = new_instancemethod(_cmf_core.Cell_surface_water_coverage, None, Cell)
Cell.heat_flux = new_instancemethod(_cmf_core.Cell_heat_flux, None, Cell)
Cell.leave_wetness = new_instancemethod(_cmf_core.Cell_leave_wetness, None, Cell)
Cell.get_WKB = new_instancemethod(_cmf_core.Cell_get_WKB, None, Cell)
Cell.set_WKB = new_instancemethod(_cmf_core.Cell_set_WKB, None, Cell)
Cell.get_weather = new_instancemethod(_cmf_core.Cell_get_weather, None, Cell)
Cell.layer_count = new_instancemethod(_cmf_core.Cell_layer_count, None, Cell)
Cell.get_layer = new_instancemethod(_cmf_core.Cell_get_layer, None, Cell)
Expand Down
2 changes: 1 addition & 1 deletion cmf/cmf_core_src/cmf.i
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
%feature("autodoc","1");

// Usage for automated downcast
%include factory.i
%include "factory.i"

// Include typemaps for STL
%include "std_string.i"
Expand Down
96 changes: 91 additions & 5 deletions cmf/cmf_core_src/cmf_wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3206,11 +3206,12 @@ SWIG_Python_NonDynamicSetAttr(PyObject *obj, PyObject *name, PyObject *value) {
#define SWIGTYPE_p_std__shared_ptrT_cmf__water__waterbalance_integrator_t swig_types[194]
#define SWIGTYPE_p_std__shared_ptrT_std__shared_ptrT_cmf__atmosphere__ConstantRainSource_t_t swig_types[195]
#define SWIGTYPE_p_std__shared_ptrT_std__shared_ptrT_cmf__atmosphere__RainfallStationReference_t_t swig_types[196]
#define SWIGTYPE_p_std__vectorT_std__shared_ptrT_cmf__water__WaterStorage_t_t swig_types[197]
#define SWIGTYPE_p_std__vectorT_std__shared_ptrT_cmf__water__flux_connection_t_t__const_iterator swig_types[198]
#define SWIGTYPE_p_std__vectorT_std__shared_ptrT_cmf__water__flux_connection_t_t__iterator swig_types[199]
static swig_type_info *swig_types[201];
static swig_module_info swig_module = {swig_types, 200, 0, 0, 0, 0};
#define SWIGTYPE_p_std__string swig_types[197]
#define SWIGTYPE_p_std__vectorT_std__shared_ptrT_cmf__water__WaterStorage_t_t swig_types[198]
#define SWIGTYPE_p_std__vectorT_std__shared_ptrT_cmf__water__flux_connection_t_t__const_iterator swig_types[199]
#define SWIGTYPE_p_std__vectorT_std__shared_ptrT_cmf__water__flux_connection_t_t__iterator swig_types[200]
static swig_type_info *swig_types[202];
static swig_module_info swig_module = {swig_types, 201, 0, 0, 0, 0};
#define SWIG_TypeQuery(name) SWIG_TypeQueryModule(&swig_module, &swig_module, name)
#define SWIG_MangledTypeQuery(name) SWIG_MangledTypeQueryModule(&swig_module, &swig_module, name)

Expand Down Expand Up @@ -45495,6 +45496,85 @@ SWIGINTERN PyObject *_wrap_Cell_Id_get(PyObject *SWIGUNUSEDPARM(self), PyObject
}


SWIGINTERN PyObject *_wrap_Cell_get_WKB(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
PyObject *resultobj = 0;
cmf::upslope::Cell *arg1 = (cmf::upslope::Cell *) 0 ;
void *argp1 = 0 ;
int res1 = 0 ;
PyObject *swig_obj[1] ;
cmf::bytestring result;

if (!args) SWIG_fail;
swig_obj[0] = args;
res1 = SWIG_ConvertPtr(swig_obj[0], &argp1,SWIGTYPE_p_cmf__upslope__Cell, 0 | 0 );
if (!SWIG_IsOK(res1)) {
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Cell_get_WKB" "', argument " "1"" of type '" "cmf::upslope::Cell const *""'");
}
arg1 = reinterpret_cast< cmf::upslope::Cell * >(argp1);
{
try {
result = ((cmf::upslope::Cell const *)arg1)->get_WKB();
} catch (const std::out_of_range& e) {
SWIG_exception(SWIG_IndexError, e.what());
} catch (const std::exception& e) {
SWIG_exception(SWIG_RuntimeError, e.what());
}

}
{
// Converting cmf::bytestring to PyBytes
resultobj = PyBytes_FromStringAndSize((&result)->c_str(), (&result)->size());
}
return resultobj;
fail:
return NULL;
}


SWIGINTERN PyObject *_wrap_Cell_set_WKB(PyObject *SWIGUNUSEDPARM(self), PyObject *args, PyObject *kwargs) {
PyObject *resultobj = 0;
cmf::upslope::Cell *arg1 = (cmf::upslope::Cell *) 0 ;
cmf::bytestring arg2 ;
void *argp1 = 0 ;
int res1 = 0 ;
PyObject * obj0 = 0 ;
PyObject * obj1 = 0 ;
char * kwnames[] = {
(char *) "self",(char *) "wkb", NULL
};

if (!PyArg_ParseTupleAndKeywords(args,kwargs,(char *)"OO:Cell_set_WKB",kwnames,&obj0,&obj1)) SWIG_fail;
res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_cmf__upslope__Cell, 0 | 0 );
if (!SWIG_IsOK(res1)) {
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Cell_set_WKB" "', argument " "1"" of type '" "cmf::upslope::Cell *""'");
}
arg1 = reinterpret_cast< cmf::upslope::Cell * >(argp1);
{
// Convert PyBytes to cmf::bytestring
if (PyBytes_Check(obj1)) {
Py_ssize_t size = PyBytes_Size(obj1);
arg2 = cmf::bytestring(PyBytes_AsString(obj1), size);
} else {
SWIG_exception_fail(SWIG_TypeError,"WKB expects byte string");
}
}
{
try {
(arg1)->set_WKB(arg2);
} catch (const std::out_of_range& e) {
SWIG_exception(SWIG_IndexError, e.what());
} catch (const std::exception& e) {
SWIG_exception(SWIG_RuntimeError, e.what());
}

}
resultobj = SWIG_Py_Void();
return resultobj;
fail:
return NULL;
}


SWIGINTERN PyObject *_wrap_Cell_get_weather(PyObject *SWIGUNUSEDPARM(self), PyObject *args, PyObject *kwargs) {
PyObject *resultobj = 0;
cmf::upslope::Cell *arg1 = (cmf::upslope::Cell *) 0 ;
Expand Down Expand Up @@ -78595,6 +78675,8 @@ static PyMethodDef SwigMethods[] = {
""},
{ (char *)"Cell_Id_set", _wrap_Cell_Id_set, METH_VARARGS, (char *)"Cell_Id_set(Cell self, ptrdiff_t Id)"},
{ (char *)"Cell_Id_get", (PyCFunction)_wrap_Cell_Id_get, METH_O, (char *)"Cell_Id_get(Cell self) -> ptrdiff_t"},
{ (char *)"Cell_get_WKB", (PyCFunction)_wrap_Cell_get_WKB, METH_O, (char *)"Cell_get_WKB(Cell self) -> cmf::bytestring"},
{ (char *)"Cell_set_WKB", (PyCFunction) _wrap_Cell_set_WKB, METH_VARARGS | METH_KEYWORDS, (char *)"Cell_set_WKB(Cell self, cmf::bytestring wkb)"},
{ (char *)"Cell_get_weather", (PyCFunction) _wrap_Cell_get_weather, METH_VARARGS | METH_KEYWORDS, (char *)"\n"
"Cell_get_weather(Cell self, Time t) -> Weather\n"
"\n"
Expand Down Expand Up @@ -82405,6 +82487,7 @@ static swig_type_info _swigt__p_std__shared_ptrT_cmf__water__flux_node_t = {"_p_
static swig_type_info _swigt__p_std__shared_ptrT_cmf__water__waterbalance_integrator_t = {"_p_std__shared_ptrT_cmf__water__waterbalance_integrator_t", "std::shared_ptr< cmf::water::waterbalance_integrator > *", 0, 0, (void*)0, 0};
static swig_type_info _swigt__p_std__shared_ptrT_std__shared_ptrT_cmf__atmosphere__ConstantRainSource_t_t = {"_p_std__shared_ptrT_std__shared_ptrT_cmf__atmosphere__ConstantRainSource_t_t", "std::shared_ptr< std::shared_ptr< cmf::atmosphere::ConstantRainSource > > *|std::shared_ptr< cmf::atmosphere::ConstantRainSource::ptr > *", 0, 0, (void*)0, 0};
static swig_type_info _swigt__p_std__shared_ptrT_std__shared_ptrT_cmf__atmosphere__RainfallStationReference_t_t = {"_p_std__shared_ptrT_std__shared_ptrT_cmf__atmosphere__RainfallStationReference_t_t", "std::shared_ptr< std::shared_ptr< cmf::atmosphere::RainfallStationReference > > *|std::shared_ptr< cmf::atmosphere::RainfallStationReference::ptr > *", 0, 0, (void*)0, 0};
static swig_type_info _swigt__p_std__string = {"_p_std__string", "std::string *|cmf::bytestring *", 0, 0, (void*)0, 0};
static swig_type_info _swigt__p_std__vectorT_std__shared_ptrT_cmf__water__WaterStorage_t_t = {"_p_std__vectorT_std__shared_ptrT_cmf__water__WaterStorage_t_t", "std::vector< std::shared_ptr< cmf::water::WaterStorage > > *|cmf::water::storage_vector *", 0, 0, (void*)0, 0};
static swig_type_info _swigt__p_std__vectorT_std__shared_ptrT_cmf__water__flux_connection_t_t__const_iterator = {"_p_std__vectorT_std__shared_ptrT_cmf__water__flux_connection_t_t__const_iterator", "cmf::water::connection_list::const_iterator *|std::vector< std::shared_ptr< cmf::water::flux_connection > >::const_iterator *", 0, 0, (void*)0, 0};
static swig_type_info _swigt__p_std__vectorT_std__shared_ptrT_cmf__water__flux_connection_t_t__iterator = {"_p_std__vectorT_std__shared_ptrT_cmf__water__flux_connection_t_t__iterator", "cmf::water::connection_list::iterator *|std::vector< std::shared_ptr< cmf::water::flux_connection > >::iterator *", 0, 0, (void*)0, 0};
Expand Down Expand Up @@ -82607,6 +82690,7 @@ static swig_type_info *swig_type_initial[] = {
&_swigt__p_std__shared_ptrT_cmf__water__waterbalance_integrator_t,
&_swigt__p_std__shared_ptrT_std__shared_ptrT_cmf__atmosphere__ConstantRainSource_t_t,
&_swigt__p_std__shared_ptrT_std__shared_ptrT_cmf__atmosphere__RainfallStationReference_t_t,
&_swigt__p_std__string,
&_swigt__p_std__vectorT_std__shared_ptrT_cmf__water__WaterStorage_t_t,
&_swigt__p_std__vectorT_std__shared_ptrT_cmf__water__flux_connection_t_t__const_iterator,
&_swigt__p_std__vectorT_std__shared_ptrT_cmf__water__flux_connection_t_t__iterator,
Expand Down Expand Up @@ -82809,6 +82893,7 @@ static swig_cast_info _swigc__p_std__shared_ptrT_cmf__water__flux_node_t[] = {
static swig_cast_info _swigc__p_std__shared_ptrT_cmf__water__waterbalance_integrator_t[] = { {&_swigt__p_std__shared_ptrT_cmf__water__waterbalance_integrator_t, 0, 0, 0},{0, 0, 0, 0}};
static swig_cast_info _swigc__p_std__shared_ptrT_std__shared_ptrT_cmf__atmosphere__ConstantRainSource_t_t[] = { {&_swigt__p_std__shared_ptrT_std__shared_ptrT_cmf__atmosphere__ConstantRainSource_t_t, 0, 0, 0},{0, 0, 0, 0}};
static swig_cast_info _swigc__p_std__shared_ptrT_std__shared_ptrT_cmf__atmosphere__RainfallStationReference_t_t[] = { {&_swigt__p_std__shared_ptrT_std__shared_ptrT_cmf__atmosphere__RainfallStationReference_t_t, 0, 0, 0},{0, 0, 0, 0}};
static swig_cast_info _swigc__p_std__string[] = { {&_swigt__p_std__string, 0, 0, 0},{0, 0, 0, 0}};
static swig_cast_info _swigc__p_std__vectorT_std__shared_ptrT_cmf__water__WaterStorage_t_t[] = { {&_swigt__p_std__vectorT_std__shared_ptrT_cmf__water__WaterStorage_t_t, 0, 0, 0},{0, 0, 0, 0}};
static swig_cast_info _swigc__p_std__vectorT_std__shared_ptrT_cmf__water__flux_connection_t_t__const_iterator[] = { {&_swigt__p_std__vectorT_std__shared_ptrT_cmf__water__flux_connection_t_t__const_iterator, 0, 0, 0},{0, 0, 0, 0}};
static swig_cast_info _swigc__p_std__vectorT_std__shared_ptrT_cmf__water__flux_connection_t_t__iterator[] = { {&_swigt__p_std__vectorT_std__shared_ptrT_cmf__water__flux_connection_t_t__iterator, 0, 0, 0},{0, 0, 0, 0}};
Expand Down Expand Up @@ -83011,6 +83096,7 @@ static swig_cast_info *swig_cast_initial[] = {
_swigc__p_std__shared_ptrT_cmf__water__waterbalance_integrator_t,
_swigc__p_std__shared_ptrT_std__shared_ptrT_cmf__atmosphere__ConstantRainSource_t_t,
_swigc__p_std__shared_ptrT_std__shared_ptrT_cmf__atmosphere__RainfallStationReference_t_t,
_swigc__p_std__string,
_swigc__p_std__vectorT_std__shared_ptrT_cmf__water__WaterStorage_t_t,
_swigc__p_std__vectorT_std__shared_ptrT_cmf__water__flux_connection_t_t__const_iterator,
_swigc__p_std__vectorT_std__shared_ptrT_cmf__water__flux_connection_t_t__iterator,
Expand Down
5 changes: 4 additions & 1 deletion cmf/cmf_core_src/math/real.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@
real sign(real x);
real square(real x);


const real Pi=3.141592654;

namespace cmf {
typedef std::string bytestring;
}


#endif // real_h__
13 changes: 13 additions & 0 deletions cmf/cmf_core_src/math/time.i
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ static bool check_time(PyObject* dt) {
%typemap(typecheck,precedence=100) const cmf::math::Time &{
$1 = check_time($input); // typecheck const cmf::math::Time &
}
%typemap(out) cmf::bytestring {
// Converting cmf::bytestring to PyBytes
$result = PyBytes_FromStringAndSize($1.c_str(), $1.size());
}
%typemap(in) cmf::bytestring {
// Convert PyBytes to cmf::bytestring
if (PyBytes_Check($input)) {
Py_ssize_t size = PyBytes_Size($input);
$1 = cmf::bytestring(PyBytes_AsString($input), size);
} else {
SWIG_exception_fail(SWIG_TypeError,"WKB expects byte string");
}
}

%implicitconv cmf::math::Time;
%implicitconv cmf::math::Date;
Expand Down
9 changes: 8 additions & 1 deletion cmf/cmf_core_src/upslope/cell.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ namespace cmf {

cmf::project & m_project;
meteo_pointer m_meteo;


cmf::bytestring m_WKB;

public:
/// The vegetation object of the cell
Expand Down Expand Up @@ -264,6 +265,12 @@ namespace cmf {
}
}
ptrdiff_t Id;
cmf::bytestring get_WKB() const {
return m_WKB;
}
void set_WKB(cmf::bytestring wkb) {
m_WKB = wkb;
}
cmf::project& get_project() const
{
return m_project;
Expand Down
1 change: 1 addition & 0 deletions cmf/cmf_core_src/upslope/cell.i
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
%attribute(cmf::upslope::Cell,real,saturated_depth,get_saturated_depth,set_saturated_depth);
%attribute(cmf::upslope::Cell,real,area,get_area);
%attribute(cmf::upslope::Cell,real,soildepth,get_soildepth);

%include "upslope/cell.h"

%extend cmf::upslope::Cell
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from distutils.sysconfig import get_config_var, customize_compiler


version = '1.1.0'
version = '1.1.1a1'

# Try to import numpy, if it fails we have a problem
try:
Expand Down

0 comments on commit 7b03912

Please sign in to comment.