热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Python瓦片图下载、合并、绘图、标记的代码示例

本文提供了Python瓦片图下载、合并、绘图、标记的代码示例,包括下载代码、多线程下载、图像处理等功能。通过参考geoserver,使用PIL、cv2、numpy、gdal、osr等库实现了瓦片图的下载、合并、绘图和标记功能。代码示例详细介绍了各个功能的实现方法,供读者参考使用。

 下载代码 仅供参考.瓦片服务是我们自己搭的服务器,参考geoserver。

import io
import math
import multiprocessing
import time
import urllib.request as ur
from threading import Thread
import traceback
import PIL.Image as pil
from pathlib import Pathfrom numpy.lib.type_check import imag
import sys
# from PIL import Image
# import Image
import cv2
import numpy as np
from osgeo import gdal, osr
from tools import *
# from tqdm import tqdm, trange# -----------------------------------------------------------# ---------------------------------------------------------
class Downloader(Thread):# multiple threads downloaderdef __init__(self, index, count, urls, datas):# index represents the number of threads# count represents the total number of threads# urls represents the list of URLs nedd to be downloaded# datas represents the list of data need to be returned.super().__init__()self.urls &#61; urlsself.datas &#61; datasself.index &#61; indexself.count &#61; countdef download(self, url):print("下载图片地址",url,)HEADERS &#61; {&#39;User-Agent&#39;: &#39;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.68&#39;}header &#61; ur.Request(url, headers&#61;HEADERS)err &#61; 1while err <11:try:data &#61; ur.urlopen(header, timeout&#61;10).read()except:traceback.print_exc()print("下载错误 重新下载...")err &#43;&#61; 1else:img &#61; cv2.imdecode(np.frombuffer(data, np.uint8), cv2.IMREAD_COLOR)print("最小像素点", np.min(img))if np.min(img) &#61;&#61; 255:print(f"图片是空白的,开始重新下载第{err}次")err &#43;&#61; 1else:return dataraise Exception("网络连接异常.")def run(self):for i, url in enumerate(self.urls):print("下载任务", i, "/", len(self.urls))tile_x, tile_y, z &#61; url.split("&xyz&#61;")[1].split(",")# lon, lat &#61; tile2lonlat(tile_x, tile_y, z)# 如果i除线程总数1取余 不等于 第几个线程 总是0if i % self.count !&#61; self.index:print("warning!!!跳过该url&#xff0c;记录一下", url)continueself.datas[str(tile_x)&#43;"_"&#43;str(tile_y)] &#61; self.download(url)# ---------------------------------------------------------# ---------------------------------------------------------
def getExtent(x1, y1, x2, y2, z, source&#61;"Google China"):pos1x, pos1y &#61; wgs_to_tile(x1, y1, z)pos2x, pos2y &#61; wgs_to_tile(x2, y2, z)Xframe &#61; pixls_to_mercator({"LT": (pos1x, pos1y), "RT": (pos2x, pos1y), "LB": (pos1x, pos2y), "RB": (pos2x, pos2y), "z": z})for i in ["LT", "LB", "RT", "RB"]:Xframe[i] &#61; mercator_to_wgs(*Xframe[i])if source &#61;&#61; "Google":passelif source &#61;&#61; "ZKLF":passelif source &#61;&#61; "Google China":for i in ["LT", "LB", "RT", "RB"]:Xframe[i] &#61; gcj_to_wgs(*Xframe[i])else:raise Exception("Invalid argument: source.")return Xframedef saveTiff(r, g, b, gt, filePath):fname_out &#61; filePathdriver &#61; gdal.GetDriverByName(&#39;GTiff&#39;)# Create a 3-band datasetdset_output &#61; driver.Create(fname_out, r.shape[1], r.shape[0], 3, gdal.GDT_Byte)dset_output.SetGeoTransform(gt)try:proj &#61; osr.SpatialReference()proj.ImportFromEPSG(4326)dset_output.SetSpatialRef(proj)except:print("Error: Coordinate system setting failed")dset_output.GetRasterBand(1).WriteArray(r)dset_output.GetRasterBand(2).WriteArray(g)dset_output.GetRasterBand(3).WriteArray(b)dset_output.FlushCache()dset_output &#61; Noneprint("Image Saved tif图片生成成功")# ---------------------------------------------------------# ---------------------------------------------------------
MAP_URLS &#61; {"onwer_server": "http://ip:port/geoserver/ows?service&#61;WMS&version&#61;1.3.0&transparent&#61;true&request&#61;GetMap&layers&#61;{style}&width&#61;256&height&#61;256&srs&#61;EPSG%3A3857&format&#61;image/png&bbox&#61;{bbox}&xyz&#61;{xyz}","Google": "http://mts0.googleapis.com/vt?lyrs&#61;{style}&x&#61;{x}&y&#61;{y}&z&#61;{z}","Google China": "http://mt2.google.cn/vt/lyrs&#61;{style}&hl&#61;zh-CN&gl&#61;CN&src&#61;app&x&#61;{x}&y&#61;{y}&z&#61;{z}&s&#61;Galile"}def get_url(source, x, y, z, style): #if source &#61;&#61; &#39;Google China&#39;:url &#61; MAP_URLS["Google China"].format(x&#61;x, y&#61;y, z&#61;z, style&#61;style)elif source &#61;&#61; &#39;Google&#39;:url &#61; MAP_URLS["Google"].format(x&#61;x, y&#61;y, z&#61;z, style&#61;style)elif source &#61;&#61; "onwer_server":min_xy_list &#61; tile2lonlat(x, y &#43; 1, z)max_xy_list &#61; tile2lonlat(x &#43; 1, y, z)print("min_xy_list:", min_xy_list)print("max_xy_list:", max_xy_list)lng_min, lat_min &#61; min_xy_list[0], min_xy_list[1]lng_max, lat_max &#61; max_xy_list[0], max_xy_list[1]# gcj02转wgs84# lng_min, lat_min &#61; GCJ02_to_WGS84(min_xy_list[0], min_xy_list[1])lng_min, lat_min &#61; WGS84_to_WebMercator(lng_min, lat_min)# lng_max, lat_max &#61; GCJ02_to_WGS84(max_xy_list[0], max_xy_list[1])lng_max, lat_max &#61; WGS84_to_WebMercator(lng_max, lat_max)bbox &#61; ",".join([str(lng_min), str(lat_min), str(lng_max), str(lat_max)])xyz &#61; ",".join([str(x), str(y), str(z)])url &#61; MAP_URLS["ZKLF"].format(bbox&#61;bbox, style&#61;style, xyz&#61;xyz)else:raise Exception("Unknown Map Source ! ")return urldef get_urls(x1, y1, x2, y2, z, source, style):"""&#64;description :左上角x1,y1右下角x2,y2---------&#64;param :-------&#64;Returns :-------"""pos1x, pos1y &#61; wgs_to_tile(x1, y1, z) # 左上角的瓦片图坐标pos2x, pos2y &#61; wgs_to_tile(x2, y2, z)print("pos1x, pos1y:", pos1x, pos1y)print("pos2x, pos2y:", pos2x, pos2y)lenx &#61; abs(pos2x - pos1x) &#43; 1leny &#61; abs(pos2y - pos1y) &#43; 1print("Total tiles number&#xff1a;{x} X {y}".format(x&#61;lenx, y&#61;leny))print("pos1y, pos1y &#43; leny:", pos1y, pos1y &#43; leny)print("pos1x, pos1x &#43; lenx:", pos1x, pos1x &#43; lenx)urls &#61; [get_url(source, i, j, z, style) for j in range(pos1y, pos1y &#43; leny) for i in range(pos1x, pos1x &#43; lenx)]return urls# ---------------------------------------------------------# ---------------------------------------------------------
def merge_tiles(datas, x1, y1, x2, y2, z):pos1x, pos1y &#61; wgs_to_tile(x1, y1, z)pos2x, pos2y &#61; wgs_to_tile(x2, y2, z)lenx &#61; pos2x - pos1x &#43; 1leny &#61; pos2y - pos1y &#43; 1outpic &#61; pil.new(&#39;RGBA&#39;, (lenx * 256, leny * 256))for i, data in enumerate(datas):picio &#61; io.BytesIO(data)small_pic &#61; pil.open(picio)y, x &#61; i // lenx, i % lenxoutpic.paste(small_pic, (x * 256, y * 256))print(&#39;Tiles merge completed&#39;)return outpicdef download_tiles(urls, multi&#61;1):url_len &#61; len(urls)# datas &#61; [None] * url_lendatas &#61; dict()if multi <1 or multi > 20 or not isinstance(multi, int):raise Exception("multi of Downloader shuold be int and between 1 to 20.")tasks &#61; [Downloader(i, multi, urls, datas) for i in range(multi)]for i in tasks:i.start()for i in tasks:i.join()return datas# ---------------------------------------------------------# ---------------------------------------------------------
def main(left, top, right, bottom, zoom, filePath, style&#61;&#39;s&#39;, server&#61;"Google China", zone_id&#61;""):"""Download images based on spatial extent.East longitude is positive and west longitude is negative.North latitude is positive, south latitude is negative.Parameters----------left, top : left-top coordinate, for example (100.361,38.866)right, bottom : right-bottom coordinatez : zoomfilePath : File path for storing results, TIFF formatstyle : m for map; s for satellite; y for satellite with label; t for terrain; p for terrain with label; h for label;source : Google China (default) or Google"""# ---------------------------------------------------------# Get the urls of all tiles in the extenturls &#61; get_urls(left, top, right, bottom, zoom, server, style)print("瓦片图总数:", len(urls))# Group URLs based on the number of CPU cores to achieve roughly equal amounts of tasksurls_group &#61; [urls[i:i &#43; math.ceil(len(urls) / 2)] for i inrange(0, len(urls), math.ceil(len(urls) / 2))]# urls_group &#61; [urls[i:i &#43; math.ceil(len(urls) / multiprocessing.cpu_count())] for i in# range(0, len(urls), math.ceil(len(urls) / multiprocessing.cpu_count()))]print(urls_group)# return False# Each set of URLs corresponds to a process for downloading tile mapsprint(&#39;Tiles downloading......瓦片图下载中&#39;)# results &#61; []pool &#61; multiprocessing.Pool(2)# pool &#61; multiprocessing.Pool(multiprocessing.cpu_count())print("cpu_count:", multiprocessing.cpu_count())print("pool", pool)results &#61; pool.map(download_tiles, urls_group)pool.close()pool.join()# print("results:", type(results[0]))image_number &#61; 1for res in results:for key in res.keys():print(f"开始保存第{image_number}张图片")print("image_name:", key)x &#61; str(key).split("_")[0]y &#61; str(key).split("_")[1]parent_dir &#61; Path(f"D:\\tiles\\{zone_id}\\tiles\\{zoom}\\{x}") # 父级目录if not parent_dir.exists():parent_dir.mkdir(exist_ok&#61;True, parents&#61;True)with open(f"D:\\tiles\\{zone_id}\\tiles\\{zoom}\\{x}\\{y}.png", "wb") as f:# print(_)print("sys.getsizeof(res[key]):", sys.getsizeof(res[key]))f.write(res[key])image_number &#43;&#61; 1# print(res.get())# result &#61; [x for j in results for x in j]print(&#39;Tiles download complete 瓦片图 下载成功&#39;)# # break# Combine downloaded tile maps into one map# outpic &#61; merge_tiles(result, left, top, right, bottom, zoom)# outpic &#61; outpic.convert(&#39;RGB&#39;)# r, g, b &#61; cv2.split(np.array(outpic))# # Get the spatial information of the four corners of the merged map and use it for outputting# extent &#61; getExtent(left, top, right, bottom, zoom, server)# gt &#61; (extent[&#39;LT&#39;][0], (extent[&#39;RB&#39;][0] - extent[&#39;LT&#39;][0]) / r.shape[1], 0, extent[&#39;LT&#39;][1], 0,# (extent[&#39;RB&#39;][1] - extent[&#39;LT&#39;][1]) / r.shape[0])# saveTiff(r, g, b, gt, filePath)# ---------------------------------------------------------
if __name__ &#61;&#61; &#39;__main__&#39;:from sqls import select_zone_infoimport jsonstart_time &#61; time.time()zone_id &#61; "1547212382202232832"# main(118.901, 32.066,118.934, 32.109, 18, r&#39;.\Temp\test.tif&#39;, server&#61;"Google China")# main(100.361, 38.866, 100.386, 38.839, 18, r&#39;.\Temp\test.tif&#39;, server&#61;"Google China")# main(97.376955,37.133309,97.4074156,37.118448, 5, r&#39;.\Temp\test-one.tif&#39;, server&#61;"onwer_server", style&#61;"onwer_server_AREA:onwer_server_super_hd")# path &#61; [{"lng": 118.909207, "lat": 32.081909}, {"lng": 118.912005, "lat": 32.081703}, {"lng": 118.911776, "lat": 32.081512}, {"lng": 118.909180, "lat": 32.080421}]left_top_x, left_top_y, right_buttom_x, right_buttom_y &#61; get_zone_gps_max(zone_id)# print(float(left_top[0]), float(left_top[1]), float(right_buttom[0]), float(right_buttom[1]))for zoom in range(18, 19):parent_dir &#61; Path(f"D:\\tiles\\{zone_id}\\tiles\\{zoom}") # 父级目录if not parent_dir.exists():parent_dir.mkdir(exist_ok&#61;True, parents&#61;True)if 1:main(float(left_top_x),float(left_top_y),float(right_buttom_x),float(right_buttom_y), zoom, r&#39;.\\Temp\\test-one.tif&#39;, server&#61;"onwer_server", style&#61;"onwer_server_AREA:onwer_server_super_hd", zone_id&#61;zone_id)#main(97.376955,37.133309,97.376955,37.133309, 19, r&#39;.\Temp\test-one.tif&#39;, server&#61;"Google China")end_time &#61; time.time()print(&#39;lasted a total of {:.2f} seconds&#39;.format(end_time - start_time))

2.合并瓦片图  谷歌的瓦片图长这样.瓦片图是金字塔类型的&#xff0c;这里就不多做解释了.

 合并代码  就是创建一个大的画布&#xff0c;然后把下载好的瓦片图一张张的贴上去&#xff0c;没有难度

import glob
import re
from PIL import Imagefiles &#61; glob.glob(&#39;D:\\tiles\\1547212382202232832\\tiles\\17\\*\\*.png&#39;)
# print(re.findall(r"\d&#43;", files[0])[-2:])
# print(tuple(int(i) for i in re.findall(r"\d&#43;", files[0])[-2:]))
files.sort(key&#61;lambda x: tuple(int(i) for i in re.findall(r"\d&#43;", x)[-2:]))
# print(files)
imagefiles &#61; {}
for item in files:match &#61; re.findall(r&#39;\d&#43;&#39;, item)# print(match[-2])pre &#61; int(match[-2])if not imagefiles.get(pre):imagefiles[pre] &#61; []imagefiles[pre].append(item)imagefiles &#61; sorted(zip(imagefiles.keys(), imagefiles.values()))
print(imagefiles)
total_width &#61; len(imagefiles) * 256
total_height &#61; len(imagefiles[0][1]) * 256new_image &#61; Image.new(&#39;RGB&#39;, (total_width, total_height))x_offset &#61; 0
for item in imagefiles:y_offset &#61; 0# print(item[1])images &#61; map(Image.open, item[1])# print(list(images))for subitem in images:new_image.paste(subitem, (x_offset, y_offset))y_offset &#43;&#61; subitem.size[0]_x &#61; subitem.size[0]# print(list(images))x_offset &#43;&#61; _xnew_image.save(&#39;merge.jpg&#39;, quality&#61;90)

3.在合并好的瓦片图上绘制我们的mark点和多边形。

思路&#xff1a;首先我们合并好的瓦片图上只有像素一个计量单位&#xff0c;如果要化gps点上去的话&#xff0c;就要找到一个全局的参考坐标。最好的参考坐标就是左上角第一块瓦片。找到左上角的点坐标。因为切出来的瓦片像素是我们自定义的&#xff0c;我用的是256*256,同时可以获取到瓦片的实际长度和宽度(就是bbox参数/墨卡托投影),由此我们可以算出单位像素对应的实际长度(单位是米)。   接下来我们只需要找到左上角第一块瓦片的左上角的点坐标即可。   通过左上角瓦片图的gps可以算出对应的瓦片图坐标&#xff0c;根据瓦片图坐标既可以算出 瓦片的左下角坐标和右上角坐标&#xff0c;既得左上角坐标。再转成墨卡托投影即可作为全局的参考坐标了。 ps:瓦片的范围用最小外接矩形来算。

def get_zone_gps_max(zone_id):path_info &#61; select_zone_info(zone_id)path &#61; json.loads(path_info[0]["path"])path &#61; [list(WGS84_to_WebMercator(_["lng"], _["lat"])) for _ in path]print(path)# left_top, right_buttom &#61; get_maxarea_box(path)min_x, max_x, min_y, max_y &#61; get_maxarea_box(path)print("右下→左下→左上→右上:", min_x, max_x, min_y, max_y)left_top_x, left_top_y &#61; WebMercator_to_WGS84(min_x, max_y)right_buttom_x, right_buttom_y &#61; WebMercator_to_WGS84(max_x, min_y)return (float(left_top_x), float(left_top_y), float(right_buttom_x), float(right_buttom_y))def get_first_tile_poi(zone_id, z&#61;17):"""&#64;description :获取第一块瓦片图的坐标---------&#64;param :-------&#64;Returns :左下角和右上角-------"""left_top_x, left_top_y, right_buttom_x, right_buttom_y &#61; get_zone_gps_max(zone_id)pos1x, pos1y &#61; wgs_to_tile(left_top_x, left_top_y, z) # 左上角的瓦片图坐标min_xy_list &#61; tile2lonlat(pos1x, pos1y &#43; 1, z)max_xy_list &#61; tile2lonlat(pos1x &#43; 1, pos1y, z)lng_min, lat_min &#61; min_xy_list[0], min_xy_list[1]lng_max, lat_max &#61; max_xy_list[0], max_xy_list[1]# gcj02转wgs84# lng_min, lat_min &#61; GCJ02_to_WGS84(min_xy_list[0], min_xy_list[1])lng_min, lat_min &#61; WGS84_to_WebMercator(lng_min, lat_min)# lng_max, lat_max &#61; GCJ02_to_WGS84(max_xy_list[0], max_xy_list[1])lng_max, lat_max &#61; WGS84_to_WebMercator(lng_max, lat_max)bbox &#61; [lng_min, lat_min, lng_max, lat_max]return bbox

有疑问或者错误的地方 可以留言讨论 互相学习


推荐阅读
  • 本文介绍了使用cacti监控mssql 2005运行资源情况的操作步骤,包括安装必要的工具和驱动,测试mssql的连接,配置监控脚本等。通过php连接mssql来获取SQL 2005性能计算器的值,实现对mssql的监控。详细的操作步骤和代码请参考附件。 ... [详细]
  • 基于dlib的人脸68特征点提取(眨眼张嘴检测)python版本
    文章目录引言开发环境和库流程设计张嘴和闭眼的检测引言(1)利用Dlib官方训练好的模型“shape_predictor_68_face_landmarks.dat”进行68个点标定 ... [详细]
  • 本文介绍了解决Netty拆包粘包问题的一种方法——使用特殊结束符。在通讯过程中,客户端和服务器协商定义一个特殊的分隔符号,只要没有发送分隔符号,就代表一条数据没有结束。文章还提供了服务端的示例代码。 ... [详细]
  • 本文介绍了在rhel5.5操作系统下搭建网关+LAMP+postfix+dhcp的步骤和配置方法。通过配置dhcp自动分配ip、实现外网访问公司网站、内网收发邮件、内网上网以及SNAT转换等功能。详细介绍了安装dhcp和配置相关文件的步骤,并提供了相关的命令和配置示例。 ... [详细]
  • 搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的详细步骤
    本文详细介绍了搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的步骤,包括环境说明、相关软件下载的地址以及所需的插件下载地址。 ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • 本文介绍了在使用Python中的aiohttp模块模拟服务器时出现的连接失败问题,并提供了相应的解决方法。文章中详细说明了出错的代码以及相关的软件版本和环境信息,同时也提到了相关的警告信息和函数的替代方案。通过阅读本文,读者可以了解到如何解决Python连接服务器失败的问题,并对aiohttp模块有更深入的了解。 ... [详细]
  • Java在运行已编译完成的类时,是通过java虚拟机来装载和执行的,java虚拟机通过操作系统命令JAVA_HOMEbinjava–option来启 ... [详细]
  • 本文介绍了Windows操作系统的版本及其特点,包括Windows 7系统的6个版本:Starter、Home Basic、Home Premium、Professional、Enterprise、Ultimate。Windows操作系统是微软公司研发的一套操作系统,具有人机操作性优异、支持的应用软件较多、对硬件支持良好等优点。Windows 7 Starter是功能最少的版本,缺乏Aero特效功能,没有64位支持,最初设计不能同时运行三个以上应用程序。 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • 也就是|小窗_卷积的特征提取与参数计算
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了卷积的特征提取与参数计算相关的知识,希望对你有一定的参考价值。Dense和Conv2D根本区别在于,Den ... [详细]
  • 欢乐的票圈重构之旅——RecyclerView的头尾布局增加
    项目重构的Git地址:https:github.comrazerdpFriendCircletreemain-dev项目同步更新的文集:http:www.jianshu.comno ... [详细]
  • EzPP 0.2发布,新增YAML布局渲染功能
    EzPP发布了0.2.1版本,新增了YAML布局渲染功能,可以将YAML文件渲染为图片,并且可以复用YAML作为模版,通过传递不同参数生成不同的图片。这个功能可以用于绘制Logo、封面或其他图片,让用户不需要安装或卸载Photoshop。文章还提供了一个入门例子,介绍了使用ezpp的基本渲染方法,以及如何使用canvas、text类元素、自定义字体等。 ... [详细]
  • 十大经典排序算法动图演示+Python实现
    本文介绍了十大经典排序算法的原理、演示和Python实现。排序算法分为内部排序和外部排序,常见的内部排序算法有插入排序、希尔排序、选择排序、冒泡排序、归并排序、快速排序、堆排序、基数排序等。文章还解释了时间复杂度和稳定性的概念,并提供了相关的名词解释。 ... [详细]
  • 本文整理了常用的CSS属性及用法,包括背景属性、边框属性、尺寸属性、可伸缩框属性、字体属性和文本属性等,方便开发者查阅和使用。 ... [详细]
author-avatar
红星闪闪爱哭的鱼
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有