use of net.osmand.render.RenderingRulesStorage in project OsmAnd-tools by osmandapp.
the class NativeSwingRendering method loadRuleStorage.
public void loadRuleStorage(String path, String renderingProperties) throws IOException, XmlPullParserException, SAXException {
final LinkedHashMap<String, String> renderingConstants = new LinkedHashMap<String, String>();
final RenderingRulesStorageResolver resolver = new RenderingRulesStorageResolver() {
@Override
public RenderingRulesStorage resolve(String name, RenderingRulesStorageResolver ref) throws XmlPullParserException, IOException {
RenderingRulesStorage depends = new RenderingRulesStorage(name, renderingConstants);
depends.parseRulesFromXmlInputStream(RenderingRulesStorage.class.getResourceAsStream(name + ".render.xml"), ref);
return depends;
}
};
if (path == null || path.equals("default.render.xml")) {
loadRenderingAttributes(RenderingRulesStorage.class.getResourceAsStream("default.render.xml"), renderingConstants);
storage = new RenderingRulesStorage("default", renderingConstants);
storage.parseRulesFromXmlInputStream(RenderingRulesStorage.class.getResourceAsStream("default.render.xml"), resolver);
} else {
InputStream is = null;
InputStream is2 = null;
if (new File(path).exists()) {
is = new FileInputStream(new File(path));
is2 = new FileInputStream(new File(path));
} else {
is = RenderingRulesStorage.class.getResourceAsStream(path);
is2 = RenderingRulesStorage.class.getResourceAsStream(path);
}
if (is == null) {
throw new IllegalArgumentException("Can't find rendering style '" + path + "'");
}
loadRenderingAttributes(is, renderingConstants);
storage = new RenderingRulesStorage("default", renderingConstants);
storage.parseRulesFromXmlInputStream(is2, resolver);
is.close();
is2.close();
}
renderingProps = new HashMap<String, String>();
String[] props = renderingProperties.split(",");
for (String s : props) {
int i = s.indexOf('=');
if (i > 0) {
String key = s.substring(0, i).trim();
String value = s.substring(i + 1).trim();
if (value.contains(";")) {
value = value.substring(0, value.indexOf(';'));
}
renderingProps.put(key, value);
}
}
initRenderingRulesStorage(storage);
}
use of net.osmand.render.RenderingRulesStorage in project Osmand by osmandapp.
the class ConfigureMapMenu method getRenderDescr.
protected String getRenderDescr(final MapActivity activity) {
RendererRegistry rr = activity.getMyApplication().getRendererRegistry();
RenderingRulesStorage storage = rr.getCurrentSelectedRenderer();
if (storage == null) {
return "";
}
String translation = RendererRegistry.getTranslatedRendererName(activity, storage.getName());
return translation == null ? storage.getName() : translation;
}
use of net.osmand.render.RenderingRulesStorage in project Osmand by osmandapp.
the class ConfigureMapMenu method createListAdapter.
public ContextMenuAdapter createListAdapter(final MapActivity ma) {
ContextMenuAdapter adapter = new ContextMenuAdapter();
adapter.setDefaultLayoutId(R.layout.list_item_icon_and_menu);
adapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.app_modes_choose, ma).setLayout(R.layout.mode_toggles).createItem());
adapter.setChangeAppModeListener(new OnClickListener() {
@Override
public void onClick() {
ma.getDashboard().updateListAdapter(createListAdapter(ma));
}
});
RenderingRulesStorage renderer = ma.getMyApplication().getRendererRegistry().getCurrentSelectedRenderer();
List<RenderingRuleProperty> customRules = new ArrayList<>();
boolean hasDepthContours = ma.getMyApplication().getResourceManager().hasDepthContours();
if (renderer != null) {
for (RenderingRuleProperty p : renderer.PROPS.getCustomRules()) {
if (!RenderingRuleStorageProperties.UI_CATEGORY_HIDDEN.equals(p.getCategory()) && (hasDepthContours || !p.getAttrName().equals("depthContours"))) {
customRules.add(p);
}
}
}
createLayersItems(customRules, adapter, ma);
createRenderingAttributeItems(customRules, adapter, ma);
return adapter;
}
use of net.osmand.render.RenderingRulesStorage in project Osmand by osmandapp.
the class MapRenderRepositories method loadMap.
public synchronized void loadMap(RotatedTileBox tileRect, MapTileDownloader mapTileDownloader) {
boolean prevInterrupted = interrupted;
interrupted = false;
// added to avoid zoomAnimation != 0 which produces wrong map position on the screen
tileRect.setZoomAndAnimation(tileRect.getZoom(), 0);
// prevent editing
requestedBox = new RotatedTileBox(tileRect);
log.info("RENDER MAP: new request " + tileRect);
if (currentRenderingContext != null) {
currentRenderingContext = null;
}
try {
// find selected rendering type
OsmandApplication app = ((OsmandApplication) context.getApplicationContext());
boolean nightMode = app.getDaynightHelper().isNightMode();
// boolean moreDetail = prefs.SHOW_MORE_MAP_DETAIL.get();
RenderingRulesStorage storage = app.getRendererRegistry().getCurrentSelectedRenderer();
RenderingRuleSearchRequest renderingReq = new RenderingRuleSearchRequest(storage);
renderingReq.setBooleanFilter(renderingReq.ALL.R_NIGHT_MODE, nightMode);
for (RenderingRuleProperty customProp : storage.PROPS.getCustomRules()) {
if (customProp.isBoolean()) {
if (customProp.getAttrName().equals(RenderingRuleStorageProperties.A_ENGINE_V1)) {
renderingReq.setBooleanFilter(customProp, true);
} else if (RenderingRuleStorageProperties.UI_CATEGORY_HIDDEN.equals(customProp.getCategory())) {
renderingReq.setBooleanFilter(customProp, false);
} else {
CommonPreference<Boolean> pref = prefs.getCustomRenderBooleanProperty(customProp.getAttrName());
renderingReq.setBooleanFilter(customProp, pref.get());
}
} else if (RenderingRuleStorageProperties.UI_CATEGORY_HIDDEN.equals(customProp.getCategory())) {
if (customProp.isString()) {
renderingReq.setStringFilter(customProp, "");
} else {
renderingReq.setIntFilter(customProp, 0);
}
} else {
CommonPreference<String> settings = prefs.getCustomRenderProperty(customProp.getAttrName());
String res = settings.get();
if (!Algorithms.isEmpty(res)) {
if (customProp.isString()) {
renderingReq.setStringFilter(customProp, res);
} else {
try {
renderingReq.setIntFilter(customProp, Integer.parseInt(res));
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
} else {
if (customProp.isString()) {
renderingReq.setStringFilter(customProp, "");
}
}
}
}
renderingReq.saveState();
NativeOsmandLibrary nativeLib = !prefs.SAFE_MODE.get() ? NativeOsmandLibrary.getLibrary(storage, context) : null;
// calculate data box
QuadRect dataBox = requestedBox.getLatLonBounds();
int dataBoxZoom = requestedBox.getZoom();
long now = System.currentTimeMillis();
if (cObjectsBox.left > dataBox.left || cObjectsBox.top < dataBox.top || cObjectsBox.right < dataBox.right || cObjectsBox.bottom > dataBox.bottom || (nativeLib != null) == (cNativeObjects == null) || dataBoxZoom != cObjectsZoom || prevInterrupted) {
// increase data box in order for rotate
if ((dataBox.right - dataBox.left) > (dataBox.top - dataBox.bottom)) {
double wi = (dataBox.right - dataBox.left) * .05;
dataBox.left -= wi;
dataBox.right += wi;
} else {
double hi = (dataBox.top - dataBox.bottom) * .05;
dataBox.top += hi;
dataBox.bottom -= hi;
}
validateLatLonBox(dataBox);
renderedState = 0;
boolean loaded;
if (nativeLib != null) {
cObjects = new LinkedList<BinaryMapDataObject>();
loaded = loadVectorDataNative(dataBox, requestedBox.getZoom(), renderingReq, nativeLib);
} else {
cNativeObjects = null;
loaded = loadVectorData(dataBox, requestedBox.getZoom(), renderingReq);
}
if (!loaded || checkWhetherInterrupted()) {
return;
}
}
final long searchTime = System.currentTimeMillis() - now;
currentRenderingContext = new OsmandRenderer.RenderingContext(context);
renderingReq.clearState();
renderingReq.setIntFilter(renderingReq.ALL.R_MINZOOM, requestedBox.getZoom());
if (renderingReq.searchRenderingAttribute(RenderingRuleStorageProperties.A_DEFAULT_COLOR)) {
currentRenderingContext.defaultColor = renderingReq.getIntPropertyValue(renderingReq.ALL.R_ATTR_COLOR_VALUE);
}
renderingReq.clearState();
renderingReq.setIntFilter(renderingReq.ALL.R_MINZOOM, requestedBox.getZoom());
if (renderingReq.searchRenderingAttribute(RenderingRuleStorageProperties.A_SHADOW_RENDERING)) {
currentRenderingContext.shadowRenderingMode = renderingReq.getIntPropertyValue(renderingReq.ALL.R_ATTR_INT_VALUE);
currentRenderingContext.shadowRenderingColor = renderingReq.getIntPropertyValue(renderingReq.ALL.R_SHADOW_COLOR);
}
if (renderingReq.searchRenderingAttribute("polygonMinSizeToDisplay")) {
currentRenderingContext.polygonMinSizeToDisplay = renderingReq.getIntPropertyValue(renderingReq.ALL.R_ATTR_INT_VALUE);
}
final QuadPointDouble lt = requestedBox.getLeftTopTile(requestedBox.getZoom());
double cfd = MapUtils.getPowZoom(requestedBox.getZoomFloatPart()) * requestedBox.getMapDensity();
lt.x *= cfd;
lt.y *= cfd;
// LatLon ltn = requestedBox.getLeftTopLatLon();
final double tileDivisor = MapUtils.getPowZoom(31 - requestedBox.getZoom()) / cfd;
currentRenderingContext.leftX = lt.x;
currentRenderingContext.topY = lt.y;
currentRenderingContext.zoom = requestedBox.getZoom();
currentRenderingContext.rotate = requestedBox.getRotate();
currentRenderingContext.width = requestedBox.getPixWidth();
currentRenderingContext.height = requestedBox.getPixHeight();
currentRenderingContext.nightMode = nightMode;
if (requestedBox.getZoom() <= zoomOnlyForBasemaps && "".equals(prefs.MAP_PREFERRED_LOCALE.get())) {
currentRenderingContext.preferredLocale = app.getLanguage();
currentRenderingContext.transliterate = !"ru".equals(app.getLanguage()) && !"uk".equals(app.getLanguage()) && !"be".equals(app.getLanguage()) && !"bg".equals(app.getLanguage()) && !"mk".equals(app.getLanguage()) && !"sr".equals(app.getLanguage());
} else {
currentRenderingContext.preferredLocale = prefs.MAP_PREFERRED_LOCALE.get();
currentRenderingContext.transliterate = prefs.MAP_TRANSLITERATE_NAMES.get();
if (currentRenderingContext.preferredLocale.equals("en")) {
currentRenderingContext.transliterate = true;
}
}
final float mapDensity = (float) requestedBox.getMapDensity();
currentRenderingContext.setDensityValue(mapDensity);
// Text/icon scales according to mapDensity (so text is size of road)
// currentRenderingContext.textScale = (requestedBox.getDensity()*app.getSettings().TEXT_SCALE.get());
// Text/icon stays same for all sizes
currentRenderingContext.textScale = (requestedBox.getDensity() * app.getSettings().TEXT_SCALE.get()) / mapDensity;
currentRenderingContext.screenDensityRatio = 1 / Math.max(1, requestedBox.getDensity());
// init rendering context
currentRenderingContext.tileDivisor = tileDivisor;
if (checkWhetherInterrupted()) {
return;
}
now = System.currentTimeMillis();
Bitmap bmp;
boolean transparent = false;
RenderingRuleProperty rr = storage.PROPS.get("noPolygons");
if (rr != null) {
transparent = renderingReq.getIntPropertyValue(rr) > 0;
}
// 1. generate image step by step
Bitmap reuse = prevBmp;
this.prevBmp = this.bmp;
this.prevBmpLocation = this.bmpLocation;
// necessary for transparent, otherwise 2 times smaller
Config cfg = transparent ? Config.ARGB_8888 : Config.RGB_565;
if (reuse != null && reuse.getWidth() == currentRenderingContext.width && reuse.getHeight() == currentRenderingContext.height && cfg == reuse.getConfig()) {
bmp = reuse;
bmp.eraseColor(currentRenderingContext.defaultColor);
} else {
if (reuse != null) {
log.warn(String.format("Create new image ? %d != %d (w) %d != %d (h) ", currentRenderingContext.width, reuse.getWidth(), currentRenderingContext.height, reuse.getHeight()));
}
bmp = Bitmap.createBitmap(currentRenderingContext.width, currentRenderingContext.height, cfg);
if (reuse != null) {
reuse.recycle();
}
}
this.bmp = bmp;
this.bmpLocation = tileRect;
if (nativeLib != null) {
renderer.generateNewBitmapNative(currentRenderingContext, nativeLib, cNativeObjects, bmp, renderingReq, mapTileDownloader);
} else {
renderer.generateNewBitmap(currentRenderingContext, cObjects, bmp, renderingReq, mapTileDownloader);
}
// Force to use rendering request in order to prevent Garbage Collector when it is used in C++
if (renderingReq != null) {
log.info("Debug :" + renderingReq != null);
}
String renderingDebugInfo = currentRenderingContext.renderingDebugInfo;
currentRenderingContext.ended = true;
if (checkWhetherInterrupted()) {
// (be smart a bit do not revert if road already drawn)
if (currentRenderingContext.lastRenderedKey < OsmandRenderer.DEFAULT_LINE_MAX) {
reuse = this.bmp;
this.bmp = this.prevBmp;
this.bmpLocation = this.prevBmpLocation;
this.prevBmp = reuse;
this.prevBmpLocation = null;
}
currentRenderingContext = null;
return;
} else {
visibleRenderingContext = currentRenderingContext;
this.checkedRenderedState = renderedState;
this.checkedBox = this.bmpLocation;
}
currentRenderingContext = null;
// 2. replace whole image
// keep cache
// this.prevBmp = null;
this.prevBmpLocation = null;
if (prefs.DEBUG_RENDERING_INFO.get() && OsmandPlugin.getEnabledPlugin(OsmandDevelopmentPlugin.class) != null) {
// $NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
String timeInfo = "Searching: " + searchTime + " ms";
if (renderingDebugInfo != null) {
timeInfo += "\n" + renderingDebugInfo;
}
final String msg = timeInfo;
log.info(msg);
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
}
});
}
} catch (RuntimeException e) {
// $NON-NLS-1$
log.error("Runtime memory exception", e);
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(context, R.string.rendering_exception, Toast.LENGTH_SHORT).show();
}
});
} catch (OutOfMemoryError e) {
// $NON-NLS-1$
log.error("Out of memory error", e);
cObjects = new ArrayList<BinaryMapDataObject>();
cObjectsBox = new QuadRect();
handler.post(new Runnable() {
@Override
public void run() {
// ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
// ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
// activityManager.getMemoryInfo(memoryInfo);
// int avl = (int) (memoryInfo.availMem / (1 << 20));
int max = (int) (Runtime.getRuntime().maxMemory() / (1 << 20));
int avl = (int) (Runtime.getRuntime().freeMemory() / (1 << 20));
String s = " (" + avl + " MB available of " + max + ") ";
Toast.makeText(context, context.getString(R.string.rendering_out_of_memory) + s, Toast.LENGTH_SHORT).show();
}
});
} finally {
if (currentRenderingContext != null) {
currentRenderingContext.ended = true;
}
}
}
use of net.osmand.render.RenderingRulesStorage in project Osmand by osmandapp.
the class RendererRegistry method loadRenderer.
// private static boolean USE_PRECOMPILED_STYLE = false;
private RenderingRulesStorage loadRenderer(String name, final Map<String, RenderingRulesStorage> loadedRenderers, final Map<String, String> renderingConstants) throws IOException, XmlPullParserException {
// if ((name.equals(DEFAULT_RENDER) || name.equalsIgnoreCase("default")) && USE_PRECOMPILED_STYLE) {
// RenderingRulesStorage rrs = new RenderingRulesStorage("", null);
// new DefaultRenderingRulesStorage().createStyle(rrs);
// log.info("INIT rendering from class");
// return rrs;
// }
InputStream is = getInputStream(name);
if (is == null) {
return null;
}
try {
XmlPullParser parser = PlatformUtil.newXMLPullParser();
parser.setInput(is, "UTF-8");
int tok;
while ((tok = parser.next()) != XmlPullParser.END_DOCUMENT) {
if (tok == XmlPullParser.START_TAG) {
String tagName = parser.getName();
if (tagName.equals("renderingConstant")) {
if (!renderingConstants.containsKey(parser.getAttributeValue("", "name"))) {
renderingConstants.put(parser.getAttributeValue("", "name"), parser.getAttributeValue("", "value"));
}
}
}
}
} finally {
is.close();
}
// parse content
is = getInputStream(name);
final RenderingRulesStorage main = new RenderingRulesStorage(name, renderingConstants);
loadedRenderers.put(name, main);
try {
main.parseRulesFromXmlInputStream(is, new RenderingRulesStorageResolver() {
@Override
public RenderingRulesStorage resolve(String name, RenderingRulesStorageResolver ref) throws XmlPullParserException {
// reload every time to propogate rendering constants
if (loadedRenderers.containsKey(name)) {
// $NON-NLS-1$
log.warn("Circular dependencies found " + name);
}
RenderingRulesStorage dep = null;
try {
dep = loadRenderer(name, loadedRenderers, renderingConstants);
} catch (IOException e) {
// $NON-NLS-1$
log.warn("Dependent renderer not found : " + e.getMessage(), e);
}
if (dep == null) {
// $NON-NLS-1$
log.warn("Dependent renderer not found : " + name);
}
return dep;
}
});
} finally {
is.close();
}
if (rendererLoadedEventListener != null)
rendererLoadedEventListener.onRendererLoaded(name, main, getInputStream(name));
return main;
}
Aggregations