uber / uber/h3-java

Export to GeoJson

Open
#78 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
333
Forks
68
PR merge metrics
No merged PRs in 30d

Description

You will need

        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
		    <version>20160810</version>
        </dependency>

I still getting some strange results like the image in the end where I show the resulting hexagons covers a much bigger area than my fence.


package br.com.cmabreu.hexagon.services;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Service;

import com.uber.h3core.H3Core;
import com.uber.h3core.util.GeoCoord;

@Service
public class InitService {
	private H3Core h3;
	
	
	@PostConstruct
	public void onInit() {
		
		
		try {
			List<GeoCoord> fence = new ArrayList<GeoCoord>();
            fence.add( new GeoCoord(-19.487, -46.747) );
            fence.add( new GeoCoord(-19.487, -35.585) );
            fence.add( new GeoCoord(-25.275, -35.585) );
            fence.add( new GeoCoord(-25.275, -46.747) );	
			
			h3 = H3Core.newInstance();
			
			List<Long> hexagons = h3.polyfill(fence, null, 3);
			
			System.out.println( getGeoJson( hexagons ) );
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
	}	

	
	private JSONObject getGeometry( List<GeoCoord> coordinates ) {
		JSONObject geometry = new JSONObject();

		JSONArray wrapper = new JSONArray();
		JSONArray coords = new JSONArray();
		for( int x=0; x < coordinates.size(); x++ ) {
			JSONArray coord = new JSONArray();
			coord.put(0, coordinates.get(x).lng );
			coord.put(1, coordinates.get(x).lat );
			wrapper.put( x, coord );
		}
		
		// Close the polygon last = first
		JSONArray coord = new JSONArray();
		coord.put(0, coordinates.get(0).lng );
		coord.put(1, coordinates.get(0).lat );
		wrapper.put( coordinates.size(), coord );
		
		coords.put( wrapper );
		geometry.put("type", "Polygon");
		geometry.put("coordinates", coords);
		return geometry;
	}
	
	private JSONObject getFeature( List<GeoCoord> coordinates ) {
		JSONObject feature = new JSONObject();
		feature.put("type", "Feature");
		feature.put("properties", getProperties() );
		feature.put("geometry", getGeometry( coordinates ) );
		return feature;
	}
	
	private JSONObject getProperties() {
		JSONObject properties = new JSONObject();
		properties.put("prop01", "value01");
		return properties;
	}
	
	private String getGeoJson( List<Long> hexagons ) {
		JSONObject featureCollection = new JSONObject();
		featureCollection.put("type", "FeatureCollection");
		JSONArray features = new JSONArray();
		for( Long cell : hexagons ) {
			features.put( getFeature( h3.h3ToGeoBoundary(cell) ) );
		}
		featureCollection.put("features", features);
		return featureCollection.toString();
	}
	
	
}

result
fence

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The example is in InitService, especially h3.polyfill(..., 3), h3ToGeoBoundary, and getGeoJson; start by checking the coordinate order and the polygon passed to polyfill against the GeoJSON coordinate convention. Reproduce the printed FeatureCollection and compare its hexagon boundaries with the supplied fence; done means the exported polygons match the intended area.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.