I have been trying to make sense out of TimestampedGeoJson plugin from folium.
I want to draw lines that change their colour over time. At the moment, what I do is to completely redraw a line every time I need to change the color, with the massive overhead that entails.
Another issue is how to specify the time in the features. At the moment, I have this example:
import folium
from folium.plugins import TimestampedGeoJson
m = folium.Map(
location=[42.80491692, -4.62577249],
zoom_start=10
)
data = [
{
'coordinates': [
[-4.018876661, 43.11843382],
[-4.856537491, 42.82202193],
],
'dates': [
'2017-06-02T00:00:00',
'2017-06-02T00:10:00'
],
'color': 'red'
},
{
'coordinates': [
[-4.018876661, 43.11843382],
[-4.856537491, 42.82202193],
],
'dates': [
'2017-06-02T00:00:00',
'2017-06-02T00:20:00'
],
'color': 'blue'
},
]
features = [
{
'type': 'Feature',
'geometry': {
'type': 'LineString',
'coordinates': d['coordinates'],
},
'properties': {
'times': d['dates'],
'style': {
'color': d['color'],
'weight': d['weight'] if 'weight' in d else 5
}
}
}
for d in data
]
TimestampedGeoJson({
'type': 'FeatureCollection',
'features': features,
}, period='PT1M', add_last_point=True).add_to(m)
m.save('dynamic4.html')
To me, the first date does not make any sense, but apparently it is required because otherwise the browser will not draw anything.
So:
a) How can I change the style without redrawing the lines?
b) What does the time mean? How can I specify a consistent time sequence?
I will first try to address your questions individually and I'm putting a full solution of what I would do in the end. essentially:
change the TimestampedGeoJson _template variable to change the style_function and it will enable it to make style dinamic
make sure you have one timestep per coordinates in the TimestampedGeoJson data
For avoiding confusion try to not overlap features or have features missing data of in a certain timestep
I believe in your scenario you only have one feature but change colors in different timesteps
Addressing your questions:
a) How can I change the style without redrawing the lines?
I don't think it's possible from folium itself, it would be necessary to pass a style_function to TimestampedGeoJson which is not even a parameter on the class init at the moment. It seems to be hard to do that because you would need to translate a python style_function, to a javascript style_function.
There would be a simple work around. Inside the class definition of TimestampedGeoJson it uses a _template variable as a string template of the javascript code so you could potentially adapt this template however you want but using javascript.
class TimestampedGeoJson(MacroElement):
.... hidding lines to save space
_template = Template("""
{% macro script(this, kwargs) %}
.... hidding lines to save space
style: function (feature) {
return feature.properties.style;
},
onEachFeature: function(feature, layer) {
if (feature.properties.popup) {
layer.bindPopup(feature.properties.popup);
}
}
})
{% endmacro %}
""") # noqa
..... hidding lines to save space
So for changing the line color at every time step you could change this part of the template:
style: function (feature) {
return feature.properties.style;
},
by this : loop through an array of colors
style: function(feature) {
lastIdx=feature.properties.colors.length-1
currIdx=feature.properties.colors.indexOf(feature.properties.color);
if(currIdx==lastIdx){
feature.properties.color = feature.properties.colors[0]
}
else{
feature.properties.color =feature.properties.colors[currIdx+1]
}
return {color: feature.properties.color}
},
change it so that you update the color inside properties.style every timestep.
b) What does the time mean? How can I specify a consistent time sequence?
TimestampedGeoJson is using the library Leaflet.TimeDimension, so TimestampedGeoJson corresponds to L.TimeDimension.Layer.GeoJSON.
from that documentation you get
"coordTimes, times or linestringTimestamps: array of times that can be associated with a geometry (datestrings or ms). In the case of a LineString, it must have as many items as coordinates in the LineString."
so essentially to be consistent just make sure
1. for each feature the times size is the same as coordinates and
2. use a valid datestrings or ms format
3. if your dates are increasing by a constant set your period to that value
putting all together I mainly changed in your previous example:
1) added a _template variable with the new style_function and change the TimestampedGeoJson default template
2) changed the two features coordinates a bit to show that the two features you set were overlapping and some timesteps and at some timesteps just the first feature was defined and later just the second feature was defined so it get's confusing what is happening in each time step.
3) added a list of colors to loop through for each feature
from jinja2 import Template
_template = Template("""
{% macro script(this, kwargs) %}
L.Control.TimeDimensionCustom = L.Control.TimeDimension.extend({
_getDisplayDateFormat: function(date){
var newdate = new moment(date);
console.log(newdate)
return newdate.format("{{this.date_options}}");
}
});
{{this._parent.get_name()}}.timeDimension = L.timeDimension(
{
period: {{ this.period|tojson }},
}
);
var timeDimensionControl = new L.Control.TimeDimensionCustom(
{{ this.options|tojson }}
);
{{this._parent.get_name()}}.addControl(this.timeDimensionControl);
var geoJsonLayer = L.geoJson({{this.data}}, {
pointToLayer: function (feature, latLng) {
if (feature.properties.icon == 'marker') {
if(feature.properties.iconstyle){
return new L.Marker(latLng, {
icon: L.icon(feature.properties.iconstyle)});
}
//else
return new L.Marker(latLng);
}
if (feature.properties.icon == 'circle') {
if (feature.properties.iconstyle) {
return new L.circleMarker(latLng, feature.properties.iconstyle)
};
//else
return new L.circleMarker(latLng);
}
//else
return new L.Marker(latLng);
},
style: function(feature) {
lastIdx=feature.properties.colors.length-1
currIdx=feature.properties.colors.indexOf(feature.properties.color);
if(currIdx==lastIdx){
feature.properties.color = feature.properties.colors[currIdx+1]
}
else{
feature.properties.color =feature.properties.colors[currIdx+1]
}
return {color: feature.properties.color}
},
onEachFeature: function(feature, layer) {
if (feature.properties.popup) {
layer.bindPopup(feature.properties.popup);
}
}
})
var {{this.get_name()}} = L.timeDimension.layer.geoJson(
geoJsonLayer,
{
updateTimeDimension: true,
addlastPoint: {{ this.add_last_point|tojson }},
duration: {{ this.duration }},
}
).addTo({{this._parent.get_name()}});
{% endmacro %}
""")
import folium
from folium.plugins import TimestampedGeoJson
m = folium.Map(
location=[42.80491692, -4.62577249],
zoom_start=9
)
data = [
{
'coordinates': [
[-4.018876661, 43.11843382],
[-4.856537491, 42.82202193],
],
'dates': [
'2017-06-02T00:00:00',
'2017-06-02T00:10:00'
],
'color': 'brown',
'colors':["black","orange","pink"],
},
{
'coordinates': [
[-4.058876661, 43.11843382],
[-4.936537491, 42.82202193],
],
'dates': [
'2017-06-02T00:00:00',
'2017-06-02T00:10:00'
],
'color': 'blue',
'colors':["red","yellow","green"],
},
]
features = [
{
'type': 'Feature',
'geometry': {
'type': 'LineString',
'coordinates': d['coordinates'],
},
'properties': {
'times': d['dates'],
'color': d["color"],
'colors':d["colors"]
}
}
for d in data
]
t=TimestampedGeoJson({
'type': 'FeatureCollection',
'features': features,
}, period='PT10H', add_last_point=True)
t._template=_template
t.add_to(m)
m.save('original.html')
Related
I am not familiar with cytoscape or Java, so I do not know if there is a way to define a node size which depend on node's degree.
Currently I am using the following:
cytoscapeobj.set_style(
[
{
'selector':node,
'style': {
'font-family': 'helvetica',
'font-size': '20px',
'label': 'data(id)'
}
},
{
'selector': 'edge',
'style': {
'font-family': 'helvetica',
'font-size': '20px',
'width' : 'mapData(weight)' # it is actually not working
}
},
{
'selector': 'node[Degree>0]',
'style': {
'width': '100px',
'height': '100px'
}
},
{
'selector': 'node[Degree>1]',
'style': {
'width': '150px',
'height': '150px'
}
},
{
'selector': 'node[Degree>2]',
'style': {
'width': '200px',
'height': '200px'
}
}
]
)
I have thousands of nodes, some of them has degree 1 (most of them), then I have nodes with degree 2, 3, 4, 5 ,... 100, ...
It would be not easy to add a selector for each of them.
Do you know if there is an easier way to plot any node's degree?
You can define any required parameters in the node's data and then use style mappers to apply that data to style properties like width or any other. So your code is actually the right way to do that.
It doesn't work because mapData() and data() works in a different ways. While data() will just apply the data value to that property, as it does to label in your example, the mapData() requires additional parameters to be set.
Check this out:
width: mapData(weight, 0, 100, 1, 3)
In that case, mapData will take the value of data.weight and then check where is that value between 0 and 100 and proportionally set width to the according value between 1 and 3.
In my experience, I find it more convenient to use values, precalculated in the code. So I'd go with creating the data.width param and setting to the desired width and then just map the simple data() mapper to that value.
I have a list of dictionary as below. I need to iterate the list of dictionary and remove the content of the parameters and set as an empty dictionary in sections dictionary.
input = [
{
"category":"Configuration",
"sections":[
{
"section_name":"Global",
"parameters":{
"name":"first",
"age":"second"
}
},
{
"section_name":"Operator",
"parameters":{
"adrress":"first",
"city":"first"
}
}
]
},
{
"category":"Module",
"sections":[
{
"section_name":"Global",
"parameters":{
"name":"first",
"age":"second"
}
}
]
}
]
Expected Output:
[
{
"category":"Configuration",
"sections":[
{
"section_name":"Global",
"parameters":{}
},
{
"section_name":"Operator",
"parameters":{}
}
]
},
{
"category":"Module",
"sections":[
{
"section_name":"Global",
"parameters":{}
}
]
}
]
My current code looks like below:
category_list = []
for categories in input:
sections_list = []
category_name_dict = {"category": categories["category"]}
for sections_dict in categories["sections"]:
section = {}
section["section_name"] = sections_dict['section_name']
section["parameters"] = {}
sections_list.append(section)
category_name_dict["sections"] = sections_list
category_list.append(category_name_dict)
Is there any elegant and more performant way to do compute this logic. Keys such as category, sections, section_name, and parameters are constants.
The easier way is not to rebuild the dictionary without the parameters, just clear it in every section:
for value in values:
for section in value['sections']:
section['parameters'] = {}
Code demo
Elegance is in the eye of the beholder, but rather than creating empty lists and dictionaries then filling them why not do it in one go with a list comprehension:
category_list = [
{
**category,
"sections": [
{
**section,
"parameters": {},
}
for section in category["sections"]
],
}
for category in input
]
This is more efficient and (in my opinion) makes it clearer that the intention is to change a single key.
I have an Eve app publishing a simple read-only (GET) interface. It is interfacing a MongoDB collection called centroids, which has documents like:
[
{
"name":"kachina chasmata",
"location":{
"type":"Point",
"coordinates":[-116.65,-32.6]
},
"body":"ariel"
},
{
"name":"hokusai",
"location":{
"type":"Point",
"coordinates":[16.65,57.84]
},
"body":"mercury"
},
{
"name":"caƱas",
"location":{
"type":"Point",
"coordinates":[89.86,-31.188]
},
"body":"mars"
},
{
"name":"anseris cavus",
"location":{
"type":"Point",
"coordinates":[95.5,-29.708]
},
"body":"mars"
}
]
Currently, (Eve) settings declare a DOMAIN as follows:
crater = {
'hateoas': False,
'item_title': 'crater centroid',
'url': 'centroid/<regex("[\w]+"):body>/<regex("[\w ]+"):name>',
'datasource': {
'projection': {'name': 1, 'body': 1, 'location.coordinates': 1}
}
}
DOMAIN = {
'centroids': crater,
}
Which will successfully answer to requests of the form http://hostname/centroid/<body>/<name>. Inside MongoDB this represents a query like: db.centroids.find({body:<body>, name:<name>}).
What I would like to do also is to offer an endpoint for all the documents of a given body. I.e., a request to http://hostname/centroids/<body> would answer the list of all documents with body==<body>: db.centroids.find({body:<body>}).
How do I do that?
I gave a shot by including a list of rules to the DOMAIN key centroids (the name of the database collection) like below,
crater = {
...
}
body = {
'item_title': 'body craters',
'url': 'centroids/<regex("[\w]+"):body>'
}
DOMAIN = {
'centroids': [crater, body],
}
but didn't work...
AttributeError: 'list' object has no attribute 'setdefault'
Got it!
I was assuming the keys in the DOMAIN structure was directly related to the collection Eve was querying. That is true for the default settings, but it can be adjusted inside the resources datasource.
I figured that out while handling an analogous situation as that of the question: I wanted to have an endpoint hostname/bodies listing all the (unique) values for body in the centroids collection. To that, I needed to set an aggregation to it.
The following settings give me exactly that ;)
centroids = {
'item_title': 'centroid',
'url': 'centroid/<regex("[\w]+"):body>/<regex("[\w ]+"):name>',
'datasource': {
'source': 'centroids',
'projection': {'name': 1, 'body': 1, 'location.coordinates': 1}
}
}
bodies = {
'datasource': {
'source': 'centroids',
'aggregation': {
'pipeline': [
{"$group": {"_id": "$body"}},
]
},
}
}
DOMAIN = {
'centroids': centroids,
'bodies': bodies
}
The endpoint, for example, http://127.0.0.1:5000/centroid/mercury/hokusai give me the name, body, and coordinates of mercury/hokusai.
And the endpoint http://127.0.0.1:5000/bodies, the list of unique values for body in centroids.
Beautiful. Thumbs up to Eve!
I've been trying to write my own Google Sheets wrapper, and it's been a frustrating experience so far. The thing I'm stuck on at the moment is how to get a symmetrical in / out format of sheet data.
Basically, I want to call values().get(), alter the resulting hash, and send that same hash back up to update().
I'm happy to write my own solution to process or coerce the output of values().get() to the structure that batchUpdate() needs, but I need the formatting information of each of the cells to do that.
batchUpdate() expects formatting information like this:
bod = {
'updateCells': {
'start': {
'sheetId': 0,
'rowIndex': 7,
'columnIndex': 0
},
'rows': [
{
'values': [
{
"userEnteredValue": {
'stringValue': 'LOL'
},
"userEnteredFormat": {
'backgroundColor': {
'red': .2,
'blue': .75,
'green': .75
}
}
},
{
"userEnteredValue": {
'stringValue': 'LOL2'
},
"userEnteredFormat": {
'backgroundColor': {
'red': .2,
'blue': 1,
'green': .75
}
}
},
{
"userEnteredValue": {
'stringValue': 'LOL3'
},
"userEnteredFormat": {
'backgroundColor': {
'red': .2,
'blue': 1,
'green': 1
}
}
}
]
}
],
'fields': 'userEnteredValue,userEnteredFormat.backgroundColor'
}
}
How I'm retrieving values currently looks something like this:
import requests
import json
from oauth2client.service_account import ServiceAccountCredentials
from apiclient.discovery import build
#Set up credentials object
auth_key_url = "<JSON CREDENTIALS FILE>"
file_contents = requests.get(auth_key_url).content
key_dict = json.loads(file_contents)
creds = ServiceAccountCredentials.from_json_keyfile_dict(key_dict, ['https://spreadsheets.google.com/feeds'])
#Now build the API object
discoveryUrl = "https://sheets.googleapis.com/$discovery/rest?version=v4"
gsheet = build('sheets', 'v4', discoveryServiceUrl=discoveryUrl, credentials=creds)
result = gsheet.spreadsheets().values().get(spreadsheetId="<A SHEET ID>", range="Sheet1!A1:ZZ").execute()
This produces "results", which is a dictionary with 2 keys, "range" and "values", and "values" is a list of lists of the values of the spreadsheet. These lists do not contain formatting data - just the values in the cells.
Can someone show me, in Python, how I can get cell value, background color, alignment, and other cell formatting information from spreadsheets().values() or from the spreadsheet?
The spreadsheets.values.get endpoint only returns the values. If you want a more complete picture of the spreadsheet (formatting, etc) then you need to use the spreadsheets.get endpoint:
https://developers.google.com/sheets/reference/rest/v4/spreadsheets/get
Make sure to pass either includeGridData=true or pass a value for the fields that includes sheets.data so that the cell data is returned. Pass a value in the range parameter to limit the results to only a specific range.
Is there a way to create geospatial indexes over fields in a MongoKit Document?
Right now I can't find any reference to do so using the indexes descriptor.
I would like to have something like
class Foo(Document):
structure = {
'location': {
'lat': float,
'lon': float
}
}
indexes = [
{
'fields': ['location'],
'type': '2d'
}
]
Can I do this using Pymongo?
It was documented.
To create a geospatial index you need to:
class Foo(Document):
structure = {
'location': {
'lat': float,
'lon': float
}
}
indexes = [
{
'fields': [('location', '2d'), ],
}
]
I had to look at the sources to realize this.
Regards,