Nikoloz Mamisashvili
Nikoloz Mamisashvili

Reputation: 99

bokeh map plot. lat/long to x and y

I am trying to plot some circle markers in bokeh, python on stamer toner map. I have locations from google map api in such format:

 latitude 41.552164 longitude 44.990961

But the bokeh map plot gets data points in X and Y coordinate format. How can I transform these lat/long coordinates to X and Y?

Thank you in advance.

Upvotes: 3

Views: 5313

Answers (2)

DuCorey
DuCorey

Reputation: 895

your question is a little vague on what exactly you are trying to do and a code example would help give a better answer.

Generally when trying to plot geographical data obtained by online sources, you will have data in a coordinate system (WGS84) with latitude being the y and longitude being the x. Bokeh can plot longitude and latitude simply by specifying the proper names in the ColumnDataSource.

from bokeh.io import show
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure


longitude  = [44.990961]
latitude = [41.552164]

source = ColumnDataSource(data=dict(longitude=longitude, latitude=latitude))

p = figure(plot_width=400, plot_height=400)
p.circle(x='longitude', y='latitude', source=source)
show(p)

If truly your question is related to a coordinate transformation of your data, it will be difficult to answer your question without more details. I recommend you take a look at https://en.wikipedia.org/wiki/Map_projection to understand map projections.

If you need to convert the longitude/latitude coordinates into a different coordinates system. You can use the pyproj package.

import pyproj

project_projection = pyproj.Proj("+init=EPSG:4326")  # wgs84
google_projection = pyproj.Proj("+init=EPSG:3857")  # default google projection

longitude = [44.990961]
latitude = [41.552164]

x, y = pyproj.transform(google_projection, project_projection, longitude, latitude)

print(x, y)

Reference for the google projection Google map api v3 projection?

Upvotes: 5

ewcz
ewcz

Reputation: 13097

in order to easily transform from longitude/latitude (EPSG:4326) into another projection (for example WebMercator EPSG:3857, used by GoogleMaps AFAIK), I would recommend pyproj package in combination with shapely, for example:

from functools import partial
from shapely.geometry import Point
from shapely.ops import transform
import pyproj

pnt = transform(
    partial(
        pyproj.transform,
        pyproj.Proj(init='EPSG:4326'),
        pyproj.Proj(init='EPSG:3857')), Point(44.990961, 41.552164))

print(pnt.x, pnt.y)

Upvotes: 1

Related Questions