uber / h3-java

Java bindings for H3, a hierarchical hexagonal geospatial indexing system
https://uber.github.io/h3/
Apache License 2.0
282 stars 54 forks source link

h3 index converted to Geocoordinates: how to export to geojson #66

Open JB-data opened 4 years ago

JB-data commented 4 years ago

Hi, I had 2 quesions related to using h3ToGeoBoundary: Question 1: I am doing some preprocessing in spark using h3, and I want to save my results into a format that can easily be exported. My goal is to load the data in geomesa/geoserver. I notice that using h3ToGeoBoundary I get results that are a list of GeoCoord like this: [GeoCoord{lat=50.346809, lng=14.153511}, GeoCoord{lat=50.316251, lng=14.138129}, GeoCoord{lat=50.294416, lng=14.173950}, GeoCoord{lat=50.303130, lng=14.225160}, GeoCoord{lat=50.333686, lng=14.240581}, GeoCoord{lat=50.355530, lng=14.204753}] I assume this is the google coordinate type: https://pub.dev/documentation/google_directions_api/latest/google_directions_api/GeoCoord-class.html. But is this compatible with geospark/geomesa?

It would be very helpful, if there was an option to save this as let's say geojson like there is for h3-js (https://github.com/uber/h3-js#module_h3.h3ToGeoBoundary where a boolean decides how it is saved). Is there an example somewhere, if I have let's say 10k h3 indexes how to save it into a format that other geospatial tools (geomesa or geospark ) can read?

Question2: Also when I want to apply a udf to a full column with h3 indexes, this works: //works val h3ToGeoBoundary = udf{ (h3Index: Long) => H3.instance.h3ToGeoBoundary(h3Index).toString() } but this does not: //does not work val h3ToGeoBoundaryGeoJson = udf{ (h3Index: Long) => H3.instance.h3ToGeoBoundary(h3Index) } //error:java.lang.UnsupportedOperationException: Schema for type java.util.List[com.uber.h3core.util.GeoCoord] is not supported This means that if I want to create a dataframe with couple 1000 rows, I can only have that in string format, which does not allow me to do transformations on it (like selecting each element of the list, and using those coordinates to turn it myself into geojson). Any clue how to solve this?

Thanks! PS: great job-very user friendly and cool library.

isaacbrodsky commented 4 years ago

GeoCoord here is an immutable object constructed by H3-Java so as to not tie it to different frameworks. It contains the latitude and longitude in degrees.

We don't currently have the options around GeoJson output formatting. H3-JS needs that option because it represents coordinates as either lat,lng or lng,lat. In Java these are typed fields, so there is no possibility of switching them. We could add support for formatting or parsing H3 geometries into GeoSpark/GeoJSON/WKT/etc. in a module alongside h3-java. I think this would be a separate set of functions so that only users needing that functionality pull in those dependencies.

Unfortunately I am not very familiar with GeoSpark, for your example, I'd suggest adding the conversion to GeoJSON directly to the UDF. E.g. convertToGeoJson(H3.instance.h3ToGeoBoundary(h3Index)) It looks like GeoSpark doesn't permit GeoCoords within Lists - this might be an issue of GeoCoord not implementing Serializable but I'm not 100% sure.

JB-data commented 4 years ago

Thanks for response! Indeed, such a separate module would be a big plus. The current GeoCoord field comes from https://github.com/uber/h3-java/blob/master/src/main/java/com/uber/h3core/util/GeoCoord.java - for exporting to other geospatial tools it might have been more convenient to just write an array of (lat,long) tuples (without any strings)?

Thanks for the suggestion about the UDF. I believe you might be right about the serializable. I assume convertToGeo should be constructed then using some regular expressions after converting the result of h3ToGeoBoundary to a string, which indeed should be feasible.

Best&good luck with this project.

isaacbrodsky commented 4 years ago

You may still be able to avoid string serialization by mapping the GeoCoords into the form you need For example, you could do something like:

List<GeoCoord> coords = h3.h3ToGeoBoundary(index);
// convert to arrays of lat,lon
List<double[]> rawCoords = coords.stream().map(c -> new double[]{c.lat, c.lon}).collect(Collectors.toList());
return rawCoords;
icemagno commented 3 years ago

See #78