Folium custom tile map shows only grey box - python

I use folium to program a leaflet map which I want to customize with an open map from https://sg.geodatenzentrum.de/wms_webatlasde.light_grau. I don't receive an error, but the tile is not displayed: I only get a grey box. I read folium custom map tiles but I still haven't understood how sometimes custom tiles have to be given in a form like:
http://tile.stamen.com/toner/{z}/{x}/{y}.png
http://tile.stamen.com/terrain/{z}/{x}/{y}.jpg
http://tile.stamen.com/watercolor/{z}/{x}/{y}.jpg
I use the map as in the java script source code of the following map: https://www.zdm-emob.de/Kartendarstellung/konzepte.asp. Therefore, I don't get where I went wrong. I assume it could also have something to do with the projection of leaflet and the other map, I tried adjusting but without success.
Here is the code I am using:
import folium
ger = 'https://sg.geodatenzentrum.de/wms_webatlasde.light_grau'
m = folium.Map(location=['51.133333','10.416667'],
tiles = ger,
attr = 'some_attribute',
zoom_start=6)
m

Instead of importing the map as tile in folium.Map one can create a map with a "None" tile. Then, folium.raster_layers.WmsTileLayer can be added to my_map
my_map = folium.Map(tiles=None,min_zoom=6, max_zoom=12, zoom_start=6)
attribute = ('&copy GeoBasis-DE /BKG')
folium.raster_layers.WmsTileLayer(url = 'https://sgx.geodatenzentrum.de/wms_webatlasde.light_grau?',
layers='webatlasde.light_grau',
fmt='image/png',
attr=attribute,
transparent=False).add_to(my_map)

Related

Holes in PolyData while using vtkLinearExtrusionFilter in Python

I wanna create a 3D object from a surface.I used vtkAdaptiveSubdivisionFilter to remesh a polydata. When I GetOutput from vtkAdaptiveSubdivisionFilter and use vtkLinearExtusionFilter to extrude
I see some holes on extrude output and some bad edges. see the pic below.
here is my code:
normals = vtk.vtkPolyDataNormals()
normals.SetInputData(polydata)
normals.SetComputePointNormals(True)
normals.SetComputeCellNormals(False)
normals.SplittingOff()
normals.NonManifoldTraversalOff()
normals.FlipNormalsOff()
normals.ConsistencyOff()
normals.AutoOrientNormalsOff()
normals.Update()
extrude = vtk.vtkLinearExtrusionFilter()
# extrude = vtk.vtkPLinearExtrusionFilter()
# extrude.PieceInvariantOff ()
extrude.SetInputData(normals.GetOutput())
extrude.SetScaleFactor(3)
extrude.SetExtrusionTypeToNormalExtrusion()
extrude.Update()
as you see, I used vtkPlinearExtusionFilter too but there is the same problem.
Should I use another library or package or vtk Classes?
how can I fix it?
best regards
You can solve this problem by Remeshing the object. you can use vtkAdaptiveSubdivisionFilter class to do that.

Programming a picture maker template in Python possible?

I'm looking for a library that enables to "create pictures" (or even videos) with the following functions:
Accepting picture inputs
Resizing said inputs to fit given template / scheme
Positioning the pictures in pre-set up layers or coordinates
A rather schematic approach to look at this:
whereas the red spots are supposed to represent e.g. text, picture (or if possible video) elements.
The end goal would be to give the .py script multiple input pictures and the .py creating a finished version like mentioned above.
Solutions I tried were looking into Python PIL, but I wasn't able to find what I was looking for.
Yes, it is possible to do this with Python.
The library you are looking for is OpenCV([https://opencv.org][1]/).
Some basic OpenCV python tutorials (https://docs.opencv.org/master/d9/df8/tutorial_root.html).
1) You can use imread() function to read images from files.
2) You can use resize() function to resize the images.
3) You can create a empty master numpy array matching the size and depth(color depth) of the black rectangle in the figure you have shown, resize your image and copy the contents into the empty array starting from the position you want.
Below is a sample code which does something close to what you might need, you can modify this to suit your actual needs. (Since your requirements are not clear I have written the code like this so that it can at least guide you.)
import numpy as np
import cv2
import matplotlib.pyplot as plt
# You can store most of these values in another file and load them.
# You can modify this to set the dimensions of the background image.
BG_IMAGE_WIDTH = 100
BG_IMAGE_HEIGHT = 100
BG_IMAGE_COLOR_DEPTH = 3
# This will act as the black bounding box you have shown in your figure.
# You can also load another image instead of creating empty background image.
empty_background_image = np.zeros(
(BG_IMAGE_HEIGHT, BG_IMAGE_WIDTH, BG_IMAGE_COLOR_DEPTH),
dtype=np.int
)
# Loading an image.
# This will be copied later into one of those red boxes you have shown.
IMAGE_PATH = "./image1.jpg"
foreground_image = cv2.imread(IMAGE_PATH)
# Setting the resize target and top left position with respect to bg image.
X_POS = 4
Y_POS = 10
RESIZE_TARGET_WIDTH = 30
RESIZE_TARGET_HEIGHT = 30
# Resizing
foreground_image= cv2.resize(
src=foreground_image,
dsize=(RESIZE_TARGET_WIDTH, RESIZE_TARGET_HEIGHT),
)
# Copying this into background image
empty_background_image[
Y_POS: Y_POS + RESIZE_TARGET_HEIGHT,
X_POS: X_POS + RESIZE_TARGET_WIDTH
] = foreground_image
plt.imshow(empty_background_image)
plt.show()

