热门标签 | 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

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


推荐阅读
  • Requests库的基本使用方法
    本文介绍了Python中Requests库的基础用法,包括如何安装、GET和POST请求的实现、如何处理Cookies和Headers,以及如何解析JSON响应。相比urllib库,Requests库提供了更为简洁高效的接口来处理HTTP请求。 ... [详细]
  • 高级缩放示例.就像谷歌地图一样.它仅缩放图块,但不缩放整个图像.因此,缩放的瓷砖占据了恒定的记忆,并且不会为大型缩放图像调整大小的图像.对于简化的缩放示例lookhere.在Win ... [详细]
  • AI炼金术:KNN分类器的构建与应用
    本文介绍了如何使用Python及其相关库(如NumPy、scikit-learn和matplotlib)构建KNN分类器模型。通过详细的数据准备、模型训练及新样本预测的过程,展示KNN算法的实际操作步骤。 ... [详细]
  • OBS Studio自动化实践:利用脚本批量生成录制场景
    本文探讨了如何利用OBS Studio进行高效录屏,并通过脚本实现场景的自动生成。适合对自动化办公感兴趣的读者。 ... [详细]
  • Web动态服务器Python基本实现
    Web动态服务器Python基本实现 ... [详细]
  • 在OpenCV 3.1.0中实现SIFT与SURF特征检测
    本文介绍如何在OpenCV 3.1.0版本中通过Python 2.7环境使用SIFT和SURF算法进行图像特征点检测。由于这些高级功能在OpenCV 3.0.0及更高版本中被移至额外的contrib模块,因此需要特别处理才能正常使用。 ... [详细]
  • Jenkins API当前未直接提供获取任务构建队列长度的功能,因此需要通过解析HTML页面来间接实现这一需求。 ... [详细]
  • 深入理解:AJAX学习指南
    本文详细探讨了AJAX的基本概念、工作原理及其在现代Web开发中的应用,旨在为初学者提供全面的学习资料。 ... [详细]
  • 本文介绍了一种方法,通过使用Python的ctypes库来调用C++代码。具体实例为实现一个简单的加法器,并详细说明了从编写C++代码到编译及最终在Python中调用的全过程。 ... [详细]
  • 本文介绍如何使用JavaScript中的for循环来创建一个九九乘法表,适合初学者学习循环结构的应用。 ... [详细]
  • 深入解析层次聚类算法
    本文详细介绍了层次聚类算法的基本原理,包括其通过构建层次结构来分类样本的特点,以及自底向上(凝聚)和自顶向下(分裂)两种主要的聚类策略。文章还探讨了不同距离度量方法对聚类效果的影响,并提供了具体的参数设置指导。 ... [详细]
  • 在Python编程中,经常需要处理文件下载的任务。本文将介绍三种常用的下载方法:使用urllib、urllib2以及requests库进行HTTP请求下载,同时也会提及如何通过ftplib从FTP服务器下载文件。 ... [详细]
  • 机器学习(ML)三之多层感知机
    深度学习主要关注多层模型,现在以多层感知机(multilayerperceptron,MLP)为例,介绍多层神经网络的概念。隐藏层多层感知机在单层神经网络的基础上引入了一到多个隐藏 ... [详细]
  • 分层学习率衰减在NLP预训练模型中的应用
    本文探讨了如何通过分层学习率衰减技术来优化NLP预训练模型的微调过程,特别是针对BERT模型。通过调整不同层的学习率,可以有效提高模型性能。 ... [详细]
  • 本文探讨了在处理大型字典时,如何通过优化字典理解和列表操作来提高代码执行效率。具体问题涉及从字典中筛选出特定条件下的条目。 ... [详细]
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社区 版权所有