PYTHON帮助

Folium Python(工作原理:开发人员指南)

发布 2025年三月5日
分享:

介绍

将Folium与IronPDF集成到Python中,能够创建交互式地图并将其转换为高质量的PDF,使得从动态可视化平稳过渡到可共享报告变得非常容易。 由Rob Story版权所有的Python Folium库可以执行交互式Python映射,具有丰富的映射优势和功能,简化了处理的过程地理空间数据. 这是一种实用工具,用于制作和编辑地图,包括标记、多边形和其他地理特征,这些在数据分析、地理空间数据或 geoJSON 数据的可视化以及基于位置的 Web 地图服务中非常有用。

IronPDF将在这里派上用场,因为它将通过允许将这些动态地图转换为PDF格式来填补这一空白,从而尽可能保留地图的布局和互动性,弥合这一差异。 这种集成可以帮助在需要详细地图和PDF多功能性的详细报告、演示文稿和文档中进行开发,从而以专业和便携的格式有效地传达地理空间数据。

什么是Folium Python?

Folium是一个Python库,它结合了Python生态系统强大的数据整合能力,用于创建交互式的leaflet地图。适用于在地图上可视化位置和地理空间数据,它可以制作显示数据整合能力在给定兴趣区域变化的分级统计地图。它提供了一系列基础地图,并允许进行广泛的自定义,让用户在Python生态系统内根据需要设计他们的地图。 通过生成互动性的传单地图来增强用户在数据探索中的参与度,这些地图可视化了与可视化和分析相关的空间数据。 它通过与Python库的集成增强了功能,将有助于在广泛的应用中高效地进行复杂的地理数据可视化。

破损图片 添加自 Pixabay,请从您的文件中选择或将图片拖放到此处。

Folium Python的功能

易于使用:Folium抽象了使用Leaflet.js创建地图时的复杂性; 使用Folium,可以通过一个非常简单直观的界面创建地图。

与 Python 数据生态系统的集成: Folium 可以非常容易地与其他类似的 Python 库(如 Pandas 和 Geopandas)一起使用,从而非常容易地将地理空间数据集成到 leaflet 地图中。

自定义: Folium中有许多选项可用于自定义标记、多边形、线条和弹出窗口,以可视化特定数据或区域。

内置支持各种图层,如OpenStreetMap、Mapbox、Stamen等。所有这些都属于不同的地图风格和背景,可以根据许可使用。 交互性:Folium 创建的地图是交互式的,用户可以滚动、缩放和点击地图特征以获取更多信息。 可扩展性:在 Folium 中,可以添加插件和特定业务的 JavaScript 来扩展 Folium 地图的功能。

创建和配置 Folium Python

在 Python 中使用 Folium 设置和配置地图相对简单。 以下是让一个人入门的基本步骤,以及如何设置 Folium 地图不同方面的一些示例。

安装Folium

如果您还没有安装Folium库,请使用pip进行安装:

pip install folium
pip install folium
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'pip install folium
$vbLabelText   $csharpLabel

创建基本地图

您可以通过简单地导入 folium 并创建一个 Web Map 对象来创建基础地图。 这是一个非常简单的例子:

import folium
# Create a map centered at a specific latitude and longitude
m = folium.Map(location=[45.5236, -122.6750], zoom_start=13)
# Save the map to an HTML file
m.save('basic_map.html')
import folium
# Create a map centered at a specific latitude and longitude
m = folium.Map(location=[45.5236, -122.6750], zoom_start=13)
# Save the map to an HTML file
m.save('basic_map.html')
#Create a map centered at a specific latitude and longitude
#Save the map to an HTML file
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'import folium m = folium.Map(location=[45.5236, -122.6750], zoom_start=13) m.save('basic_map.html')
$vbLabelText   $csharpLabel

破损图片 添加自 Pixabay,请从您的文件中选择或将图片拖放到此处。

添加标记和层

您可以向Folium地图添加多种类型的标记和图层。 以下是一些示例:

import folium
# Create a map centered at a specific latitude and longitude
m = folium.Map(location=[45.5236, -122.6750], zoom_start=13)
# Add a marker
folium.Marker([45.5236, -122.6750], popup='Portland, OR').add_to(m)
# Save the map as an HTML document
m.save('map_with_marker.html')
import folium
# Create a map centered at a specific latitude and longitude
m = folium.Map(location=[45.5236, -122.6750], zoom_start=13)
# Add a marker
folium.Marker([45.5236, -122.6750], popup='Portland, OR').add_to(m)
# Save the map as an HTML document
m.save('map_with_marker.html')
#Create a map centered at a specific latitude and longitude
#Add a marker
#Save the map as an HTML document
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'import folium m = folium.Map(location=[45.5236, -122.6750], zoom_start=13) folium.Marker([45.5236, -122.6750], popup='Portland, @OR').add_to(m) m.save('map_with_marker.html')
$vbLabelText   $csharpLabel

