0

I'm drawing a map of Hong Kong and I want to plot all the subway stations on the map. After specifying the bounding box, the center point in the Basemap function and converting the shapefile to the coordinate system 4326 using arcmap, I want to combine the Basemap and the shapefile together. But they don't match very well.

The bounding box I set is:

llcrnrlon=113.80779, llcrnrlat= 22.16694, urcrnrlon=114.416158, urcrnrlat=22.5783

And the center point is:

lat_0=22.394400, lon_0=114.156489

But what I get is the following plot: Map of Hong Kong

The green points represent the subway stations in Hong Kong and the size of the point denote the number of tweets posted near each station. A larger point means more tweets are posted near this station.

From the plot, we could clearly see that the coastlines don't match the shapefile at all. Is it because of the quality of the Basemap function? The following shows all the codes I am using:

import matplotlib.pyplot as plt
import matplotlib.cm

from mpl_toolkits.basemap import Basemap
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
from matplotlib.colors import Normalize

dataframe = pd.read_csv(os.path.join(path, 'map_for_positive_negative.csv'))
dataframe['pos'] = dataframe.apply(lambda row: (row['lat'], row['lon']), axis=1)

# Plot the subway station on the map
def plot_station(pos):
    count = dataframe.loc[dataframe.pos == pos]['Tweet Activity']
    # x is longitude, y is latitude
    x, y = m(pos[1], pos[0])
    size = np.log2(count)
    m.plot(x, y, 'o', markersize=size, color='#0FE500', alpha=0.8)

fig, ax = plt.subplots(figsize=(20, 20))
m = Basemap(resolution='f', # c, l, i, h, f or None
            projection='mill',
            lat_0=22.394400, lon_0=114.156489,
            llcrnrlon=113.80779, llcrnrlat= 22.16694, urcrnrlon=114.416158, urcrnrlat=22.5783)

m.drawmapboundary(fill_color='#46bcec')
m.fillcontinents(color='#f2f2f2',lake_color='#46bcec')
m.drawcoastlines()

# The coordinate system of the shapefile should be 4326. Use arcmap to change it
shape_file_path = r'...\hk_tpu'
m.readshapefile(shapefile=shape_file_path, name='hk_tpu')

dataframe.pos.apply(plot_station)

plt.show(m)

The file which records the location of the subway station is in here:

Subway station location in Hong Kong

The shapefiles I am using are in here:

Shapefile of Hong Kong

Thank you very much!

Bright Chang
  • 191
  • 2
  • 14
  • well written question: was able to replicate the plot you're showing. will look into it and reply if I am able to offer any guidance but it may take some time. please post any solution if you're able to resolve the issue. BTW, what version of Basemap module are you using? – SanV Apr 14 '19 at 04:54
  • Thank you for your prompt reply! I think the basemap version should be 1.2.0. I just downloaded the basemap from here:https://www.lfd.uci.edu/~gohlke/pythonlibs/#basemap And use the following code to install it: ``` pip install basemap‑1.2.0‑cp35‑cp35m‑win_amd64.whl ``` BTW, I will post the solution once I find it! – Bright Chang Apr 14 '19 at 05:11

1 Answers1

0

Oh boy, I give up! I think the reason is that the basemap offered by the Basemap function is outdated and could not catch the city development simultaneously. Hong Kong is a place where the government is always considering expanding the city. Hence, it is quite common if the basemap could not match the latest shapefile very well.

Actually, my goal is to just draw some points on the plot. In this case, I want to plot all the subway stations on the map. And for each point, I want to use the size of the point to represent the number of tweets in the nearby area. Moreover, I also want to use the density of colour to represent people's sentiment level in this area.

Finally, I choose to use Tableau. I did not load the basemap and just load the data from a CSV file which contains the geoinformation of each subway station and tweets posted near each station. A sample graph is shown below:

enter image description here

Happy coding!

Bright Chang
  • 191
  • 2
  • 14