Is there a limit to plotting markers with folium?

I'm plotting the missions ran by the USAF on North Korea during the Korean War.
The following is the map with 2800 plots.
I have a total of about 7500 plots, but whenever I try to plot above 2800 a blank map renders. I'm rendering on a pc laptop. Would it render if I use a desktop? Or is this a limit with folium?
I'm not speculating that it's an issue with the data. I'll share the coordinates data in case someone would like to explore it: link to public excel sheet.
As #Bob Haffner suggested you can use FastMarkerCluster from Folium library.
Here is my code, in my file there is ~500K points.
import pandas as pd
import json
from folium.plugins import FastMarkerCluster
rome_lat, rome_lng = 41.9028, 12.4964
with open("file_name.json", 'r') as f:
# create a new DataFrame
samples = pd.DataFrame(json.loads(f.read()))
# init the folium map object
my_map = folium.Map(location=[rome_lat, rome_lng], zoom_start=5)
# add all the point from the file to the map object using FastMarkerCluster
my_map.add_child(FastMarkerCluster(samples[['latitude', 'longitude']].values.tolist()))
# save the map
my_map.save("save_file.html")
This code takes ~10ms to render the map.
For more details example please follow this link:
FastMarkerCluster example
Hope this is helpful.
Another option is that we can add a specific number of markers(let's say 3000 markers) on a layer, using folium.map.FeatureGroup() function that will add 3000 markers on a single layer, and we can add that layer to the map using add_child() function, which reduces the number of layers on the map. I got the result for 20,000 Markers and 3000 line string. And is able to load within 40-45 seconds.

How to create an animation with the "svgwrite" library in python

I'm working on the creation of a bioinformatic tool using the library SVGWRITE.
I need some help with the using of the animate module, (like animateColor, animateMotion...), because even if I read the documentation, also read all the example in the svgwrite package, I couldn't find a way to have some informations about how to properly use it.
The SVG draw I'm creating is composed only with rectangles, what I call exon, this is how I'm creating them :
def drawExon(dwg,lineNumber,start,end,rvb):
"""
A function which draw a new exon on a opened dwg draw
it draw that exon from 'start', to 'end' (x position)
at the line 'lineNumber' (y position) with the color'rvb'
"""
dwg.add(dwg.rect((start,lineNumber*line_height),(end - start,
exon_height),fill=svgwrite.rgb(rvb[0],rvb[1],rvb[2])))
I would like to add a animation of theses rectangles, when I fly over the rectangle, I would like that an ID appear on that rectangle (in a text box above it) and also that the rectangle could change color.
The problem is that I have no idea of how and where to apply the animate module, neither what kind of arguments I have to give...
Well, I tried during the last two hours to do such a thing, I read many things on the following link:
http://svgwrite.readthedocs.org/en/latest/classes/animate.html#animate
But it didn't help me to find a answer.
Heres an example for rotating rectangle and color changing text
import svgwrite
path = [(100,100),(100,200),(200,200),(200,100)]
image = svgwrite.Drawing('test.svg',size=(300,300))
rectangle = image.add(image.polygon(path,id ='polygon',stroke="black",fill="white"))
rectangle.add(image.animateTransform("rotate","transform",id="polygon", from_="0 150 150", to="360 150 150",dur="4s",begin="0s",repeatCount="indefinite"))
text = image.add(image.text('rectangle1',insert=(150,30),id="text"))
text.add(image.animateColor("fill", attributeType="XML",from_="green", to="red",id="text", dur="4s",repeatCount="indefinite"))
image.save()
notice that animateColor dont work in every browsers

Python + Mapnik: Example on how to render a map with a gps track on it

I'm trying to render a map using mapnik and python from a GPS track recorded. I get the gps data from a Database, so it is just an array (lat, long).
Does anyone knows an example for doing that? I know I need to create a shape file first, but I'm new to mapnik and I don't really understand it so far. maybe with a good example I will get it :-)
Thanks
The simplest method would actually be to use a KML file. Install the simplekml module and then run through your array to create the KML file.
import simplekml
kml = simplekml.Kml()
for i, coord in enumerate(coords):
# assuming coord is a lat, lon tuple
kml.newpoint(name="Point %s" % i, coords=[coord])
kml.save("GPS_tracking_data.kml")
Now you can load that into mapnik as a datasource and plot it;
import mapnik
# Setup the map
map_canvas = mapnik.Map(width_in_px, height_in_px)
map_canvas.background = mapnik.Color('rgb(0,0,0,0)') # transparent
# Create a symbolizer to draw the points
style = mapnik.Style()
rule = mapnik.Rule()
point_symbolizer = mapnik.MarkersSymbolizer()
point_symbolizer.allow_overlap = True
point_symbolizer.opacity = 0.5 # semi-transparent
rule.symbols.append(point_symbolizer)
style.rules.append(rule)
map_canvas.append_style('GPS_tracking_points', style)
# Create a layer to hold the ponts
layer = mapnik.Layer('GPS_tracking_points')
layer.datasource = mapnik.Ogr(file="GPS_tracking_data.kml", layer_by_index=0)
layer.styles.append('GPS_tracking_points')
map_canvas.layers.append(layer)
# Save the map
map_canvas.zoom_all()
mapnik.render_to_file(map_canvas, 'GPS_tracking_points.png', 'png')
That should just about do it. The docs for python+mapnik are a little weak but you should be able to build on this if you reference;
The mapnik wiki
The python mapnik package docs

Categories