下图显示了标记的位置地图。

Folium Python(如何运作:开发人员指南):图3 - 带标记的基础地图

使用插件

Folium 支持各种插件以扩展其功能。 例如,您可以使用MarkerCluster插件来维护大量的标记。

import folium
from folium.plugins import MarkerCluster
# Create a map centered at a specific latitude and longitude
m = folium.Map(location=[45.5236, -122.6750], zoom_start=13)
# Create a marker cluster
marker_cluster = MarkerCluster().add_to(m)
# Add markers to the cluster
for lat, lon in [[45.5236, -122.6750], [45.5286, -122.6800], [45.5215, -122.6764]]:
    folium.Marker(location=[lat, lon]).add_to(marker_cluster)
# Save the map to an HTML file
m.save('map_with_marker_cluster.html')
import folium
from folium.plugins import MarkerCluster
# Create a map centered at a specific latitude and longitude
m = folium.Map(location=[45.5236, -122.6750], zoom_start=13)
# Create a marker cluster
marker_cluster = MarkerCluster().add_to(m)
# Add markers to the cluster
for lat, lon in [[45.5236, -122.6750], [45.5286, -122.6800], [45.5215, -122.6764]]:
    folium.Marker(location=[lat, lon]).add_to(marker_cluster)
# Save the map to an HTML file
m.save('map_with_marker_cluster.html')
#Create a map centered at a specific latitude and longitude
#Create a marker cluster
#Add markers to the cluster
#Save the map to an HTML file
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'import folium from folium.plugins import MarkerCluster m = folium.Map(location=[45.5236, -122.6750], zoom_start=13) marker_cluster = MarkerCluster().add_to(m) for lat, lon in [[45.5236, -122.6750], [45.5286, -122.6800], [45.5215, -122.6764]]: folium.Marker(location=[lat, lon]).add_to(marker_cluster) m.save('map_with_marker_cluster.html')
$vbLabelText   $csharpLabel

Folium Python(工作原理:开发者指南):图4 - 地图标记

入门

使用Folium和IronPDF入门的第一步是使用Folium创建交互式地图,然后使用IronPDF将其制作为PDF出版物。 这种方法将IronPDF的专业文档生成技能与Folium强大的映射功能结合起来。

什么是IronPDF?

"(《世界人权宣言》)IronPDFPython 模块是一个强大且功能齐全的软件包,用于创建、编辑和读取 PDF。 开发人员可以利用 PDF 的强大功能来完成非常复杂的可编程任务,例如从 HTML 文件生成 PDF 文档,以便可以处理预先准备好的 PDF 文档。 这将增强互操作性,使生成精美 PDF 报告的任务更加轻松。 这对于动态应用程序生成和即时处理PDF文件非常有用。

Folium Python(工作原理:开发者指南):图5 - IronPDF

HTML 转换为 PDF

通过IronPDF文档的所有功能,任何HTML数据,无论其创建日期如何,都能快速转换为PDF文档。 此外,您可以在创建从网络材料直接生成的极具创意、极具吸引力的PDF出版物时,使用HTML5、CSS3和JavaScript的大多数新功能。

生成和编辑 PDF

您可以使用某种计算机语言以编程方式生成包含文本、照片、表格等内容的新PDF文档。 预先准备好的文档可以提前打开,然后使用IronPDF进行编辑,以提供更多自定义选项。 可以随时向任何版本的PDF文档添加、修改或删除其内容。

复杂的设计和样式

它在PDF内部具有内在的内容风格,因此可以处理不同字体和颜色的复杂布局,以及其他设计问题。 此外,如果需要在PDF中修改动态内容,则无法使用JavaScript,这就使得在HTML中显示数据更容易。

安装 IronPDF

然而,IronPDF也可以通过使用pip来安装,如下所示。 这是安装命令的外观。

pip install ironpdf
pip install ironpdf
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'pip install ironpdf
$vbLabelText   $csharpLabel

使用IronPDF转换Folium地图

使用 Folium 创建一个简单的地图并将其保存为 PDF。 下面是一个例子:

