-
Notifications
You must be signed in to change notification settings - Fork 22
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
Executing Geojson2h3 on the browser without Nodejs #28
Comments
So this is a Node.js library using the CommonJS module system. If you want to get it in the browser, you need to bundle it with a shim of some sort to work in the browser. Browserify would be the simplest way to make such a bundle, though it is less popular these days. |
Hi. Thank you for your reply. |
Fully client side like you suspect. It's not hard to add that shim, but |
Thank you so much, |
So all of the browser-shim tools require you to use Node.js to generate a browser-only JS file. But I mentioned Browserify because I personally find it the easiest to work with. I just made damocles@zelbinion:~/oss$ mkdir tmp
damocles@zelbinion:~/oss$ cd tmp
damocles@zelbinion:~/oss/tmp$ yarn add browserify h3-js
...
damocles@zelbinion:~/oss/tmp$ ./node_modules/.bin/browserify -o h3js.js -r h3-js
damocles@zelbinion:~/oss/tmp$ vim index.html The last command, <!doctype html>
<html>
<head>
<title>Example Browserified h3js</title>
<script src="./h3js.js"></script>
</head>
<body>
<script>
const h3js = require('h3-js');
window.alert(h3js.geoToH3(0, 0, 9));
</script>
</body>
</html> Browserify creates a Opening that file in Firefox, I then got: Which confirms that it works. EDIT: Btw, this requires that you have EDIT2: I just realized that I used |
Thank you so much David Ellis. |
I used an ubuntu server to first install geojson2h3 and also browserify I started by installing the browserify and h3-js on the ubuntu server
Then I imported the generated geojson2h3.js file in the html Then when the html page loads, I call this javascript function via browser console
But I receive this output and error
|
So that error is expected. Currently geojson2h3 only supports [ |
Thank you again David, That worked! My geojson is created from shapefile and just contains points, no polygons there. |
Well, that really depends on what those points represent and how you want to turn them into a polygon. To be a polygon there needs to be an order for the points. If your shapefile already has the points listed in the right order for the polygon you want, then it would just be wrapping all of the point arrays into a big array in the GeoJSON format and you're done. However, if the points represent a "cloud" of activity in an area and you want the smallest polygon to contain them, then you need to implement a convex hull algorithm to get the polygon to then put into the GeoJSON to then feed into this to produce a set of H3 cells. You can simply short circuit that by calling geoToH3 on all of the points and use the JS Basically, there is no one right answer and it all depends on both the kind of input data you have and how you intend to use the output. |
Attaching sample geojson data (of the first dataset shown above). Note file is zipped. |
So I wouldn't use You'd have to ask @nrabinowitz what the buffer radius is about / how it works, but the basics of what you need are just: const h3 = require('h3-js');
const resolution = 9;
const aggregatedH3Density = {};
for (const point in pointCloud) {
const h3Index = h3.geoToH3(point[0], point[1], resolution); // Assuming point is [lat, lng]
aggregatedH3Density[h3Index] = (aggregatedH3Density[h3Index] ?? 0) + 1;
} Then all of the hexagons that have any data at all will be the keys of the The easiest way is to smooth in grid-space using the const h3 = require('h3-js');
const resolution = 9;
const k = 1;
const aggregatedH3Density = {};
for (const point in pointCloud) {
const krd = h3.kRingDistnaces(h3.geoToH3(point[0], point[1], resolution), k); // Assuming point is [lat, lng]
const numRings = krd.length;
for (const [index, ring] in krd.entries()) {
const distributedVal = 1.0 / (numRings * ring.length);
for (const cell in ring) {
aggregatedH3Density[h3Index] = (aggregatedH3Density[h3Index] ?? 0) + distributedVal;
}
}
} This doesn't smooth by exact kilometers, but by grid-space sizes, however it is super simple to understand and is very similar to how Uber's hex surge algorithm dealt with "eyeball" point data when I worked there (it was in Java and was written in "reverse" so it could be parallelizable across CPU cores, with batches of output hexagons assigned to different cores all reading from the same read-only input array, which was possible because the service area to cover was known ahead of time). |
Dear David, I would first have to bring the geojson data (point features) to coordinate format to perform that line. I would be grateful if you can guide me on that aspect as well, if you know. |
Hi David,
|
I am trying to execute Geojson2h3 on the client browser without Nodejs. I have imported the 'geojson2h3.js' javascript file
However, I am getting an error with the following code.
I am hoping that this is possible to execute geojson2h3 in the browser.
I receive this error.
The text was updated successfully, but these errors were encountered: