Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C API: Add functions to work on CoordinateSequences with M values #1246

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions capi/geos_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,12 @@ extern "C" {
return GEOSCoordSeq_create_r(handle, size, dims);
}

CoordinateSequence*
GEOSCoordSeq_createWithDimensions(unsigned int size, int hasZ, int hasM)
{
return GEOSCoordSeq_createWithDimensions_r(handle, size, hasZ, hasM);
}

CoordinateSequence*
GEOSCoordSeq_copyFromBuffer(const double* buf, unsigned int size, int hasZ, int hasM)
{
Expand Down Expand Up @@ -1138,19 +1144,25 @@ extern "C" {
int
GEOSCoordSeq_setX(CoordinateSequence* s, unsigned int idx, double val)
{
return GEOSCoordSeq_setOrdinate(s, idx, 0, val);
return GEOSCoordSeq_setOrdinate(s, idx, CoordinateSequence::X, val);
}

int
GEOSCoordSeq_setY(CoordinateSequence* s, unsigned int idx, double val)
{
return GEOSCoordSeq_setOrdinate(s, idx, 1, val);
return GEOSCoordSeq_setOrdinate(s, idx, CoordinateSequence::Y, val);
}

int
GEOSCoordSeq_setZ(CoordinateSequence* s, unsigned int idx, double val)
{
return GEOSCoordSeq_setOrdinate(s, idx, 2, val);
return GEOSCoordSeq_setOrdinate(s, idx, CoordinateSequence::Z, val);
}

int
GEOSCoordSeq_setM(CoordinateSequence* s, unsigned int idx, double val)
{
return GEOSCoordSeq_setOrdinate(s, idx, CoordinateSequence::M, val);
}

int
Expand Down Expand Up @@ -1180,19 +1192,25 @@ extern "C" {
int
GEOSCoordSeq_getX(const CoordinateSequence* s, unsigned int idx, double* val)
{
return GEOSCoordSeq_getOrdinate(s, idx, 0, val);
return GEOSCoordSeq_getOrdinate(s, idx, CoordinateSequence::X, val);
}

int
GEOSCoordSeq_getY(const CoordinateSequence* s, unsigned int idx, double* val)
{
return GEOSCoordSeq_getOrdinate(s, idx, 1, val);
return GEOSCoordSeq_getOrdinate(s, idx, CoordinateSequence::Y, val);
}

int
GEOSCoordSeq_getZ(const CoordinateSequence* s, unsigned int idx, double* val)
{
return GEOSCoordSeq_getOrdinate(s, idx, 2, val);
return GEOSCoordSeq_getOrdinate(s, idx, CoordinateSequence::Z, val);
}

int
GEOSCoordSeq_getM(const CoordinateSequence* s, unsigned int idx, double* val)
{
return GEOSCoordSeq_getOrdinate(s, idx, CoordinateSequence::M, val);
}

int
Expand Down
54 changes: 54 additions & 0 deletions capi/geos_c.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,13 @@ extern GEOSCoordSequence GEOS_DLL *GEOSCoordSeq_create_r(
unsigned int size,
unsigned int dims);

/** \see GEOSCoordSeq_createWithDimensions */
extern GEOSCoordSequence GEOS_DLL *GEOSCoordSeq_createWithDimensions_r(
GEOSContextHandle_t handle,
unsigned int size,
int hasZ,
int hasM);

/** \see GEOSCoordSeq_copyFromBuffer */
extern GEOSCoordSequence GEOS_DLL *GEOSCoordSeq_copyFromBuffer_r(
GEOSContextHandle_t handle,
Expand Down Expand Up @@ -506,6 +513,12 @@ extern int GEOS_DLL GEOSCoordSeq_setZ_r(
GEOSCoordSequence* s, unsigned int idx,
double val);

/** \see GEOSCoordSeq_setM */
extern int GEOS_DLL GEOSCoordSeq_setM_r(
GEOSContextHandle_t handle,
GEOSCoordSequence* s, unsigned int idx,
double val);

/** \see GEOSCoordSeq_setXY */
extern int GEOS_DLL GEOSCoordSeq_setXY_r(
GEOSContextHandle_t handle,
Expand Down Expand Up @@ -543,6 +556,12 @@ extern int GEOS_DLL GEOSCoordSeq_getZ_r(
const GEOSCoordSequence* s,
unsigned int idx, double *val);

/** \see GEOSCoordSeq_getM */
extern int GEOS_DLL GEOSCoordSeq_getM_r(
GEOSContextHandle_t handle,
const GEOSCoordSequence* s,
unsigned int idx, double *val);

/** \see GEOSCoordSeq_getXY */
extern int GEOS_DLL GEOSCoordSeq_getXY_r(
GEOSContextHandle_t handle,
Expand Down Expand Up @@ -2252,6 +2271,16 @@ extern void GEOS_DLL GEOSFree(void *buffer);
*/
extern GEOSCoordSequence GEOS_DLL *GEOSCoordSeq_create(unsigned int size, unsigned int dims);

/**
* Create a coordinate sequence.
* \param size number of coordinates in the sequence
* \param hasZ whether the sequence should store Z values
* \param hasM whether the sequence should store M values
* \return the sequence or NULL on exception
* \since 3.14
*/
extern GEOSCoordSequence GEOS_DLL *GEOSCoordSeq_createWithDimensions(unsigned int size, int hasZ, int hasM);

/**
* Create a coordinate sequence by copying from an interleaved buffer of doubles (e.g., XYXY or XYZXYZ)
* \param buf pointer to buffer
Expand Down Expand Up @@ -2347,6 +2376,18 @@ extern int GEOS_DLL GEOSCoordSeq_setY(GEOSCoordSequence* s,
*/
extern int GEOS_DLL GEOSCoordSeq_setZ(GEOSCoordSequence* s,
unsigned int idx, double val);

/**
* Set M ordinate values in a coordinate sequence.
* \param s the coordinate sequence
* \param idx the index of the coordinate to alter, zero based
* \param val the value to set the ordinate to
* \return 0 on exception
* \since 3.14
*/
extern int GEOS_DLL GEOSCoordSeq_setM(GEOSCoordSequence* s,
unsigned int idx, double val);

/**
* Set X and Y ordinate values in a coordinate sequence simultaneously.
* \param s the coordinate sequence
Expand Down Expand Up @@ -2405,6 +2446,7 @@ extern int GEOS_DLL GEOSCoordSeq_getX(const GEOSCoordSequence* s,
*/
extern int GEOS_DLL GEOSCoordSeq_getY(const GEOSCoordSequence* s,
unsigned int idx, double *val);

/**
* Read Z ordinate values from a coordinate sequence.
* \param s the coordinate sequence
Expand All @@ -2415,6 +2457,18 @@ extern int GEOS_DLL GEOSCoordSeq_getY(const GEOSCoordSequence* s,
*/
extern int GEOS_DLL GEOSCoordSeq_getZ(const GEOSCoordSequence* s,
unsigned int idx, double *val);

/**
* Read M ordinate values from a coordinate sequence.
* \param s the coordinate sequence
* \param idx the index of the coordinate to alter, zero based
* \param val pointer where ordinate value will be placed
* \return 0 on exception
* \since 3.14
*/
extern int GEOS_DLL GEOSCoordSeq_getM(const GEOSCoordSequence* s,
unsigned int idx, double *val);

/**
* Read X and Y ordinate values from a coordinate sequence.
* \param s the coordinate sequence
Expand Down
32 changes: 26 additions & 6 deletions capi/geos_ts_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2683,6 +2683,14 @@
});
}

CoordinateSequence*
GEOSCoordSeq_createWithDimensions_r(GEOSContextHandle_t extHandle, unsigned int size, int hasZ, int hasM)
{
return execute(extHandle, [&]() {
return new CoordinateSequence(size, hasZ, hasM);
});
}

CoordinateSequence*
GEOSCoordSeq_copyFromBuffer_r(GEOSContextHandle_t extHandle, const double* buf, unsigned int size, int hasZ, int hasM)
{
Expand Down Expand Up @@ -2848,19 +2856,25 @@
int
GEOSCoordSeq_setX_r(GEOSContextHandle_t extHandle, CoordinateSequence* s, unsigned int idx, double val)
{
return GEOSCoordSeq_setOrdinate_r(extHandle, s, idx, 0, val);
return GEOSCoordSeq_setOrdinate_r(extHandle, s, idx, CoordinateSequence::X, val);
}

int
GEOSCoordSeq_setY_r(GEOSContextHandle_t extHandle, CoordinateSequence* s, unsigned int idx, double val)
{
return GEOSCoordSeq_setOrdinate_r(extHandle, s, idx, 1, val);
return GEOSCoordSeq_setOrdinate_r(extHandle, s, idx, CoordinateSequence::Y, val);
}

int
GEOSCoordSeq_setZ_r(GEOSContextHandle_t extHandle, CoordinateSequence* s, unsigned int idx, double val)
{
return GEOSCoordSeq_setOrdinate_r(extHandle, s, idx, 2, val);
return GEOSCoordSeq_setOrdinate_r(extHandle, s, idx, CoordinateSequence::Z, val);
}

int
GEOSCoordSeq_setM_r(GEOSContextHandle_t extHandle, CoordinateSequence* s, unsigned int idx, double val)

Check warning on line 2875 in capi/geos_ts_c.cpp

View check run for this annotation

Codecov / codecov/patch

capi/geos_ts_c.cpp#L2875

Added line #L2875 was not covered by tests
{
return GEOSCoordSeq_setOrdinate_r(extHandle, s, idx, CoordinateSequence::M, val);

Check warning on line 2877 in capi/geos_ts_c.cpp

View check run for this annotation

Codecov / codecov/patch

capi/geos_ts_c.cpp#L2877

Added line #L2877 was not covered by tests
}

int
Expand Down Expand Up @@ -2902,19 +2916,25 @@
int
GEOSCoordSeq_getX_r(GEOSContextHandle_t extHandle, const CoordinateSequence* s, unsigned int idx, double* val)
{
return GEOSCoordSeq_getOrdinate_r(extHandle, s, idx, 0, val);
return GEOSCoordSeq_getOrdinate_r(extHandle, s, idx, CoordinateSequence::X, val);
}

int
GEOSCoordSeq_getY_r(GEOSContextHandle_t extHandle, const CoordinateSequence* s, unsigned int idx, double* val)
{
return GEOSCoordSeq_getOrdinate_r(extHandle, s, idx, 1, val);
return GEOSCoordSeq_getOrdinate_r(extHandle, s, idx, CoordinateSequence::Y, val);
}

int
GEOSCoordSeq_getZ_r(GEOSContextHandle_t extHandle, const CoordinateSequence* s, unsigned int idx, double* val)
{
return GEOSCoordSeq_getOrdinate_r(extHandle, s, idx, 2, val);
return GEOSCoordSeq_getOrdinate_r(extHandle, s, idx, CoordinateSequence::Z, val);
}

int
GEOSCoordSeq_getM_r(GEOSContextHandle_t extHandle, const CoordinateSequence* s, unsigned int idx, double* val)

Check warning on line 2935 in capi/geos_ts_c.cpp

View check run for this annotation

Codecov / codecov/patch

capi/geos_ts_c.cpp#L2935

Added line #L2935 was not covered by tests
{
return GEOSCoordSeq_getOrdinate_r(extHandle, s, idx, CoordinateSequence::M, val);

Check warning on line 2937 in capi/geos_ts_c.cpp

View check run for this annotation

Codecov / codecov/patch

capi/geos_ts_c.cpp#L2937

Added line #L2937 was not covered by tests
}

int
Expand Down
36 changes: 33 additions & 3 deletions tests/unit/capi/GEOSCoordSeqTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -835,9 +835,7 @@ void object::test<25>()
{
set_test_name("setOrdinate on XYM coordinate");

/* TODO: use GEOSCoordSeq_createWithDimensions() instead */
double buffer[3];
cs_ = GEOSCoordSeq_copyFromBuffer(buffer, 1, 0, 1);
cs_ = GEOSCoordSeq_createWithDimensions(1, 0, 1);

ensure("setX", GEOSCoordSeq_setOrdinate(cs_, 0, 0, 1));
ensure("setY", GEOSCoordSeq_setOrdinate(cs_, 0, 1, 2));
Expand All @@ -852,4 +850,36 @@ void object::test<25>()
ensure_equals("M", m, 4);
}

template<>
template<>
void object::test<26>()
{
set_test_name("getM and setM on XYZ sequence");

cs_ = GEOSCoordSeq_create(1, 3);

// setM fails
ensure("setM", !GEOSCoordSeq_setM(cs_, 0, 4));

// getM succeeds but sets value to NaN
double m = 0;
ensure("getM", GEOSCoordSeq_getM(cs_, 0, &m));
ensure(std::isnan(m));
}

template<>
template<>
void object::test<27>()
{
set_test_name("getM and setM on XYZM sequence");

cs_ = GEOSCoordSeq_create(1, 4);

ensure("setM", GEOSCoordSeq_setM(cs_, 0, 4));

double m;
ensure(GEOSCoordSeq_getM(cs_, 0, &m));
ensure_equals(m, 4);
}

} // namespace tut