Skip to content

Instantly share code, notes, and snippets.

@wviana
Last active April 25, 2016 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wviana/82600c8e11518614084e19e323ebbd24 to your computer and use it in GitHub Desktop.
Save wviana/82600c8e11518614084e19e323ebbd24 to your computer and use it in GitHub Desktop.
package neocom.dealerbook.models.layer.dataAdapter;
import com.cocoahero.android.geojson.FeatureCollection;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.maps.android.heatmaps.WeightedLatLng;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import neocom.dealerbook.models.layer.exceptions.InvalidInputDataException;
/**
* Created by wviana on 13/04/16.
*/
public class GeojsonWeightedDataAdapter extends GeoJsonDataAdpter<WeightedLatLng> {
private GeojsonLatLngDataAdapter latLngAdapter;
private List<WeightedLatLng> list;
public GeojsonWeightedDataAdapter(FeatureCollection data) throws InvalidInputDataException {
super(data);
latLngAdapter = new GeojsonLatLngDataAdapter(data);
}
@Override
public LatLng getLatLng(int position) {
return latLngAdapter.get(position);
}
public double getIntensity(int position) {
WeightedLatLng weightedLatLng = get(position);
double intensity = (weightedLatLng != null) ? weightedLatLng.getIntensity() : 0.0;
return intensity;
}
@Override
public WeightedLatLng get(int position) {
WeightedLatLng result;
JSONObject properties = features.get(position).getProperties();
double value = properties.optDouble("Value", 0.0);
result = new WeightedLatLng(getLatLng(position), value);
return result;
}
@Override
public List<WeightedLatLng> getList() {
if (list == null) {
list = new ArrayList<>();
for (int i = 0; i < getSize(); i++) {
list.add(get(i));
}
}
return list;
}
@Override
public int getIconDrawable(int position) {
return 0;
}
@Override
public GeojsonWeightedDataAdapter filterByBound(LatLngBounds bounds) {
FeatureCollection filteredCollections = new FeatureCollection();
for (int i = 0; i < features.size(); i++) {
if (bounds.contains(getLatLng(i))) {
filteredCollections.addFeature(features.get(i));
}
}
try {
return new GeojsonWeightedDataAdapter(filteredCollections);
} catch (InvalidInputDataException e) {
return null;
}
}
public GeojsonWeightedDataAdapter filter(Filter filter){
FeatureCollection filteredCollection = new FeatureCollection();
for(int i = 0; i < getSize(); i++){
if( filter.filterElement(this, i) ){
filteredCollection.addFeature( features.get(i) );
}
}
try {
return new GeojsonWeightedDataAdapter(filteredCollection);
} catch (InvalidInputDataException e) {
return null;
}
}
public static interface Filter{
boolean filterElement(GeojsonWeightedDataAdapter data, int index);
}
public int getIndexOf(WeightedLatLng weightedLatLng){
return getList().indexOf(weightedLatLng);
}
public double getMax() {
List<WeightedLatLng> list = getList();
switch (list.size()){
case 0:
return 0;
case 1:
return list.get(0).getIntensity();
default:
return Collections.max(list, new Comparator<WeightedLatLng>() {
@Override
public int compare(WeightedLatLng weightedLatLng, WeightedLatLng t1) {
if(t1 == null) return (int) (weightedLatLng.getIntensity() - 0);
return (int) (weightedLatLng.getIntensity() - t1.getIntensity());
}
}).getIntensity();
}
}
}
import com.cocoahero.android.geojson.FeatureCollection;
import com.google.android.gms.maps.model.LatLng;
import com.google.maps.android.heatmaps.WeightedLatLng;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.internal.util.io.IOUtil;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import static org.junit.Assert.*;
import neocom.dealerbook.models.layer.dataAdapter.GeojsonLatLngDataAdapter;
import neocom.dealerbook.models.layer.dataAdapter.GeojsonWeightedDataAdapter;
import neocom.dealerbook.models.layer.exceptions.InvalidInputDataException;
/**
* Created by wviana on 25/04/16.
*/
public class GeojsonWeightedDataAdapterTest {
private static String data;
private static final String fileName = "dnc_feature_collection.json";
private GeojsonWeightedDataAdapter dataAdapter;
private FeatureCollection featureCollection;
@Before
public void before() throws JSONException, InvalidInputDataException {
featureCollection = new FeatureCollection(new JSONObject(data));
dataAdapter = new GeojsonWeightedDataAdapter(featureCollection);
}
@Test
public void test_sizes() {
assertEquals(dataAdapter.getSize(), featureCollection.getFeatures().size());
assertEquals(dataAdapter.getSize(), dataAdapter.getList().size());
assertEquals(featureCollection.getFeatures().size(), dataAdapter.getList().size());
}
@Test
public void test_getData() {
assertEquals(featureCollection, dataAdapter.getData());
}
@Test
public void test_getIndexOf(){
WeightedLatLng invalid = new WeightedLatLng(new LatLng(0, 0), 0.0);
assertEquals(-1, dataAdapter.getIndexOf(invalid));
for (int i = 0; i < dataAdapter.getSize(); i++) {
WeightedLatLng valid = dataAdapter.get(i);
assertEquals(i, dataAdapter.getIndexOf(valid));
}
}
@BeforeClass
public static void populateData() throws IOException {
FileReader reader = new FileReader(fileName);
BufferedReader br = new BufferedReader(reader);
StringBuffer sb = new StringBuffer();
String line = br.readLine();
while (line != null){
sb.append(line);
line = br.readLine();
}
data = sb.toString();
}
}
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.google.maps.android.heatmaps;
import com.google.android.gms.maps.model.LatLng;
import com.google.maps.android.geometry.Point;
import com.google.maps.android.projection.SphericalMercatorProjection;
import com.google.maps.android.quadtree.PointQuadTree.Item;
public class WeightedLatLng implements Item {
public static final double DEFAULT_INTENSITY = 1.0D;
private static final SphericalMercatorProjection sProjection = new SphericalMercatorProjection(1.0D);
private Point mPoint;
private double mIntensity;
public WeightedLatLng(LatLng latLng, double intensity) {
this.mPoint = sProjection.toPoint(latLng);
if(intensity >= 0.0D) {
this.mIntensity = intensity;
} else {
this.mIntensity = 1.0D;
}
}
public WeightedLatLng(LatLng latLng) {
this(latLng, 1.0D);
}
public Point getPoint() {
return this.mPoint;
}
public double getIntensity() {
return this.mIntensity;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment