Skip to content
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

Use last style values as defaults and generate new colours for additional layers of the same type #2399

Merged
merged 3 commits into from
Sep 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/main/java/de/blau/android/Map.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public void setUpLayers(@NonNull Context ctx) {
layer = new de.blau.android.layer.tasks.MapOverlay(this);
break;
case GEOJSON:
layer = new de.blau.android.layer.geojson.MapOverlay(this);
layer = new de.blau.android.layer.geojson.MapOverlay(this, contentId);
if (!((de.blau.android.layer.geojson.MapOverlay) layer).loadGeoJsonFile(ctx, Uri.parse(contentId), true)) {
// other error, has already been toasted
layer = null; // this will delete the layer
Expand Down Expand Up @@ -376,6 +376,24 @@ private List<MapViewLayer> getLayers(@NonNull LayerType type, @Nullable String c
return result;
}

/**
* Get a count of layers of a specific type
*
* @param type the LayerType
* @return a count
*/
public int getLayerTypeCount(@NonNull LayerType type) {
int count = 0;
synchronized (mLayers) {
for (MapViewLayer l : mLayers) {
if (l.getType().equals(type)) {
count++;
}
}
}
return count;
}

/**
* Get the list of configured layers
*
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/de/blau/android/Splash.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ protected Void doInBackground(Void param) {
}
// read Presets here to avoid reading them on UI thread on startup of Main
Progress.showDialog(Splash.this, Progress.PROGRESS_LOADING_PRESET);
Log.d(DEBUG_TAG, "Initial preset load");
App.getCurrentPresets(Splash.this);
Log.d(DEBUG_TAG, "Preset load finished");
//
Intent intent = new Intent(Splash.this, Main.class);
intent.putExtra(SHORTCUT_EXTRAS_KEY, shortcutExtras);
Expand Down
1 change: 0 additions & 1 deletion src/main/java/de/blau/android/dialogs/Layers.java
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,6 @@ private void addStyleableLayerFromUri(@NonNull final FragmentActivity activity,
map.setUpLayers(activity);
layer = (de.blau.android.layer.StyleableLayer) map.getLayer(type, uriString);
if (layer != null) { // if null setUpLayers will have toasted
layer.resetStyling();
LayerStyle.showDialog(activity, layer.getIndex());
SelectFile.savePref(prefs, R.string.config_osmPreferredDir_key, fileUri);
layer.invalidate();
Expand Down
59 changes: 59 additions & 0 deletions src/main/java/de/blau/android/layer/StyleableFileLayer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package de.blau.android.layer;

import java.io.InputStream;

import android.content.Context;
import android.net.Uri;
import androidx.annotation.NonNull;
import de.blau.android.contract.FileExtensions;
import de.blau.android.util.Hash;

/**
* StyleableLayer that is loaded from a file
*
* @author simon
*
*/
public abstract class StyleableFileLayer extends StyleableLayer {

private static final long serialVersionUID = 1L;

/**
* State file file name
*/
protected String stateFileName;

protected String contentId; // could potentially be transient

protected StyleableFileLayer(@NonNull String contentId, String defaultStateFileName) {
this.contentId = contentId;
this.stateFileName = defaultStateFileName;
}

/**
* Check if we have a state file
*
* @param context an Android Context
* @return true if a state file exists
*/
protected boolean hasStateFile(@NonNull Context context) {
setStateFileName(Uri.parse(contentId).getEncodedPath());
try (InputStream stream = context.openFileInput(stateFileName)) {
return true;
} catch (Exception ex) {
return false;
}
}

/**
* Set the name of the state file
*
* This needs to be unique across all instances so best an encoded uri, to avoid filename length issues we use the
* SHA-256 hash
*
* @param baseName the base name for this specific instance
*/
protected void setStateFileName(@NonNull String baseName) {
stateFileName = Hash.sha256(baseName) + "." + FileExtensions.RES;
}
}
105 changes: 64 additions & 41 deletions src/main/java/de/blau/android/layer/geojson/MapOverlay.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,23 @@
import de.blau.android.layer.LabelMinZoomInterface;
import de.blau.android.layer.LayerInfoInterface;
import de.blau.android.layer.LayerType;
import de.blau.android.layer.StyleableFileLayer;
import de.blau.android.layer.StyleableLayer;
import de.blau.android.osm.BoundingBox;
import de.blau.android.osm.OsmXml;
import de.blau.android.osm.Server;
import de.blau.android.osm.ViewBox;
import de.blau.android.prefs.Preferences;
import de.blau.android.resources.DataStyle;
import de.blau.android.resources.DataStyle.FeatureStyle;
import de.blau.android.resources.symbols.TriangleDown;
import de.blau.android.util.ColorUtil;
import de.blau.android.util.ContentResolverUtil;
import de.blau.android.util.ExecutorTask;
import de.blau.android.util.FileUtil;
import de.blau.android.util.GeoJSONConstants;
import de.blau.android.util.GeoJson;
import de.blau.android.util.GeoMath;
import de.blau.android.util.Hash;
import de.blau.android.util.SavingHelper;
import de.blau.android.util.SerializableTextPaint;
import de.blau.android.util.Snack;
Expand All @@ -78,7 +80,7 @@
import de.blau.android.util.rtree.RTree;
import de.blau.android.views.IMapView;

public class MapOverlay extends StyleableLayer
public class MapOverlay extends StyleableFileLayer
implements Serializable, ExtentInterface, DiscardInterface, ClickableInterface<Feature>, LayerInfoInterface, LabelMinZoomInterface {

private static final long serialVersionUID = 4L;
Expand Down Expand Up @@ -182,19 +184,20 @@ private void readObject(java.io.ObjectInputStream in) throws IOException, ClassN
*/
private String uri;

/**
* State file file name
*/
private String stateFileName = FILENAME;

/**
* Construct this layer
*
* @param map the Map object we are displayed on
* @param contentId the id for the current contents
*/
public MapOverlay(final Map map) {
public MapOverlay(@NonNull final Map map, @NonNull String contentId) {
super(contentId, FILENAME);
this.map = map;
resetStyling();
final Preferences prefs = map.getPrefs();
initStyling(!hasStateFile(map.getContext()), prefs.getGeoJsonStrokeWidth(), prefs.getGeoJsonLabelSource(), prefs.getGeoJsonLabelMinZoom(),
prefs.getGeoJsonSynbol());
paint.setColor(
ColorUtil.generateColor(map.getLayerTypeCount(LayerType.GEOJSON), 9, DataStyle.getInternal(DataStyle.GEOJSON_DEFAULT).getPaint().getColor()));
}

@Override
Expand Down Expand Up @@ -418,7 +421,7 @@ protected Boolean doInBackground(Void arg) {
if (name == null) {
name = uri.getLastPathSegment();
}
setFileName(uri.getEncodedPath());
setStateFileName(uri.getEncodedPath());
MapOverlay.this.uri = uri.toString();
return loadGeoJsonFile(ctx, is, fromState);
} catch (SecurityException sex) {
Expand All @@ -442,17 +445,6 @@ protected Boolean doInBackground(Void arg) {
}
}

/**
* Set the name of the state file
*
* This needs to be unique across all instances so best an encoded uri
*
* @param baseName the base name for this specific instance
*/
private void setFileName(@NonNull String baseName) {
stateFileName = Hash.sha256(baseName) + "." + FileExtensions.RES;
}

/**
* Read an InputStream containing GeoJSON data in to the layer, replacing any existing data
*
Expand Down Expand Up @@ -707,41 +699,65 @@ public List<Feature> getFeatures() {

@Override
public void resetStyling() {
initStyling(true, DataStyle.DEFAULT_GEOJSON_STROKE_WIDTH, "", Map.SHOW_LABEL_LIMIT, TriangleDown.NAME);
}

/**
* Init the styling to the provided values
*
* @param style if true set styling
* @param strokeWidth the stroke width
* @param labelKey the source of the label
* @param labelMinZoom min. zoom from on we show the label
* @param symbolName the name of the point symbol
*/
private void initStyling(boolean style, float strokeWidth, @NonNull String labelKey, int labelMinZoom, String symbolName) {
paint = new SerializableTextPaint(DataStyle.getInternal(DataStyle.GEOJSON_DEFAULT).getPaint());
labelKey = "";
labelMinZoom = Map.SHOW_LABEL_LIMIT;
iconRadius = map.getIconRadius();
marker = DataStyle.getCurrent().getSymbol(TriangleDown.NAME);
if (style) {
setStrokeWidth(strokeWidth);
setLabel(labelKey);
setLabelMinZoom(labelMinZoom);
setPointSymbol(symbolName);
}
}

@Override
public List<String> getLabelList() {
if (data != null) {
Collection<BoundedFeature> queryResult = new ArrayList<>();
data.query(queryResult);
Set<String> result = new TreeSet<>();
for (BoundedFeature bf : queryResult) {
Feature feature = bf.getFeature();
if (feature != null) {
JsonObject properties = feature.properties();
if (properties != null) {
for (String key : properties.keySet()) {
JsonElement e = properties.get(key);
if (e != null && e.isJsonPrimitive()) {
result.add(key);
}
}
}
if (data == null) {
return super.getLabelList();
}
Collection<BoundedFeature> queryResult = new ArrayList<>();
data.query(queryResult);
Set<String> result = new TreeSet<>();
for (BoundedFeature bf : queryResult) {
Feature feature = bf.getFeature();
JsonObject properties = feature != null ? feature.properties() : null;
if (properties == null) {
continue;
}
for (String key : properties.keySet()) {
JsonElement e = properties.get(key);
if (e != null && e.isJsonPrimitive()) {
result.add(key);
}
}
return new ArrayList<>(result);

}
return super.getLabelList();
return new ArrayList<>(result);
}

@Override
public void setStrokeWidth(float width) {
super.setStrokeWidth(width);
map.getPrefs().setGeoJsonStrokeWidth(width);
}

@Override
public void setLabel(String key) {
labelKey = key;
map.getPrefs().setGeoJsonLabelSource(key);
}

@Override
Expand All @@ -752,13 +768,20 @@ public String getLabel() {
@Override
public void setLabelMinZoom(int minZoom) {
labelMinZoom = minZoom;
map.getPrefs().setGeoJsonLabelMinZoom(minZoom);
}

@Override
public int getLabelMinZoom() {
return labelMinZoom;
}

@Override
public void setPointSymbol(@NonNull String symbol) {
super.setPointSymbol(symbol);
map.getPrefs().setGeoJsonSymbol(symbol);
}

/**
* Get the label value for this Feature
*
Expand Down
Loading