Code Monkey home page Code Monkey logo

Comments (9)

mhugo avatar mhugo commented on July 17, 2024

Hi,
Your assumptions are correct. We'll add a numDecimal parameter to the API.
By the way, I am very curious: do you have some snippets to share about how to use SFCGAL with Python ctypes ? :)

from sfcgal.

mloeks avatar mloeks commented on July 17, 2024

Hi Hugo,

thanks for your super-quick response. Glad that I got that right, as I'm not a C(++) programmer.. ;-)
Cool, that sounds great! Thanks for adding that additional parameter.

Sure, I'll gladly share some ctypes code. Most of it is actually pretty straightforward. Unfortunately, the official ctypes documentation is not the best in my opinion. I had to do some research as well before I got it running.

I'll post some sample code snippets tomorrow, since I have not time left for that today.

Let me know when you exposed the numDecimal parameter to the C API.

Thanks and best regards,
Matthias

from sfcgal.

vmora avatar vmora commented on July 17, 2024

Hi @Matze09,

Is my assumption correct, that this is indeed caused by the CGAL "exact" mode?

Yes it is.

And if so, is there any way to set the "numDecimal" parameter over the C API and ctypes?
If not, could you please add the numDecimal parameter to the C API function?

We need to add a 2 param version of sfcgal_geometry_as_text().

It should be trivial, but I can't do it right away. If you don't mind adding that in your fork, a PR will be welcome.

Are you making a python interface to SFCGAL ?

from sfcgal.

vmora avatar vmora commented on July 17, 2024

The function in sfcgal_c.cpp is:

extern "C" void sfcgal_geometry_as_text( const sfcgal_geometry_t* pgeom, char** buffer, size_t* len )
{
    SFCGAL_GEOMETRY_CONVERT_CATCH_TO_ERROR_NO_RET(
        std::string wkt = reinterpret_cast<const SFCGAL::Geometry*>( pgeom )->asText();
        *buffer = ( char* )__sfcgal_alloc_handler( wkt.size() + 1 );
        *len = wkt.size();
        strncpy( *buffer, wkt.c_str(), *len );
    )
}

So you just have to add one function:

extern "C" void sfcgal_geometry_as_text( const sfcgal_geometry_t* pgeom, char** buffer, size_t* len, int numDecimals )
{
    SFCGAL_GEOMETRY_CONVERT_CATCH_TO_ERROR_NO_RET(
        std::string wkt = reinterpret_cast<const SFCGAL::Geometry*>( pgeom )->asText( numDecimal );
        *buffer = ( char* )__sfcgal_alloc_handler( wkt.size() + 1 );
        *len = wkt.size();
        strncpy( *buffer, wkt.c_str(), *len );
    )
}

from sfcgal.

vmora avatar vmora commented on July 17, 2024

Sorry, the new function name must be different (sfcgal_geometry_as_text_decim).

from sfcgal.

mloeks avatar mloeks commented on July 17, 2024

Hi,

thanks for your help. I'll do as you suggested and make a pull request.

What do you think about changing the existing function to require the numDecimals parameter instead of adding a new one? It would avoid unnecessary code duplication.
However, I guess changing the methods parameters could be regarded as critical because it is not backwards compatible with existing code? Then I could of course just add the new method you suggested instead.

No, I haven't thought about a Python wrapper for SFCGAL yet. But it is an interesting idea, worth considering :-) It actually wouldn't be very hard to do I suppose.

Cheers,
Matthias

from sfcgal.

mloeks avatar mloeks commented on July 17, 2024

Btw, another consideration would be to change the default numDecimals from -1 to something like 6 or 8 in the existing method. I wonder who really uses the exact int fraction WKT, since it cannot be parsed by GEOS (Shapely, PostGIS...).

m.

from sfcgal.

mloeks avatar mloeks commented on July 17, 2024

Created pull request #71

from sfcgal.

mloeks avatar mloeks commented on July 17, 2024

Here is a small basic example how to access the SFCGAL C API via Python ctypes:

from ctypes import CDLL, c_void_p, c_double
from ctypes.util import find_library

lib = find_library('SFCGAL')
lib_sfcgal = CDLL(lib)

# if find_library doesn't work, try passing the .so path directly (e.g. /usr/local/lib):
# lib_sfcgal = CDLL('/usr/local/lib/libSFCGAL.so')

# each function from the C API that should be usable in Python has to be "prototyped"
# i.e. the return type and all argument types have to be defined via ctypes types
# cf. https://docs.python.org/2/library/ctypes.html#fundamental-data-types
lib_sfcgal.sfcgal_point_create_from_xy.restype = c_void_p
lib_sfcgal.sfcgal_point_create_from_xy.argtypes = [c_double, c_double]

lib_sfcgal.sfcgal_point_x.restype = c_double
lib_sfcgal.sfcgal_point_x.argtypes = [c_void_p]

lib_sfcgal.sfcgal_point_y.restype = c_double
lib_sfcgal.sfcgal_point_y.argtypes = [c_void_p]

# now the prototyped functions can be used.. create and retrieve basic point geometry
test_point = lib_sfcgal.sfcgal_point_create_from_xy(1.2345, 2.3456)

assert lib_sfcgal.sfcgal_point_x(test_point) == 1.2345
assert lib_sfcgal.sfcgal_point_y(test_point) == 2.3456

It runs on my machine (Ubuntu 14.04.1 LTS 64-bit, Python 2.7.6) standalone, given that SFCGAL has been compiled and installed correctly.

from sfcgal.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.