NRD
NRD

Reputation: 151

How to plot Latitude and Longitude in Bokeh

I have created a figure in bokeh with X and Y axis labels showing latitude and longitude coordinates but using a WMTS tile map which of course uses Mercator projection. I presume there is some kind of conversion taking place by Bokeh since in their documentation they say:

"Notice also that passing x_axis_type="mercator" and y_axis_type="mercator" to figure generate axes with latitude and longitude labels, instead of raw Web Mercator coordinates" (https://docs.bokeh.org/en/latest/docs/user_guide/geo.html)

When I plot markers however, which are set by LAT and LON they ignore the X and Y axis scale and instead plot according to Mercator scale

title = "test"

mercator_extent = dict(start=-20000000, end=20000000,bounds=None)

# create a tile source
tile_options = {}
tile_options['url'] = 'http://tile.stamen.com/watercolor/{z}/{x}/{y}.jpg'
tile_options['attribution'] = """
    Map tiles by <a href="http://stamen.com">Stamen Design</a>, under
    <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a>.
    Data by <a href="http://openstreetmap.org">OpenStreetMap</a>,
    under <a href="http://www.openstreetmap.org/copyright">ODbL</a>.
    """

mq_tile_source = WMTSTileSource(**tile_options)

source = ColumnDataSource(data=OrderedDict(lat=([50]), lon=([50])))

x_range= Range1d(**mercator_extent)
y_range= Range1d(**mercator_extent)

plot = figure(title='printed line on map',
              tools= "pan,wheel_zoom",
              x_range=x_range,
              x_axis_type="mercator",
              y_axis_type="mercator",
              y_range=y_range,
              sizing_mode = 'stretch_both')#create a figure

plot.add_tile(mq_tile_source)#add the map

plot.circle(source=source,x='lon',y='lat',line_color ='red')

output_file("file.html")
save(plot)

I am under the impression that setting X and Y axis = "mercator" my X and Y axis labels are shown as latitude and longitude and therefore my markers should plot against the axis accordingly. In other words, according the the example above i should have marker that observes that X and Y axis but I don't.

Image below shows the output; the hover tool shows my data is 50 LAT and LON and so I would expect the marker to be where i have manually drawn a purple blob but instead it plots almost dead center

enter image description here

any idea?

Upvotes: 4

Views: 4199

Answers (1)

NRD
NRD

Reputation: 151

I will answer my own question

The labels are LAT and LNG but the underlying plot is via Mercator coordinates

One can convert from LAT/LNG to Mercator by passing a DF with columns Lat and Lon into the following function::

def wgs84_to_web_mercator(df, lon="LON", lat="LAT"):

      k = 6378137
      df["x"] = df[lon] * (k * np.pi/180.0)
      df["y"] = np.log(np.tan((90 + df[lat]) * np.pi/360.0)) * k

      return df

Upvotes: 7

Related Questions