基线图m(lats,lons)到svg cx和cy

2024-09-27 21:25:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个SVG图像的世界与底图,我绘制了柏林市作为一个“控制点”(因为我想在那里手动放置一个圆。。。所以我有一个参考)。你知道吗

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt

# Berlin & New York & Sydney
lats = [52.516667] #[52.516667, 40.730610]
lons = [13.388889] # [13.388889, -73.935242]

plt.figure(figsize=(15,15/2))
m = Basemap(projection='robin', lon_0=0, resolution='c')
m.drawcountries(color='#ffffff', linewidth=0.75)
m.fillcontinents(color='#c0c0c0', lake_color='#e6f5ff')                
m.drawmapboundary(fill_color='#e6f5ff', linewidth=1, color='#000000') # Ocean          

x,y = m(lons,lats)

plt.plot(x, y, 'bo', color='r', markersize=5)

plt.savefig("basemap.svg", figsize=(24,12))
plt.show()

在下一步中,我想通过编辑我创建的SVG文件的代码在SVG图像上手动放置一个圆。这可以通过在SVG图像代码末尾的</svg>之前引入以下代码来实现。你知道吗

<circle fill="blue" cx="250" cy="470" r="2"/>

如何使用Python代码确定cx和cy的正确值,以将蓝点放置在Berlin所在的位置?你知道吗

我想我有mapWidth = 1080mapHeigth = 540xMax = 33973600yMax = 17231000。你知道吗

这样我就可以计算cx = mapWidth - x/xMax*mapWidth和类似的cy = mapHeigth - y/yMax*mapHeigth。然而,这并没有把蓝点放在正确的位置,如果我考虑到底部和左边缘分别为72和152磅,也不是。有什么想法吗?你知道吗


Tags: 代码svg图像importplt手动colorbasemap
1条回答
网友
1楼 · 发布于 2024-09-27 21:25:06

看来你真的需要一个解决办法,我给你一个办法。你知道吗

注意:我不鼓励您手动操作.svg文件。但是,如果没有选择,那就让我们做吧!你知道吗

解决方案假定非python进程不会更改表单中的文件。你知道吗

为了克服创建.svg文件(至少对我来说是黑框)的问题,您只需创建另一个绘制预期坐标的图像,将图像保存为临时.svg文件,找到坐标点(在.svg文件上),最后将它们添加到初始地图文件中。你知道吗

我定义了3种方法: -createMap绘制地图并将输出另存为.png文件 -get_svg_coordinates:创建一个临时地图(.svg文件),读取点坐标,删除临时文件,返回点坐标。 -add_circle:在现有的.svg映射文件上绘制圆。你知道吗

下面是代码:(工作示例)

# Import modules
import os
import re
# os.environ['PROJ_LIB'] = r'C:\Users\...\Library\share'
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt


def createMap(lats, lons, color='r', figure_name="basemap.svg", show=False):
    """ Create a map from points coordinates + export to .csv file
    Arguments
        :param lats: latitudes
        :param lons: longitudes
        :param color='r': color points
        :param figure_name="basemap.svg": name output file
        :param show=False: show the map
    """
    # Same code as yours
    plt.figure(figsize=(15, 15/2))
    m = Basemap(projection='robin', lon_0=0, resolution='c')
    m.drawcountries(color='#ffffff', linewidth=0.75)
    m.fillcontinents(color='#c0c0c0', lake_color='#e6f5ff')
    m.drawmapboundary(fill_color='#e6f5ff', linewidth=1, color='#000000')  # Ocean

    x, y = m(lons, lats)

    plt.plot(x, y, 'bo', color=color, markersize=5)
    plt.savefig(figure_name, figsize=(24, 12))

    if show: plt.show()


def get_svg_coordinates(lat, lon, color='#ff0000', figure_temp_name="tmp_figure.svg"):
    """ Create a temporary file using the createMap function
        Find the point coordinates inside (using regex check)
        Remove temporary csv file
    Arguments
        :param lat: new point latitude
        :param lon: new point  longitude
        :param color='#ff0000': point color
        :param figure_temp_name="tmp_figure.svg": temp file name
    """
    createMap(lat, lon, color=color, figure_name=figure_temp_name)

    with open(figure_temp_name, "r") as f:
        # read file
        content = f.read()  
        # Find x - y values (pattern ' x=' is unique is you are using 1 point)
        x = re.findall(r'<use.*x=\"(\d*\.*\d*)\"', content)
        y = re.findall(r'<use.*y=\"(\d*\.*\d*)\"', content)

    # remove file
    os.remove(figure_temp_name)
    return x, y


def add_circle(map_file_name, x, y):
    """ Draw circle at the end of file
    Arguments:
        :param map_file_name: filename (adding circle)
        :param x: x coordinates (results of get_svg_coordinates method)
        :param y: y coordinates (results of get_svg_coordinates method)
    """
    with open(map_file_name, "r+") as f:
        content = f.readlines()
        # get number of lines in file
        for i, l in enumerate(content):
            pass
        # Add content
        content.insert(i, '<circle fill="blue" cx="{0}" cy="{1}" r="2"/>'.format(x[0], y[0]))
        f.seek(0)                   # file pointer locates at the beginning to write the whole file again
        f.writelines(content)       # rewrite file


# Berlin & New York & Sydney
lats = [52.516667]  # [52.516667, 40.730610]
lons = [13.388889]  # [13.388889, -73.935242]

# create your initial svg map
map_file_name = "basemap.svg"
createMap(lats, lons, figure_name=map_file_name)

# Find new position point on svg file
# Define coordinates points
NewYork_lat = 40.730610
NewYork_long = -73.935242
x, y = get_svg_coordinates(NewYork_lat, NewYork_long)
add_circle(map_file_name, x, y)

注:

  • 我不熟悉.svg文件。为了回答这个问题,我按照预期在文件末尾添加了<circle fill="blue" cx="???" cy="???" r="2"/>。然而,识别整个DOM<g id="line2d_1">并复制过去可能更好。

  • 这个片段只适用于一个图像,我让你概括一组点。

相关问题 更多 >

    热门问题