學了沒記等于沒學 --賀浩
上次用qgis做了openstreet的地圖輸出,完全沒有任何記錄,現(xiàn)在重新學并記下來
工具
- Qgis
- Openlayer plugin
暫時先這么開始
- 使用openlayer加載osm和bing aerial 圖層;
- 選中區(qū)域, 用vector OSM工具中download data下載osm數(shù)據(jù),保存成.osm 格式,接下來可以讀取該osm文件并為所欲為了;
- 同樣使用Vector->OSM工具將下載的osm文件讀成xml文件;
- 接下來,使用OSM的第三個工具,提取感興趣的點、線和多邊形;
在這之前都沒有什么難度 。
image
我們注意到,圖層管理器中多了一項 ××--polylines,這是因為我感興趣的主要是道路?,F(xiàn)在可以關(guān)掉osm圖層專心研究polylines了
- 雙擊polylines圖層可以查看和修改其屬性,包括目標的名字,性質(zhì),像素寬度等等,如下圖所示。
-
按理來說,現(xiàn)在選擇感興趣區(qū)域,感興趣圖層,手工點菜單欄Project->Save as image 就OK了,但是我還有更多的需求,希望選取一大片區(qū)域,把這片區(qū)域的OSM圖保存成我希望的大小,那么另開一段。
image
將OSM數(shù)據(jù)轉(zhuǎn)換為shp文件并保存為圖片
1 . 首要我們要知道,想要將圖層保存下來,最好將圖層轉(zhuǎn)化成shp格式,因此我們將剛才保存的db文件保存成shp文件
image
- 接下來用一段代碼來總結(jié)這篇筆記 T T
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 25 18:19:28 2018
CONVERT SHP FILE TO IMAGE
@author: hehao
"""
import os
import sys
import mapnik # for drawing
from osgeo import ogr # for open shapefile
shp_path = 'your_path/*.shp'
X = int(10240)
Y = int(10240)
if X <= 0 or Y <= 0:
print('Image size should be positive')
exit()
layer_type = 'pl'
xmin = xmax = ymin = ymax = None
def render_img(width, height, layer, output, minx, miny, maxx, maxy, layer_type):
m = mapnik.Map(width, height)
s = mapnik.Style()
r = mapnik.Rule()
symbolizer = None
if layer_type == "pt":
print('not implement!')
return
elif layer_type == "pl":
symbolizer = mapnik.LineSymbolizer()
symbolizer.stroke = mapnik.Color('#ff0000')
symbolizer.width = 0.3
elif layer_type == "pg":
symbolizer = mapnik.PolygonSymbolizer()
symbolizer.fill = mapnik.Color('#ff0000')
else:
return
r.symbols.append(symbolizer)
s.rules.append(r)
m.append_style('style', s)
mlayer = mapnik.Layer(str("test_layer"))
print('layer param: ', layer)
mlayer.datasource = mapnik.Shapefile(file=layer)
mlayer.styles.append('style')
m.layers.append(mlayer)
tile_bounds = (minx, miny, maxx, maxy)
box = mapnik.Box2d(*tile_bounds)
m.zoom_to_box(box)
print(output)
mapnik.render_to_file(m, output)
ds = ogr.Open(shp_path)
layer = ds.GetLayer(0)
if xmin is None:
xmin, xmax, ymin, ymax = layer.GetExtent()
render_img(X, Y, shp_path, './out.png', xmin, ymin, xmax, ymax, layer_type)
這里放一個怎么使用Qgis下載OSM數(shù)據(jù)并檢索的官方教程