from ironpdf import *     import warnings
# Create a map with Folium and save it as an HTML file
import folium
warnings.filterwarnings('ignore')
License.LicenseKey = "";
# Create a map centered at a specific latitude and longitude
m = folium.Map(location=[45.5236, -122.6750], zoom_start=13)
# Add a marker
folium.Marker([45.5236, -122.6750], popup='Portland, OR').add_to(m)
# Save the map to an HTML file
m.save('map.html')
# Convert the HTML file to PDF
html_file_path = 'map.html'
pdf_file_path = 'map.pdf'
# Convert HTML to PDF
html_to_pdf = ChromePdfRenderer()
pdf_document = html_to_pdf.RenderHtmlFileAsPdf(html_file_path)
# Save the PDF
pdf_document.SaveAs(pdf_file_path)
from ironpdf import *     import warnings
# Create a map with Folium and save it as an HTML file
import folium
warnings.filterwarnings('ignore')
License.LicenseKey = "";
# Create a map centered at a specific latitude and longitude
m = folium.Map(location=[45.5236, -122.6750], zoom_start=13)
# Add a marker
folium.Marker([45.5236, -122.6750], popup='Portland, OR').add_to(m)
# Save the map to an HTML file
m.save('map.html')
# Convert the HTML file to PDF
html_file_path = 'map.html'
pdf_file_path = 'map.pdf'
# Convert HTML to PDF
html_to_pdf = ChromePdfRenderer()
pdf_document = html_to_pdf.RenderHtmlFileAsPdf(html_file_path)
# Save the PDF
pdf_document.SaveAs(pdf_file_path)
#Create a map with Folium and save it as an HTML file
from ironpdf import * import warnings import folium warnings.filterwarnings( 'ignore') License.LicenseKey = "";
#Create a map centered at a specific latitude and longitude
#Add a marker
#Save the map to an HTML file
#Convert the HTML file to PDF
#Convert HTML to PDF
#Save the PDF
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'm = folium.Map(location=[45.5236, -122.6750], zoom_start=13) folium.Marker([45.5236, -122.6750], popup='Portland, @OR').add_to(m) m.save('map.html') html_file_path = 'map.html' pdf_file_path = 'map.pdf' html_to_pdf = ChromePdfRenderer() pdf_document = html_to_pdf.RenderHtmlFileAsPdf(html_file_path) pdf_document.SaveAs(pdf_file_path)
$vbLabelText   $csharpLabel

首先,需要导入所有必要的库。 之后,导入了IronPDF和地图类的Folium以制作PDF。 warnings.filterwarnings("忽略")将抑制警告,以使输出看起来更简洁。 设置IronPDF的许可证密钥,尽管这里是空的。

您可以通过初始化一个地图对象来创建一个 Folium 地图,以使其居中于某些纬度和经度坐标,例如俄勒冈州的波特兰,并设置缩放级别为 13。在这个地图的确切位置添加一个标记,并配有一个 "Portland, OR" 的弹出标签。 最后,将地图保存到名为“map.html”的HTML文件中。

Folium Python(工作原理:开发者指南):图6 - Folium 分层地图

下一行将 HTML 文件转换为 PDF 文档。 这个步骤是在前一步完成后进行的,其中已将HTML文件和PDF文件路径分别设置为'map.html'和'map.pdf'。 通过IronPDF的ChromePdfRenderer类,HTML 文件被渲染到PDF文件上。最终生成的Adobe PDF文档将命名为“map.pdf”。

该工作流程旨在使用Folium制作一个交互式地图,将地图保存为HTML格式,然后使用IronPDF最终将HTML文件转换为具有专业外观和高质量的PDF。 这将有助于在制作报告或演示文稿时节省大量时间,并在同样场合下需要一个高度可共享且高质量的地图。

Folium Python(它是如何工作的:开发人员指南):图7 - Folium地图PDF

结论

这将构成一个强大且灵活的解决方案,可以制作动态的互动地图,然后通过集成IronPDF将其转换为PDF格式的精美文档。 虽然Folium在前端由于易于使用和多种可定制选项而表现相当出色,使其在可视化地理空间数据方面表现优秀,但IronPDF同样为这些可视化提供了强大的渲染和高质量PDF形式的保留。

这将使您能够轻松共享和移动复杂的地理空间信息。 Folium 和 IronPDF 之间的无缝交互确保综合文档能够包含美观且详细的地图,从而在商业报告、学术研究或个人项目中,提高地理数据的展示效果和实用性。

这将有助于将IronPDF与其他集成铁软件产品通过尖端解决方案为客户提供更高价值和更优质的软件。 这将有助于更轻松地改进项目和流程运营的整体运作。

IronPDF 是一款为开发人员提供的免费软件,具备这些必备功能,并附有详尽的文档、定期更新和活跃的使用。 根据上述信息,开发人员可以确信 Iron Software 将成为现代软件开发项目中可靠的合作伙伴。 开发人员可以通过IronPDF免费试用。 在接下来的几天里,您将确保749美元的许可证费用物有所值。

雷根·彭

雷根·彭

软件工程师

 LinkedIn

Regan毕业于雷丁大学,拥有电子工程学士学位。在加入Iron Software之前,他的前工作职位要求他专注于单一任务;他在Iron Software最喜欢的是能进行多种工作,无论是增加销售价值、技术支持、产品开发还是营销。他喜欢了解开发人员如何使用Iron Software的库,并利用这些知识不断改进文档和开发产品。
< 前一页
Pygal Python(工作原理:开发者指南)
下一步 >
fastText Python(它是如何运行的:开发者指南)