From 64b24cef1b9827670b11a0a1c2cd3b8a14d4cdcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mieszko=20Wrzeszczy=C5=84ski?= Date: Fri, 9 Nov 2018 00:49:09 +0100 Subject: [PATCH] Add approximated localizations --- app/build.gradle | 2 +- app/src/main/assets/building.geojson | 386 ++++++++++++++++++ .../wmi/findmytutor/activity/MapActivity.java | 1 + .../uam/wmi/findmytutor/model/Coordinate.java | 3 +- .../BackgroundLocalizationService.java | 15 +- .../utils/ApproximatedLocalization.java | 64 +++ .../uam/wmi/findmytutor/utils/mapUtils.java | 2 + app/src/main/res/values/colors.xml | 10 +- 8 files changed, 474 insertions(+), 9 deletions(-) create mode 100644 app/src/main/assets/building.geojson create mode 100644 app/src/main/java/com/uam/wmi/findmytutor/utils/ApproximatedLocalization.java diff --git a/app/build.gradle b/app/build.gradle index 87b98e5..aad18fc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -58,7 +58,7 @@ dependencies { implementation 'com.auth0.android:jwtdecode:1.1.1' implementation 'com.annimon:stream:1.2.1' implementation 'com.google.android.gms:play-services-location:16.0.0' - + implementation 'com.mapbox.mapboxsdk:mapbox-sdk-turf:4.0.0' // FloatingBarMenu implementation 'com.getbase:floatingactionbutton:1.10.1' diff --git a/app/src/main/assets/building.geojson b/app/src/main/assets/building.geojson new file mode 100644 index 0000000..e000dd8 --- /dev/null +++ b/app/src/main/assets/building.geojson @@ -0,0 +1,386 @@ +{ + "features": [ + { + "type": "Feature", + "properties": { + "name": "Skrzydło B" + }, + "geometry": { + "coordinates": [ + [ + [ + 16.92618, + 52.466248 + ], + [ + 16.926435, + 52.466201 + ], + [ + 16.92646, + 52.466255 + ], + [ + 16.926516, + 52.466244 + ], + [ + 16.926999, + 52.46711 + ], + [ + 16.926796, + 52.467145 + ], + [ + 16.926782, + 52.467117 + ], + [ + 16.926784, + 52.467121 + ], + [ + 16.926691, + 52.467139 + ], + [ + 16.926662, + 52.46709 + ], + [ + 16.926757, + 52.467072 + ], + [ + 16.926544, + 52.466691 + ], + [ + 16.926434, + 52.46671 + ], + [ + 16.926396, + 52.466655 + ], + [ + 16.926519, + 52.466628 + ], + [ + 16.926323, + 52.466281 + ], + [ + 16.926213, + 52.466307 + ], + [ + 16.92618, + 52.466248 + ] + ] + ], + "type": "Polygon" + }, + "id": "41798cf663bc55c10e6c51c3fe174eda" + }, + { + "type": "Feature", + "properties": { + "name": "Biblioteka" + }, + "geometry": { + "coordinates": [ + [ + [ + 16.927048, + 52.46721 + ], + [ + 16.926627, + 52.467295 + ], + [ + 16.926732, + 52.467482 + ], + [ + 16.926876, + 52.467461 + ], + [ + 16.926967, + 52.467428 + ], + [ + 16.927014, + 52.467402 + ], + [ + 16.927171, + 52.467359 + ], + [ + 16.9271, + 52.467202 + ], + [ + 16.927048, + 52.46721 + ] + ] + ], + "type": "Polygon" + }, + "id": "7328c3c9dffd3e76be8d3dcef4f58ddc" + }, + { + "type": "Feature", + "properties": { + "name": "Łącznik" + }, + "geometry": { + "coordinates": [ + [ + [ + 16.926759, + 52.466683 + ], + [ + 16.927088, + 52.46661 + ], + [ + 16.927019, + 52.466502 + ], + [ + 16.9267, + 52.466571 + ], + [ + 16.926759, + 52.466683 + ] + ] + ], + "type": "Polygon" + }, + "id": "8c19ee28e4c07ece9756fd21f290713b" + }, + { + "type": "Feature", + "properties": { + "name": "Skrzydło A" + }, + "geometry": { + "coordinates": [ + [ + [ + 16.926771, + 52.466106 + ], + [ + 16.926898, + 52.466079 + ], + [ + 16.926926, + 52.466126 + ], + [ + 16.927026, + 52.466107 + ], + [ + 16.92723, + 52.466473 + ], + [ + 16.927184, + 52.466483 + ], + [ + 16.927243, + 52.466586 + ], + [ + 16.927334, + 52.46656 + ], + [ + 16.927602, + 52.466889 + ], + [ + 16.927275, + 52.466959 + ], + [ + 16.926771, + 52.466106 + ] + ] + ], + "type": "Polygon" + }, + "id": "c33bfba772c85cc38ae417843d31b1ff" + }, + { + "type": "Feature", + "properties": { + "name": "Aule" + }, + "geometry": { + "coordinates": [ + [ + [ + 16.92731, + 52.467158 + ], + [ + 16.927315, + 52.46718 + ], + [ + 16.927348, + 52.467199 + ], + [ + 16.927393, + 52.467224 + ], + [ + 16.927426, + 52.467239 + ], + [ + 16.927476, + 52.467258 + ], + [ + 16.92755, + 52.467264 + ], + [ + 16.927625, + 52.467263 + ], + [ + 16.927699, + 52.467246 + ], + [ + 16.92776, + 52.467212 + ], + [ + 16.927821, + 52.467158 + ], + [ + 16.927838, + 52.467099 + ], + [ + 16.927827, + 52.467059 + ], + [ + 16.927793, + 52.467012 + ], + [ + 16.927738, + 52.466976 + ], + [ + 16.927661, + 52.466949 + ], + [ + 16.927581, + 52.466939 + ], + [ + 16.927534, + 52.466938 + ], + [ + 16.927467, + 52.466949 + ], + [ + 16.927387, + 52.467047 + ], + [ + 16.927315, + 52.467153 + ], + [ + 16.92731, + 52.467158 + ] + ] + ], + "type": "Polygon" + }, + "id": "c779419e3fd7faef8555e1099547a82c" + }, + { + "type": "Feature", + "properties": { + "name": "Hol", + "point" : [52.467088, 16.927150] + }, + "geometry": { + "coordinates": [ + [ + [ + 16.926625, + 52.467294 + ], + [ + 16.926568, + 52.46719 + ], + [ + 16.927008, + 52.467108 + ], + [ + 16.926953, + 52.467024 + ], + [ + 16.927463, + 52.466919 + ], + [ + 16.927467, + 52.466956 + ], + [ + 16.927316, + 52.467153 + ], + [ + 16.926907, + 52.467235 + ], + [ + 16.926625, + 52.467294 + ] + ] + ], + "type": "Polygon" + }, + "id": "facdd5349991758b9ad99d4e123c91cc" + } + ], + "type": "FeatureCollection" +} \ No newline at end of file diff --git a/app/src/main/java/com/uam/wmi/findmytutor/activity/MapActivity.java b/app/src/main/java/com/uam/wmi/findmytutor/activity/MapActivity.java index 47da9ea..ab134d2 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/activity/MapActivity.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/activity/MapActivity.java @@ -235,6 +235,7 @@ public class MapActivity extends BaseActivity latLng.getLatitude(), latLng.getLongitude(), latLng.getAltitude(), + "approx", PrefUtils.getUserFirstName(getApplicationContext()) + " " + PrefUtils.getUserLastName(getApplicationContext()), PrefUtils.getUserId(getApplicationContext()), PrefUtils.getLocationLevel(getApplicationContext()) diff --git a/app/src/main/java/com/uam/wmi/findmytutor/model/Coordinate.java b/app/src/main/java/com/uam/wmi/findmytutor/model/Coordinate.java index 5c23701..1f838e4 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/model/Coordinate.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/model/Coordinate.java @@ -44,7 +44,7 @@ public class Coordinate extends BaseResponse { @SerializedName("label") private String label; - public Coordinate (Double latitude, Double longitude, Double altitude, 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 (!longtitudeRange.contains(longitude)) throw new IllegalArgumentException("Inappropriate longitude value" + longitude); @@ -53,6 +53,7 @@ public class Coordinate extends BaseResponse { this.altitude = altitude; this.label = label; this.userId = userId; + this.approximatedLocation = approximatedLocation; this.displayMode = displayMode; } diff --git a/app/src/main/java/com/uam/wmi/findmytutor/service/BackgroundLocalizationService.java b/app/src/main/java/com/uam/wmi/findmytutor/service/BackgroundLocalizationService.java index 2b3ca3c..44a0d08 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/service/BackgroundLocalizationService.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/service/BackgroundLocalizationService.java @@ -27,14 +27,16 @@ import com.google.android.gms.location.FusedLocationProviderClient; import com.google.android.gms.location.LocationServices; import com.google.android.gms.tasks.OnSuccessListener; import com.jakewharton.retrofit2.adapter.rxjava2.HttpException; +import com.mapbox.geojson.Point; import com.uam.wmi.findmytutor.model.Coordinate; import com.uam.wmi.findmytutor.network.ApiClient; +import com.uam.wmi.findmytutor.utils.ApproximatedLocalization; import com.uam.wmi.findmytutor.utils.PrefUtils; import com.uam.wmi.findmytutor.utils.RestApiHelper; +import com.uam.wmi.findmytutor.utils.mapUtils; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.Executor; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.CompositeDisposable; @@ -43,6 +45,7 @@ import io.reactivex.schedulers.Schedulers; import okhttp3.ResponseBody; import timber.log.Timber; +import static com.mapbox.geojson.Point.fromLngLat; import static com.uam.wmi.findmytutor.utils.PrefUtils.storeBackgroundLocationStatus; public class BackgroundLocalizationService extends Service { @@ -64,6 +67,8 @@ public class BackgroundLocalizationService extends Service { private Runnable mStatusChecker; private FusedLocationProviderClient mFusedLocationClient; + + public BackgroundLocalizationService() { providers.add(LocationManager.GPS_PROVIDER); providers.add(LocationManager.NETWORK_PROVIDER); @@ -74,6 +79,7 @@ public class BackgroundLocalizationService extends Service { new LocationListener(LocationManager.NETWORK_PROVIDER), new LocationListener(LocationManager.PASSIVE_PROVIDER) }; + } @Override @@ -292,20 +298,27 @@ public class BackgroundLocalizationService extends Service { private CoordinateService coordinateService = ApiClient.getClient(getApplicationContext()) .create(CoordinateService.class); + ApproximatedLocalization approximatedLocalization; + private Task(Location location) { latitude = location.getLatitude(); longitude = location.getLongitude(); altitude = location.getAltitude(); + approximatedLocalization = new ApproximatedLocalization(mapUtils.loadJsonFromAsset(getApplicationContext(),"building.geojson")); } @Override protected Object doInBackground(Object[] objects) { +/* + Point point = approximatedLocalization.getPointerBuildingPart("Aule"); + Log.e("APPROXIMATED", String.valueOf(point));*/ try { Coordinate coordinate = new Coordinate( latitude, longitude, altitude, + approximatedLocalization.getNameOfBuildingPart(Point.fromLngLat(longitude, latitude)), PrefUtils.getUserStatus(getApplicationContext()), PrefUtils.getUserId(getApplicationContext()), PrefUtils.getLocationLevel(getApplicationContext()) diff --git a/app/src/main/java/com/uam/wmi/findmytutor/utils/ApproximatedLocalization.java b/app/src/main/java/com/uam/wmi/findmytutor/utils/ApproximatedLocalization.java new file mode 100644 index 0000000..56308d1 --- /dev/null +++ b/app/src/main/java/com/uam/wmi/findmytutor/utils/ApproximatedLocalization.java @@ -0,0 +1,64 @@ +package com.uam.wmi.findmytutor.utils; + +import android.support.annotation.NonNull; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.mapbox.geojson.BoundingBox; +import com.mapbox.geojson.Feature; +import com.mapbox.geojson.FeatureCollection; +import com.mapbox.geojson.Geometry; +import com.mapbox.geojson.Point; +import com.mapbox.geojson.Polygon; +import com.mapbox.geojson.gson.BoundingBoxDeserializer; +import com.mapbox.geojson.gson.GeoJsonAdapterFactory; +import com.mapbox.geojson.gson.GeometryDeserializer; +import com.mapbox.geojson.gson.PointDeserializer; +import com.mapbox.turf.TurfJoins; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +public class ApproximatedLocalization { + private FeatureCollection buildingSchema = null; + + public ApproximatedLocalization(String buildingObject){ + buildingSchema = fromJson(buildingObject); + } + + private FeatureCollection fromJson(@NonNull String json) { + GsonBuilder gson = new GsonBuilder(); + gson.registerTypeAdapterFactory(GeoJsonAdapterFactory.create()); + gson.registerTypeAdapter(Point.class, new PointDeserializer()); + gson.registerTypeAdapter(Geometry.class, new GeometryDeserializer()); + gson.registerTypeAdapter(BoundingBox.class, new BoundingBoxDeserializer()); + return gson.create().fromJson(json, FeatureCollection.class); + } + + public String getNameOfBuildingPart(Point point) { + + for(Feature feature : Objects.requireNonNull(buildingSchema.features())){ + boolean isInside = TurfJoins.inside(point, (Polygon) Objects.requireNonNull(feature.geometry())); + + if (isInside){ + return Objects.requireNonNull(Objects.requireNonNull(feature.getStringProperty("name"))); + } + } + + return null; + } +/* + public Point getMiddlePointForBuildingPart(String BuildingPart) { + for(Feature feature : Objects.requireNonNull(buildingSchema.features())){ + String partName = feature.getStringProperty("name"); + + if(BuildingPart.equals(partName)) { + JsonElement pointsList = feature.getProperty("point"); + return Point.fromLngLat(pointsList.getAsNumber().doubleValue(), pointsList.getAsJsonArray().get(1)); + } + } + + return Point.fromLngLat(0,0); + }*/ +} diff --git a/app/src/main/java/com/uam/wmi/findmytutor/utils/mapUtils.java b/app/src/main/java/com/uam/wmi/findmytutor/utils/mapUtils.java index a58ca10..6c7b948 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/utils/mapUtils.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/utils/mapUtils.java @@ -2,6 +2,7 @@ package com.uam.wmi.findmytutor.utils; import android.animation.TypeEvaluator; import android.content.Context; +import android.content.res.AssetManager; import com.mapbox.mapboxsdk.geometry.LatLng; @@ -41,4 +42,5 @@ public class mapUtils { return null; } } + } diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index efbf417..be1a4f3 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -1,19 +1,17 @@ - @color/mapboxGray - @color/mapboxGrayDark10 - @color/mapboxPink - #cf5e5e + #476483 + #3A3553 + #CF0E0F + #89c3c3c3 #89c3c3c3 #858585 #232323 - #d1e200f6 #ffffff #80ffffff - #F5F5F5 #dfdfdf