Send middle of polygon in approximated mode

This commit is contained in:
Mieszko Wrzeszczyński 2018-11-19 00:03:31 +01:00
parent 77b7c50e4e
commit aa3c2eb9b8
6 changed files with 102 additions and 39 deletions

View File

@ -3,7 +3,9 @@
{ {
"type": "Feature", "type": "Feature",
"properties": { "properties": {
"name": "Skrzydło B" "name": "Skrzydło B",
"longitude" : 52.466669,
"latitude" : 16.926624
}, },
"geometry": { "geometry": {
"coordinates": [ "coordinates": [
@ -89,7 +91,9 @@
{ {
"type": "Feature", "type": "Feature",
"properties": { "properties": {
"name": "Biblioteka" "name": "Biblioteka",
"longitude" : 52.467351,
"latitude" : 16.926900
}, },
"geometry": { "geometry": {
"coordinates": [ "coordinates": [
@ -139,7 +143,9 @@
{ {
"type": "Feature", "type": "Feature",
"properties": { "properties": {
"name": "Łącznik" "name": "Łącznik",
"longitude" : 52.466619,
"latitude" : 16.926860
}, },
"geometry": { "geometry": {
"coordinates": [ "coordinates": [
@ -173,7 +179,9 @@
{ {
"type": "Feature", "type": "Feature",
"properties": { "properties": {
"name": "Skrzydło A" "name": "Skrzydło A",
"longitude" : 52.466559,
"latitude" : 16.927163
}, },
"geometry": { "geometry": {
"coordinates": [ "coordinates": [
@ -231,7 +239,10 @@
{ {
"type": "Feature", "type": "Feature",
"properties": { "properties": {
"name": "Aule" "name": "Aule",
"longitude" : 52.467114,
"latitude" : 16.927621
}, },
"geometry": { "geometry": {
"coordinates": [ "coordinates": [
@ -334,7 +345,8 @@
"type": "Feature", "type": "Feature",
"properties": { "properties": {
"name": "Hol", "name": "Hol",
"point" : [52.467088, 16.927150] "longitude" : 52.4671021,
"latitude" : 16.927122
}, },
"geometry": { "geometry": {
"coordinates": [ "coordinates": [

View File

@ -19,6 +19,7 @@ import android.widget.Toast;
import com.uam.wmi.findmytutor.R; import com.uam.wmi.findmytutor.R;
import com.uam.wmi.findmytutor.service.BackgroundLocalizationService; import com.uam.wmi.findmytutor.service.BackgroundLocalizationService;
import com.uam.wmi.findmytutor.utils.PrefUtils; import com.uam.wmi.findmytutor.utils.PrefUtils;
import com.uam.wmi.findmytutor.utils.SharingLevel;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
@ -40,9 +41,10 @@ public class SharingFragment extends PreferenceFragment {
public void onCreate(final Bundle savedInstanceState) { public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
locationLevelMapping = new HashMap<Integer, String>(); locationLevelMapping = new HashMap<Integer, String>();
locationLevelMapping.put(0,"presence");
locationLevelMapping.put(1,"approximated"); locationLevelMapping.put(0, SharingLevel.PRESENCE.toString());
locationLevelMapping.put(2,"exact"); locationLevelMapping.put(1, SharingLevel.APPROXIMATED.toString());
locationLevelMapping.put(2, SharingLevel.EXACT.toString());
addPreferencesFromResource(R.layout.pref_sharing); addPreferencesFromResource(R.layout.pref_sharing);
Preference manualStatus = findPreference("key_manual_status"); Preference manualStatus = findPreference("key_manual_status");
@ -60,8 +62,8 @@ public class SharingFragment extends PreferenceFragment {
}); });
locationMode.setOnPreferenceChangeListener((preference, newValue) -> { locationMode.setOnPreferenceChangeListener((preference, newValue) -> {
ListPreference lp = (ListPreference) preference;
PrefUtils.storeLocationMode(getApplicationContext(),locationLevelMapping.get(Integer.parseInt((String) newValue))); PrefUtils.storeLocationMode(getApplicationContext(),locationLevelMapping.get(Integer.parseInt((String) newValue)));
return true; return true;
}); });

View File

@ -47,6 +47,7 @@ public class Coordinate extends BaseResponse {
public Coordinate (Double latitude, Double longitude, Double altitude, String approximatedLocation, String label, String userId, String displayMode) { public Coordinate (Double latitude, Double longitude, Double altitude, String approximatedLocation, String label, String userId, String displayMode) {
//if (!latitudeRange.contains(latitude)) throw new IllegalArgumentException("Inappropriate latitude value" + latitude); //if (!latitudeRange.contains(latitude)) throw new IllegalArgumentException("Inappropriate latitude value" + latitude);
//if (!longtitudeRange.contains(longitude)) throw new IllegalArgumentException("Inappropriate longitude value" + longitude); //if (!longtitudeRange.contains(longitude)) throw new IllegalArgumentException("Inappropriate longitude value" + longitude);
//if (approximatedLocation == null) throw new IllegalArgumentException("Inappropriate approximatedLocation");
this.latitude = latitude; this.latitude = latitude;
this.longitude = longitude; this.longitude = longitude;

View File

@ -33,6 +33,7 @@ import com.uam.wmi.findmytutor.network.ApiClient;
import com.uam.wmi.findmytutor.utils.ApproximatedLocalization; import com.uam.wmi.findmytutor.utils.ApproximatedLocalization;
import com.uam.wmi.findmytutor.utils.PrefUtils; import com.uam.wmi.findmytutor.utils.PrefUtils;
import com.uam.wmi.findmytutor.utils.RestApiHelper; import com.uam.wmi.findmytutor.utils.RestApiHelper;
import com.uam.wmi.findmytutor.utils.SharingLevel;
import com.uam.wmi.findmytutor.utils.mapUtils; import com.uam.wmi.findmytutor.utils.mapUtils;
import java.util.ArrayList; import java.util.ArrayList;
@ -45,7 +46,6 @@ import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody; import okhttp3.ResponseBody;
import timber.log.Timber; import timber.log.Timber;
import static com.mapbox.geojson.Point.fromLngLat;
import static com.uam.wmi.findmytutor.utils.PrefUtils.storeBackgroundLocationStatus; import static com.uam.wmi.findmytutor.utils.PrefUtils.storeBackgroundLocationStatus;
public class BackgroundLocalizationService extends Service { public class BackgroundLocalizationService extends Service {
@ -68,7 +68,6 @@ public class BackgroundLocalizationService extends Service {
private FusedLocationProviderClient mFusedLocationClient; private FusedLocationProviderClient mFusedLocationClient;
public BackgroundLocalizationService() { public BackgroundLocalizationService() {
providers.add(LocationManager.GPS_PROVIDER); providers.add(LocationManager.GPS_PROVIDER);
providers.add(LocationManager.NETWORK_PROVIDER); providers.add(LocationManager.NETWORK_PROVIDER);
@ -289,36 +288,49 @@ public class BackgroundLocalizationService extends Service {
} }
} }
@SuppressLint("StaticFieldLeak")
private class Task extends AsyncTask { private class Task extends AsyncTask {
ApproximatedLocalization approximatedLocalization;
private Double latitude; private Double latitude;
private Double longitude; private Double longitude;
private Double altitude; private Double altitude;
private String approximatedBuildingPart = null;
private CompositeDisposable disposable = new CompositeDisposable(); private CompositeDisposable disposable = new CompositeDisposable();
private CoordinateService coordinateService = ApiClient.getClient(getApplicationContext()) private CoordinateService coordinateService = ApiClient.getClient(getApplicationContext())
.create(CoordinateService.class); .create(CoordinateService.class);
ApproximatedLocalization approximatedLocalization;
private Task(Location location) { private Task(Location location) {
latitude = location.getLatitude(); latitude = location.getLatitude();
longitude = location.getLongitude(); longitude = location.getLongitude();
altitude = location.getAltitude(); altitude = location.getAltitude();
approximatedLocalization = new ApproximatedLocalization(mapUtils.loadJsonFromAsset(getApplicationContext(), "building.geojson")); approximatedLocalization = new ApproximatedLocalization(mapUtils.loadJsonFromAsset(getApplicationContext(), "building.geojson"));
approximatedBuildingPart = approximatedLocalization.getNameOfBuildingPart(Point.fromLngLat(longitude, latitude));
} }
@Override @Override
protected Object doInBackground(Object[] objects) { protected Object doInBackground(Object[] objects) {
/*
Point point = approximatedLocalization.getPointerBuildingPart("Aule"); if (PrefUtils.getLocationLevel(getApplicationContext()).equals(SharingLevel.PRESENCE.toString())) {
Log.e("APPROXIMATED", String.valueOf(point));*/ longitude = 52.467491;
latitude = 16.926782;
Log.e(TAG,"PRESENCE");
} else if (PrefUtils.getLocationLevel(getApplicationContext()).equals(SharingLevel.APPROXIMATED.toString())) {
List<Double> points = approximatedLocalization.getPointerBuildingPart(approximatedBuildingPart);
latitude = points.get(0);
longitude = points.get(1);
Log.e(TAG,"APPROXIMATED");
}else {
Log.e(TAG,"EXACT");
}
try { try {
Coordinate coordinate = new Coordinate( Coordinate coordinate = new Coordinate(
latitude, latitude,
longitude, longitude,
altitude, altitude,
approximatedLocalization.getNameOfBuildingPart(Point.fromLngLat(longitude, latitude)), approximatedBuildingPart,
PrefUtils.getUserStatus(getApplicationContext()), PrefUtils.getUserStatus(getApplicationContext()),
PrefUtils.getUserId(getApplicationContext()), PrefUtils.getUserId(getApplicationContext()),
PrefUtils.getLocationLevel(getApplicationContext()) PrefUtils.getLocationLevel(getApplicationContext())

View File

@ -1,8 +1,12 @@
package com.uam.wmi.findmytutor.utils; package com.uam.wmi.findmytutor.utils;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.util.Log;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
; import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.mapbox.geojson.BoundingBox; import com.mapbox.geojson.BoundingBox;
import com.mapbox.geojson.Feature; import com.mapbox.geojson.Feature;
import com.mapbox.geojson.FeatureCollection; import com.mapbox.geojson.FeatureCollection;
@ -15,8 +19,13 @@ import com.mapbox.geojson.gson.GeometryDeserializer;
import com.mapbox.geojson.gson.PointDeserializer; import com.mapbox.geojson.gson.PointDeserializer;
import com.mapbox.turf.TurfJoins; import com.mapbox.turf.TurfJoins;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects; import java.util.Objects;
;
public class ApproximatedLocalization { public class ApproximatedLocalization {
private FeatureCollection buildingSchema = null; private FeatureCollection buildingSchema = null;
@ -26,10 +35,12 @@ public class ApproximatedLocalization {
private FeatureCollection fromJson(@NonNull String json) { private FeatureCollection fromJson(@NonNull String json) {
GsonBuilder gson = new GsonBuilder(); GsonBuilder gson = new GsonBuilder();
gson.registerTypeAdapterFactory(GeoJsonAdapterFactory.create()); gson.registerTypeAdapterFactory(GeoJsonAdapterFactory.create());
gson.registerTypeAdapter(Point.class, new PointDeserializer()); gson.registerTypeAdapter(Point.class, new PointDeserializer());
gson.registerTypeAdapter(Geometry.class, new GeometryDeserializer()); gson.registerTypeAdapter(Geometry.class, new GeometryDeserializer());
gson.registerTypeAdapter(BoundingBox.class, new BoundingBoxDeserializer()); gson.registerTypeAdapter(BoundingBox.class, new BoundingBoxDeserializer());
return gson.create().fromJson(json, FeatureCollection.class); return gson.create().fromJson(json, FeatureCollection.class);
} }
@ -38,24 +49,31 @@ public class ApproximatedLocalization {
for (Feature feature : Objects.requireNonNull(buildingSchema.features())) { for (Feature feature : Objects.requireNonNull(buildingSchema.features())) {
boolean isInside = TurfJoins.inside(point, (Polygon) Objects.requireNonNull(feature.geometry())); boolean isInside = TurfJoins.inside(point, (Polygon) Objects.requireNonNull(feature.geometry()));
if (isInside){ if (isInside)
return Objects.requireNonNull(Objects.requireNonNull(feature.getStringProperty("name"))); return Objects.requireNonNull(Objects.requireNonNull(feature.getStringProperty("name")));
} }
}
return null; return null;
} }
/*
public Point getMiddlePointForBuildingPart(String BuildingPart) { public List<Double> getPointerBuildingPart(String buildingPart) {
for (Feature feature : Objects.requireNonNull(buildingSchema.features())) { for (Feature feature : Objects.requireNonNull(buildingSchema.features())) {
String partName = feature.getStringProperty("name"); String partName = feature.getStringProperty("name");
if(BuildingPart.equals(partName)) { if (buildingPart != null && buildingPart.equals(partName)) {
JsonElement pointsList = feature.getProperty("point"); Double longitude = feature.getNumberProperty("longitude").doubleValue();
return Point.fromLngLat(pointsList.getAsNumber().doubleValue(), pointsList.getAsJsonArray().get(1)); Double latitude = feature.getNumberProperty("latitude").doubleValue();
List<Double> list = Arrays.asList(longitude,latitude);
Log.e("APPROX",partName + " " + buildingPart + list);
return list;
} }
} }
return Point.fromLngLat(0,0); return Arrays.asList(0.0,0.0);
}*/ }
} }

View File

@ -0,0 +1,18 @@
package com.uam.wmi.findmytutor.utils;
public enum SharingLevel {
PRESENCE("presence"),
APPROXIMATED("approximated"),
EXACT("exact");
private final String text;
SharingLevel(final String text) {
this.text = text;
}
@Override
public String toString() {
return text;
}
}