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

return raw wkb? #1609

Closed
mdsumner opened this issue Sep 25, 2024 · 3 comments
Closed

return raw wkb? #1609

mdsumner opened this issue Sep 25, 2024 · 3 comments

Comments

@mdsumner
Copy link
Contributor

mdsumner commented Sep 25, 2024

Here raw binary wkb is obtained but then converted to string:

unsigned char *hex = GEOSGeomToHEX_buf_r(hGEOSCtxt, g[i].get(), &len);

I'd like to request returning it as a list of raw vectors, I don't think that's otherwise possible in terra atm? We can get wkt string or hex string, but not the raw vector in a list.

I'm interested to have general interop via {wk} with this (SpatVector is really the only major missing type for wk), and not go through character vectors or file or other expanded in-memory form. Thank you. I can probably manage a PR if it's not unwelcome. Not sure what to call the method ... "wkb_raw()" ?

@mdsumner
Copy link
Contributor Author

mdsumner commented Sep 27, 2024

I got this to work by adding this to 'geos_methods.cpp'

Rcpp::List SpatVector::wkb_raw() {
	GEOSContextHandle_t hGEOSCtxt = geos_init();
	std::vector<GeomPtr> g = geos_geoms(this, hGEOSCtxt);
	Rcpp::List out; 
	size_t len = 0;
	for (size_t i = 0; i < g.size(); i++) {
	 unsigned char *hex = GEOSGeomToWKB_buf_r(hGEOSCtxt, g[i].get(), &len);
         Rcpp::RawVector raw(len);
         memcpy(&(raw[0]), hex, len);
         out.push_back(raw);
         free(hex);
	}
	geos_finish(hGEOSCtxt);
	return out;
}

and the required

Rcpp::List wkb_raw(); to 'SpatVector.h'

.method("wkb_raw", &SpatVector::wkb_raw) in 'RcppModule'

That means Rcpp.h added as well which is probably not welcome? I'll keep exploring how to structure this.

Example:

wk::wkb(vect(cbind(1:10, rnorm(10)), "line")@ptr$wkb_raw())
<wk_wkb[1]>
[1] <LINESTRING (1 0.5928676, 2 0.9755936, 3 -1.12088, 4 0.08522013, 5 0.4874861, 6 0.2880466...>

@mdsumner
Copy link
Contributor Author

mdsumner commented Oct 1, 2024

Update, I figured it out, added wkb_raw() to 'geos_methods' with idiomatic style.

Implemented here: https://github.com/mdsumner/terra/tree/raw-wkb-1609

Can we add 'wkb = FALSE' to geom() to control this?

@rhijmans
Copy link
Member

rhijmans commented Oct 1, 2024

Thanks. Implemented with your PR #1612

@rhijmans rhijmans closed this as completed Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants