kopia lustrzana https://github.com/JOSM/MapWithAI
Create a new MapPaintUtils class.
This class has methods that have been moved from MapWithAIDataUtils. It also has some new methods to modify a MapWithAI.mapcss file inplace (special handling for zip files from JOSM website). Signed-off-by: Taylor Smock <taylor.smock@kaart.com>pull/1/head
rodzic
9dc02dee33
commit
9b34329209
|
@ -30,7 +30,6 @@ import org.openstreetmap.josm.plugins.Plugin;
|
|||
import org.openstreetmap.josm.plugins.PluginInformation;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.backend.DownloadListener;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIAction;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIDataUtils;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAILayer;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIMoveAction;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIObject;
|
||||
|
@ -47,6 +46,7 @@ import org.openstreetmap.josm.plugins.mapwithai.gui.MapWithAIMenu;
|
|||
import org.openstreetmap.josm.plugins.mapwithai.gui.download.MapWithAIDownloadOptions;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.gui.download.MapWithAIDownloadSourceType;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.gui.preferences.MapWithAIPreferences;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.tools.MapPaintUtils;
|
||||
import org.openstreetmap.josm.spi.preferences.Config;
|
||||
import org.openstreetmap.josm.tools.Destroyable;
|
||||
import org.openstreetmap.josm.tools.Logging;
|
||||
|
@ -111,10 +111,10 @@ public final class MapWithAIPlugin extends Plugin implements Destroyable {
|
|||
});
|
||||
|
||||
if (!Config.getPref().getKeySet().contains(PAINTSTYLE_PREEXISTS)) {
|
||||
Config.getPref().putBoolean(PAINTSTYLE_PREEXISTS, MapWithAIDataUtils.checkIfMapWithAIPaintStyleExists());
|
||||
Config.getPref().putBoolean(PAINTSTYLE_PREEXISTS, MapPaintUtils.checkIfMapWithAIPaintStyleExists());
|
||||
}
|
||||
|
||||
MapWithAIDataUtils.addMapWithAIPaintStyles();
|
||||
MapPaintUtils.addMapWithAIPaintStyles();
|
||||
|
||||
destroyables = new ArrayList<>();
|
||||
MapWithAIDownloadOptions mapWithAIDownloadOptions = new MapWithAIDownloadOptions();
|
||||
|
@ -178,7 +178,7 @@ public final class MapWithAIPlugin extends Plugin implements Destroyable {
|
|||
.forEach(layer -> MainApplication.getLayerManager().removeLayer(layer));
|
||||
|
||||
if (!Config.getPref().getBoolean(PAINTSTYLE_PREEXISTS)) {
|
||||
MapWithAIDataUtils.removeMapWithAIPaintStyles();
|
||||
MapPaintUtils.removeMapWithAIPaintStyles();
|
||||
}
|
||||
|
||||
destroyables.forEach(Destroyable::destroy);
|
||||
|
|
|
@ -31,6 +31,7 @@ import org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin;
|
|||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIConflationCategory;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIType;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.tools.MapPaintUtils;
|
||||
import org.openstreetmap.josm.tools.HttpClient;
|
||||
import org.openstreetmap.josm.tools.Logging;
|
||||
|
||||
|
@ -99,6 +100,7 @@ public class BoundingBoxMapWithAIDownloader extends BoundingBoxDownloader {
|
|||
Logging.error(e);
|
||||
}
|
||||
}
|
||||
MapPaintUtils.addSourcesToPaintStyle(externalData);
|
||||
return externalData;
|
||||
} catch (OsmApiException e) {
|
||||
if (!(e.getResponseCode() == 504 && (System.nanoTime() - lastErrorTime) < 120_000_000_000L)) {
|
||||
|
@ -115,6 +117,7 @@ public class BoundingBoxMapWithAIDownloader extends BoundingBoxDownloader {
|
|||
throw e;
|
||||
}
|
||||
}
|
||||
// Just in case something happens, try again...
|
||||
DataSet ds = new DataSet();
|
||||
GetDataRunnable runnable = new GetDataRunnable(downloadArea.toBBox(), ds, NullProgressMonitor.INSTANCE);
|
||||
runnable.setMapWithAIInfo(info);
|
||||
|
@ -128,6 +131,8 @@ public class BoundingBoxMapWithAIDownloader extends BoundingBoxDownloader {
|
|||
}
|
||||
runnable.compute();
|
||||
});
|
||||
|
||||
MapPaintUtils.addSourcesToPaintStyle(ds);
|
||||
return ds;
|
||||
}
|
||||
|
||||
|
|
|
@ -12,7 +12,6 @@ import java.util.Objects;
|
|||
import java.util.TreeSet;
|
||||
import java.util.concurrent.ForkJoinPool;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.swing.JOptionPane;
|
||||
|
@ -30,9 +29,6 @@ import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
|
|||
import org.openstreetmap.josm.gui.MainApplication;
|
||||
import org.openstreetmap.josm.gui.Notification;
|
||||
import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
||||
import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
|
||||
import org.openstreetmap.josm.gui.mappaint.StyleSource;
|
||||
import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
|
||||
import org.openstreetmap.josm.gui.progress.swing.PleaseWaitProgressMonitor;
|
||||
import org.openstreetmap.josm.gui.util.GuiHelper;
|
||||
import org.openstreetmap.josm.io.OsmTransferException;
|
||||
|
@ -56,67 +52,10 @@ public final class MapWithAIDataUtils {
|
|||
private static ForkJoinPool forkJoinPool;
|
||||
static final Object LAYER_LOCK = new Object();
|
||||
|
||||
/** The default url for the MapWithAI paint style */
|
||||
public static final String DEFAULT_PAINT_STYLE_RESOURCE_URL = "https://josm.openstreetmap.de/josmfile?page=Styles/MapWithAI&zip=1";
|
||||
|
||||
private static String paintStyleResourceUrl = DEFAULT_PAINT_STYLE_RESOURCE_URL;
|
||||
private static final Pattern TEST_PATTERN = Pattern
|
||||
.compile("^https?:\\/\\/(www\\.)?localhost[:0-9]*\\/josmfile\\?page=Styles\\/MapWithAI&zip=1$");
|
||||
|
||||
private MapWithAIDataUtils() {
|
||||
// Hide the constructor
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a paintstyle from the jar
|
||||
*/
|
||||
public static void addMapWithAIPaintStyles() {
|
||||
// Remove old url's that were automatically added -- remove after Jan 01, 2020
|
||||
final List<Pattern> oldUrls = Arrays.asList(Pattern.compile(
|
||||
"https://gitlab.com/(gokaart/JOSM_MapWithAI|smocktaylor/rapid)/raw/master/src/resources/styles/standard/(mapwithai|rapid).mapcss"),
|
||||
TEST_PATTERN, Pattern.compile("resource://styles/standard/mapwithai.mapcss"));
|
||||
new ArrayList<>(MapPaintStyles.getStyles().getStyleSources()).parallelStream()
|
||||
.filter(style -> oldUrls.stream().anyMatch(p -> p.matcher(style.url).matches()))
|
||||
.forEach(MapPaintStyles::removeStyle);
|
||||
|
||||
if (!checkIfMapWithAIPaintStyleExists()) {
|
||||
final MapCSSStyleSource style = new MapCSSStyleSource(paintStyleResourceUrl, MapWithAIPlugin.NAME,
|
||||
"MapWithAI");
|
||||
MapPaintStyles.addStyle(style);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for the existance of a MapWithAI paint style
|
||||
*
|
||||
* @return true if a MapWithAI paint style exists
|
||||
*/
|
||||
public static boolean checkIfMapWithAIPaintStyleExists() {
|
||||
return MapPaintStyles.getStyles().getStyleSources().parallelStream().filter(MapCSSStyleSource.class::isInstance)
|
||||
.map(MapCSSStyleSource.class::cast).anyMatch(source -> paintStyleResourceUrl.equals(source.url)
|
||||
|| TEST_PATTERN.matcher(source.url).matches());
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove MapWithAI paint styles
|
||||
*/
|
||||
public static void removeMapWithAIPaintStyles() {
|
||||
new ArrayList<>(MapPaintStyles.getStyles().getStyleSources()).parallelStream().filter(
|
||||
source -> paintStyleResourceUrl.equals(source.url) || TEST_PATTERN.matcher(source.url).matches())
|
||||
.forEach(style -> GuiHelper.runInEDT(() -> MapPaintStyles.removeStyle(style)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get any MapWithAI paint style
|
||||
*
|
||||
* @return get the MapWithAI Paint style
|
||||
*/
|
||||
public static StyleSource getMapWithAIPaintStyle() {
|
||||
return MapPaintStyles.getStyles().getStyleSources().parallelStream().filter(
|
||||
source -> paintStyleResourceUrl.equals(source.url) || TEST_PATTERN.matcher(source.url).matches())
|
||||
.findAny().orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add primitives and their children to a collection
|
||||
*
|
||||
|
@ -573,22 +512,4 @@ public final class MapWithAIDataUtils {
|
|||
.parallelStream().filter(MapWithAIAddCommand.class::isInstance).map(MapWithAIAddCommand.class::cast)
|
||||
.flatMap(com -> com.getSourceTags().stream()).distinct().collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the URL for the MapWithAI paint style
|
||||
*
|
||||
* @param paintUrl The paint style for MapWithAI
|
||||
*/
|
||||
public static void setPaintStyleUrl(String paintUrl) {
|
||||
paintStyleResourceUrl = paintUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the url for the paint style for MapWithAI
|
||||
*
|
||||
* @return The url for the paint style
|
||||
*/
|
||||
public static String getPaintStyleUrl() {
|
||||
return paintStyleResourceUrl;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ import static org.openstreetmap.josm.tools.I18n.tr;
|
|||
import java.awt.Component;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
@ -44,6 +45,7 @@ import org.openstreetmap.josm.gui.mappaint.StyleSource;
|
|||
import org.openstreetmap.josm.gui.util.GuiHelper;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.tools.MapPaintUtils;
|
||||
import org.openstreetmap.josm.spi.preferences.Config;
|
||||
import org.openstreetmap.josm.tools.GBC;
|
||||
import org.openstreetmap.josm.tools.ImageProvider;
|
||||
|
@ -176,7 +178,7 @@ public class MapWithAILayer extends OsmDataLayer implements ActiveLayerChangeLis
|
|||
@Override
|
||||
public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
|
||||
if (checkIfToggleLayer()) {
|
||||
final StyleSource style = MapWithAIDataUtils.getMapWithAIPaintStyle();
|
||||
final StyleSource style = MapPaintUtils.getMapWithAIPaintStyle();
|
||||
if (style.active != this.equals(MainApplication.getLayerManager().getActiveLayer())) {
|
||||
MapPaintStyles.toggleStyleActive(MapPaintStyles.getStyles().getStyleSources().indexOf(style));
|
||||
}
|
||||
|
@ -350,4 +352,10 @@ public class MapWithAILayer extends OsmDataLayer implements ActiveLayerChangeLis
|
|||
public Collection<MapWithAIInfo> getDownloadedInfo() {
|
||||
return Collections.unmodifiableCollection(downloadedInfo);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean autosave(File file) throws IOException {
|
||||
// Consider a deletetion a "successful" save.
|
||||
return file.delete();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,289 @@
|
|||
// License: GPL. For details, see LICENSE file.
|
||||
package org.openstreetmap.josm.plugins.mapwithai.tools;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.zip.CRC32;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipException;
|
||||
import java.util.zip.ZipFile;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import org.openstreetmap.josm.data.osm.DataSet;
|
||||
import org.openstreetmap.josm.data.osm.IPrimitive;
|
||||
import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
|
||||
import org.openstreetmap.josm.gui.mappaint.StyleSetting;
|
||||
import org.openstreetmap.josm.gui.mappaint.StyleSetting.StyleSettingGroup;
|
||||
import org.openstreetmap.josm.gui.mappaint.StyleSource;
|
||||
import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
|
||||
import org.openstreetmap.josm.gui.util.GuiHelper;
|
||||
import org.openstreetmap.josm.io.CachedFile;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin;
|
||||
import org.openstreetmap.josm.tools.ColorHelper;
|
||||
import org.openstreetmap.josm.tools.Logging;
|
||||
|
||||
public class MapPaintUtils {
|
||||
/** The default url for the MapWithAI paint style */
|
||||
public static final String DEFAULT_PAINT_STYLE_RESOURCE_URL = "https://josm.openstreetmap.de/josmfile?page=Styles/MapWithAI&zip=1";
|
||||
|
||||
private static String paintStyleResourceUrl = DEFAULT_PAINT_STYLE_RESOURCE_URL;
|
||||
private static final Pattern TEST_PATTERN = Pattern
|
||||
.compile("^https?:\\/\\/(www\\.)?localhost[:0-9]*\\/josmfile\\?page=Styles\\/MapWithAI&zip=1$");
|
||||
|
||||
private static final String SOURCE_KEY = "source";
|
||||
private static final String MAPWITHAI_SOURCE_KEY = "mapwithai:" + SOURCE_KEY;
|
||||
|
||||
private static final String MAPWITHAI_MAPCSS_ZIP_NAME = "Styles_MapWithAI-style.mapcss";
|
||||
private static final double CRC_DIVIDE_TO_TEN_K_MAX = 429496.7296;
|
||||
|
||||
static enum SafeColors {
|
||||
RED(Color.RED), ORANGE(Color.ORANGE), GOLD(ColorHelper.html2color("#ffd700")),
|
||||
LIME(ColorHelper.html2color("#00ff00")), CYAN(Color.CYAN), DODGER_BLUE(ColorHelper.html2color("#1e90ff")),
|
||||
AI_MAGENTA(ColorHelper.html2color("#ff26d4")), PINK(Color.PINK), LIGHT_GREY(ColorHelper.html2color("#d3d3d3")),
|
||||
LINEN(ColorHelper.html2color("#faf0e6"));
|
||||
|
||||
private final Color color;
|
||||
|
||||
SafeColors(Color color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the safe color
|
||||
*/
|
||||
public Color getColor() {
|
||||
return this.color;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a paintstyle from the jar
|
||||
*/
|
||||
public static void addMapWithAIPaintStyles() {
|
||||
// Remove old url's that were automatically added -- remove after Jan 01, 2020
|
||||
final List<Pattern> oldUrls = Arrays.asList(Pattern.compile(
|
||||
"https://gitlab.com/(gokaart/JOSM_MapWithAI|smocktaylor/rapid)/raw/master/src/resources/styles/standard/(mapwithai|rapid).mapcss"),
|
||||
TEST_PATTERN, Pattern.compile("resource://styles/standard/mapwithai.mapcss"));
|
||||
new ArrayList<>(MapPaintStyles.getStyles().getStyleSources()).parallelStream()
|
||||
.filter(style -> oldUrls.stream().anyMatch(p -> p.matcher(style.url).matches()))
|
||||
.forEach(MapPaintStyles::removeStyle);
|
||||
|
||||
if (!checkIfMapWithAIPaintStyleExists()) {
|
||||
final MapCSSStyleSource style = new MapCSSStyleSource(paintStyleResourceUrl, MapWithAIPlugin.NAME,
|
||||
"MapWithAI");
|
||||
MapPaintStyles.addStyle(style);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean checkIfMapWithAIPaintStyleExists() {
|
||||
return MapPaintStyles.getStyles().getStyleSources().parallelStream().filter(MapCSSStyleSource.class::isInstance)
|
||||
.map(MapCSSStyleSource.class::cast).anyMatch(source -> paintStyleResourceUrl.equals(source.url)
|
||||
|| TEST_PATTERN.matcher(source.url).matches());
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove MapWithAI paint styles
|
||||
*/
|
||||
public static void removeMapWithAIPaintStyles() {
|
||||
new ArrayList<>(MapPaintStyles.getStyles().getStyleSources()).parallelStream().filter(
|
||||
source -> paintStyleResourceUrl.equals(source.url) || TEST_PATTERN.matcher(source.url).matches())
|
||||
.forEach(style -> GuiHelper.runInEDT(() -> MapPaintStyles.removeStyle(style)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get any MapWithAI paint style
|
||||
*
|
||||
* @return get the MapWithAI Paint style
|
||||
*/
|
||||
public static StyleSource getMapWithAIPaintStyle() {
|
||||
return MapPaintStyles.getStyles().getStyleSources().parallelStream().filter(
|
||||
source -> paintStyleResourceUrl.equals(source.url) || TEST_PATTERN.matcher(source.url).matches())
|
||||
.findAny().orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the URL for the MapWithAI paint style
|
||||
*
|
||||
* @param paintUrl The paint style for MapWithAI
|
||||
*/
|
||||
public static void setPaintStyleUrl(String paintUrl) {
|
||||
paintStyleResourceUrl = paintUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the url for the paint style for MapWithAI
|
||||
*
|
||||
* @return The url for the paint style
|
||||
*/
|
||||
public static String getPaintStyleUrl() {
|
||||
return paintStyleResourceUrl;
|
||||
}
|
||||
|
||||
/** Add sources to the paint style */
|
||||
public static synchronized void addSourcesToPaintStyle(DataSet ds) {
|
||||
List<String> sources = ds.allPrimitives().stream().map(MapPaintUtils::getSourceValue).filter(Objects::nonNull)
|
||||
.distinct().collect(Collectors.toList());
|
||||
StyleSource styleSource = getMapWithAIPaintStyle();
|
||||
if (!styleSource.isLoaded()) {
|
||||
styleSource.loadStyleSource();
|
||||
}
|
||||
List<StyleSetting> list = styleSource.settings;
|
||||
for (StyleSetting setting : list) {
|
||||
if (setting instanceof StyleSetting.ColorStyleSetting) {
|
||||
StyleSetting.ColorStyleSetting csetting = (StyleSetting.ColorStyleSetting) setting;
|
||||
if (csetting.label != null) {
|
||||
String rLabel = csetting.label.replaceAll("color$", "").trim();
|
||||
sources.removeIf(rLabel::equalsIgnoreCase);
|
||||
}
|
||||
}
|
||||
}
|
||||
Map<StyleSettingGroup, List<StyleSetting>> groups = styleSource.settingGroups;
|
||||
String group = groups.keySet().stream().filter(p -> p.key != null && p.key.contains("color")).map(p -> p.key)
|
||||
.findFirst().orElse(null);
|
||||
try (CachedFile cachedFile = styleSource.getCachedFile()) {
|
||||
File file = cachedFile.getFile();
|
||||
String path = file.getAbsolutePath();
|
||||
for (String prefix : Arrays.asList("file:", "jar:")) {
|
||||
if (!path.startsWith(prefix)) {
|
||||
path = prefix.concat(path);
|
||||
}
|
||||
}
|
||||
try {
|
||||
ZipFile zipFile = new ZipFile(file.getAbsolutePath());
|
||||
writeZipData(zipFile, group, sources);
|
||||
} catch (ZipException e) {
|
||||
// Assume that it is a standard file, not a zip file.
|
||||
OutputStream out = new FileOutputStream(file.getName() + ".tmp");
|
||||
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
|
||||
writeData(out, bufferedReader, group, sources);
|
||||
bufferedReader.close();
|
||||
out.close();
|
||||
Files.move(new File(file.getName() + ".tmp").toPath(), new File(file.getName()).toPath(),
|
||||
StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
|
||||
}
|
||||
|
||||
styleSource.loadStyleSource();
|
||||
} catch (IOException e) {
|
||||
Logging.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void writeZipData(ZipFile file, String group, List<String> sources) throws IOException {
|
||||
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(file.getName() + ".tmp"));
|
||||
for (Enumeration<? extends ZipEntry> e = file.entries(); e.hasMoreElements();) {
|
||||
ZipEntry current = e.nextElement();
|
||||
// For the entry we are overwriting, we cannot use the current zipentry, we must
|
||||
// make a new one.
|
||||
if (!current.getName().equalsIgnoreCase(MAPWITHAI_MAPCSS_ZIP_NAME)) {
|
||||
out.putNextEntry(current);
|
||||
InputStream is = file.getInputStream(current);
|
||||
byte[] buf = new byte[1024];
|
||||
int len;
|
||||
while ((len = is.read(buf)) > 0) {
|
||||
out.write(buf, 0, len);
|
||||
}
|
||||
is.close();
|
||||
out.closeEntry();
|
||||
continue;
|
||||
}
|
||||
out.putNextEntry(new ZipEntry(MAPWITHAI_MAPCSS_ZIP_NAME));
|
||||
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(file.getInputStream(current)));
|
||||
writeData(out, bufferedReader, group, sources);
|
||||
bufferedReader.close();
|
||||
out.closeEntry();
|
||||
}
|
||||
out.close();
|
||||
Files.move(new File(file.getName() + ".tmp").toPath(), new File(file.getName()).toPath(),
|
||||
StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
|
||||
}
|
||||
|
||||
private static void writeData(OutputStream out, BufferedReader bufferedReader, String group, List<String> sources)
|
||||
throws IOException {
|
||||
String line = bufferedReader.readLine();
|
||||
while (!line.contains("End Settings for the paint style")) {
|
||||
out.write(line.getBytes());
|
||||
out.write(System.lineSeparator().getBytes());
|
||||
line = bufferedReader.readLine();
|
||||
}
|
||||
/* Finish writing the comment */
|
||||
while (!line.endsWith("*/")) {
|
||||
out.write(line.getBytes());
|
||||
out.write(System.lineSeparator().getBytes());
|
||||
line = bufferedReader.readLine();
|
||||
}
|
||||
out.write(line.getBytes());
|
||||
out.write(System.lineSeparator().getBytes());
|
||||
|
||||
for (String source : sources) {
|
||||
out.write(System.lineSeparator().getBytes());
|
||||
String simpleSource = source.replaceAll("[() /\\${}:]", "_");
|
||||
StringBuilder sb = new StringBuilder("setting::").append(simpleSource).append("{").append("type:color;")
|
||||
.append("default:").append(simpleSource).append(ColorHelper.color2html(getRandomColor(source)))
|
||||
.append(";label:tr(\"{0} color\",\"").append(source).append("\");");
|
||||
if (group != null) {
|
||||
sb.append("group:\"").append(group).append("\";");
|
||||
}
|
||||
sb.append("}");
|
||||
out.write(sb.toString().getBytes());
|
||||
out.write(System.lineSeparator().getBytes());
|
||||
sb = new StringBuilder(
|
||||
"*[/^(source|mapwithai:source)$/][any(tag(\"source\"), tag(\"mapwithai:source\"))=\"")
|
||||
.append(source).append("\"]{set_color_programatic:setting(\"").append(simpleSource)
|
||||
.append("\");}");
|
||||
out.write(sb.toString().getBytes());
|
||||
}
|
||||
while ((line = bufferedReader.readLine()) != null) {
|
||||
out.write(line.getBytes());
|
||||
out.write(System.lineSeparator().getBytes());
|
||||
}
|
||||
}
|
||||
|
||||
private static Color getRandomColor(String sourceName) {
|
||||
if (Arrays.asList("mapwithai", "maxar", "digitalglobe", "microsoft/").stream()
|
||||
.anyMatch(i -> sourceName.toLowerCase().contains(i.toLowerCase()))) {
|
||||
return SafeColors.AI_MAGENTA.getColor();
|
||||
}
|
||||
SafeColors[] colors = Stream.of(SafeColors.values()).filter(c -> SafeColors.AI_MAGENTA != c)
|
||||
.toArray(SafeColors[]::new);
|
||||
CRC32 crc = new CRC32();
|
||||
crc.update(sourceName.getBytes());
|
||||
|
||||
double bucket = crc.getValue() / CRC_DIVIDE_TO_TEN_K_MAX;
|
||||
double bucket_size = 10_000 / colors.length;
|
||||
for (int i = 1; i <= colors.length; i++) {
|
||||
if (bucket < bucket_size * i) {
|
||||
return colors[i - 1].getColor();
|
||||
}
|
||||
}
|
||||
return colors[colors.length - 1].getColor();
|
||||
}
|
||||
|
||||
private static String getSourceValue(IPrimitive p) {
|
||||
if (p.hasTag(SOURCE_KEY)) {
|
||||
return p.get(SOURCE_KEY);
|
||||
}
|
||||
if (p.hasTag(MAPWITHAI_SOURCE_KEY)) {
|
||||
return p.get(MAPWITHAI_SOURCE_KEY);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -22,9 +22,9 @@ import org.junit.Test;
|
|||
import org.openstreetmap.josm.gui.MainApplication;
|
||||
import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
|
||||
import org.openstreetmap.josm.plugins.PluginInformation;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIDataUtils;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.gui.preferences.MapWithAIPreferences;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.testutils.MapWithAITestRules;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.tools.MapPaintUtils;
|
||||
import org.openstreetmap.josm.spi.preferences.Config;
|
||||
import org.openstreetmap.josm.testutils.JOSMTestRules;
|
||||
import org.openstreetmap.josm.testutils.mockers.WindowMocker;
|
||||
|
@ -37,7 +37,7 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
|||
public class MapWithAIPluginTest {
|
||||
@Rule
|
||||
@SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
||||
public JOSMTestRules test = new MapWithAITestRules().sources().wiremock().preferences().main();
|
||||
public JOSMTestRules test = new MapWithAITestRules().sources().wiremock().preferences().main().projection();
|
||||
|
||||
public PluginInformation info;
|
||||
public MapWithAIPlugin plugin;
|
||||
|
@ -108,7 +108,7 @@ public class MapWithAIPluginTest {
|
|||
assertEquals(dataMenuSize, dataMenu.getMenuComponentCount(),
|
||||
"Menu items were added after they were already added");
|
||||
Awaitility.await().atMost(Durations.FIVE_SECONDS)
|
||||
.until(() -> existed == MapWithAIDataUtils.checkIfMapWithAIPaintStyleExists());
|
||||
.until(() -> existed == MapPaintUtils.checkIfMapWithAIPaintStyleExists());
|
||||
assertEquals(Config.getPref().getBoolean(MapWithAIPlugin.PAINTSTYLE_PREEXISTS) ? 1 : 0,
|
||||
MapPaintStyles.getStyles().getStyleSources().parallelStream()
|
||||
.filter(source -> source.url != null && source.name.contains("MapWithAI")).count(),
|
||||
|
|
|
@ -34,6 +34,7 @@ import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
|
|||
import org.openstreetmap.josm.gui.mappaint.StyleSource;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.testutils.MapWithAITestRules;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.tools.MapPaintUtils;
|
||||
import org.openstreetmap.josm.testutils.JOSMTestRules;
|
||||
import org.openstreetmap.josm.tools.Logging;
|
||||
|
||||
|
@ -139,12 +140,11 @@ public class MapWithAIDataUtilsTest {
|
|||
|
||||
@Test
|
||||
public void testAddPaintStyle() {
|
||||
MapWithAIDataUtils.removeMapWithAIPaintStyles();
|
||||
Awaitility.await().atMost(Durations.TEN_SECONDS)
|
||||
.until(() -> !MapWithAIDataUtils.checkIfMapWithAIPaintStyleExists());
|
||||
MapPaintUtils.removeMapWithAIPaintStyles();
|
||||
Awaitility.await().atMost(Durations.TEN_SECONDS).until(() -> !MapPaintUtils.checkIfMapWithAIPaintStyleExists());
|
||||
List<StyleSource> paintStyles = MapPaintStyles.getStyles().getStyleSources();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
MapWithAIDataUtils.addMapWithAIPaintStyles();
|
||||
MapPaintUtils.addMapWithAIPaintStyles();
|
||||
paintStyles = MapPaintStyles.getStyles().getStyleSources();
|
||||
assertEquals(1, paintStyles.stream().filter(s -> s.title.contains("MapWithAI")).count(),
|
||||
"The paintstyle should have been added, but only one of it");
|
||||
|
|
|
@ -45,6 +45,7 @@ import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAILayer.Continuou
|
|||
import org.openstreetmap.josm.plugins.mapwithai.commands.MapWithAIAddCommand;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.testutils.MapWithAITestRules;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.tools.MapPaintUtils;
|
||||
import org.openstreetmap.josm.spi.preferences.Config;
|
||||
import org.openstreetmap.josm.testutils.JOSMTestRules;
|
||||
import org.openstreetmap.josm.tools.Territories;
|
||||
|
@ -202,12 +203,12 @@ public class MapWithAILayerTest {
|
|||
|
||||
@Test
|
||||
public void testLayerSwitch() {
|
||||
MapWithAIDataUtils.addMapWithAIPaintStyles();
|
||||
MapPaintUtils.addMapWithAIPaintStyles();
|
||||
Layer osm = new OsmDataLayer(new DataSet(), "TEST", null);
|
||||
MainApplication.getLayerManager().addLayer(osm);
|
||||
MainApplication.getLayerManager().addLayer(layer);
|
||||
MainApplication.getLayerManager().setActiveLayer(layer);
|
||||
StyleSource pref = MapWithAIDataUtils.getMapWithAIPaintStyle();
|
||||
StyleSource pref = MapPaintUtils.getMapWithAIPaintStyle();
|
||||
layer.activeOrEditLayerChanged(null);
|
||||
assertTrue(pref.active);
|
||||
MainApplication.getLayerManager().setActiveLayer(osm);
|
||||
|
|
|
@ -22,10 +22,10 @@ import org.openstreetmap.josm.io.OsmApi;
|
|||
import org.openstreetmap.josm.io.OsmApiInitializationException;
|
||||
import org.openstreetmap.josm.io.OsmTransferCanceledException;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.backend.DataAvailability;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIDataUtils;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIConflationCategory;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAILayerInfo;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.tools.MapPaintUtils;
|
||||
import org.openstreetmap.josm.spi.preferences.Config;
|
||||
import org.openstreetmap.josm.testutils.JOSMTestRules;
|
||||
import org.openstreetmap.josm.tools.Logging;
|
||||
|
@ -111,7 +111,7 @@ public class MapWithAITestRules extends JOSMTestRules {
|
|||
wireMock = new WireMockServer(options().usingFilesUnderDirectory("test/resources/wiremock")
|
||||
.extensions(new WireMockUrlTransformer()).dynamicPort());
|
||||
wireMock.start();
|
||||
MapWithAIDataUtils.setPaintStyleUrl(replaceUrl(wireMock, MapWithAIDataUtils.getPaintStyleUrl()));
|
||||
MapPaintUtils.setPaintStyleUrl(replaceUrl(wireMock, MapPaintUtils.getPaintStyleUrl()));
|
||||
// Avoid cases where tests could write the wiremock url to some fields.
|
||||
if (currentReleaseUrl == null) {
|
||||
currentReleaseUrl = DataAvailability.getReleaseUrl();
|
||||
|
|
Ładowanie…
Reference in New Issue