def renderit(poly_sym=None, fig_index=0): # mapnik.Color('y') m = mapnik.Map(600, 200, "+proj=latlong +datum=WGS84") # m.background = mapnik.Color('steelblue') s = mapnik.Style() r = mapnik.Rule() # polygon_symbolizer = mapnik.PolygonSymbolizer(mapnik.Color('#f2eff9')) # polygon_symbolizer = mapnik.PolygonSymbolizer(mapnik.Color('blue')) r.symbols.append(poly_sym) # line_symbolizer = mapnik.LineSymbolizer(mapnik.Color('rgb(50%,50%,50%)'),0.1) line_symbolizer = mapnik.LineSymbolizer() line_symbolizer.stroke = mapnik.Color('rgb(50%,50%,50%)') line_symbolizer.stroke_linecap = mapnik.stroke_linecap.ROUND_CAP line_symbolizer.stroke_width = 5.0 # line_symbolizer.stroke_width = 0.1 # line_symbolizer.stroke_dasharray( [5,10]) r.symbols.append(line_symbolizer) s.rules.append(r) m.append_style('My Style', s) lyr = mapnik.Layer('world', "+proj=latlong +datum=WGS84") lyr.datasource = mapnik.Shapefile(file=os.path.join( os.path.dirname(__file__), '/gdata/fig_data/fig_data_line3.shp')) lyr.styles.append('My Style') m.layers.append(lyr) m.zoom_all() # mapnik.render_to_file(m, 'xx_world_fk.png', 'png') mapnik.render_to_file(m, get_tmp_file(__file__, fig_index)) mapnik.render_to_file(m, get_tmp_file(__file__, fig_index, file_ext='pdf'))
def gen_label(): extfile = get_tmp_file(__file__, '_pt', 'shp') driver = ogr.GetDriverByName("ESRI Shapefile") if os.access(extfile, os.F_OK): driver.DeleteDataSource(extfile) newds = driver.CreateDataSource(extfile) layernew = newds.CreateLayer('point', None, ogr.wkbPoint) fieldcnstr = ogr.FieldDefn("id", ogr.OFTInteger) # fieldcnstr.SetWidth(32) layernew.CreateField(fieldcnstr) fieldf = ogr.FieldDefn("name", ogr.OFTString) layernew.CreateField(fieldf) point_coors_arr = [ [1, 0], [2, 0], [3, 0], [4, 0], ] idx = 1 for point_coors in point_coors_arr: wkt = 'POINT (%f %f)' % (point_coors[0], point_coors[1]) # wkt = 'LINESTRING(3 4,10 50,20 25)' print(wkt) geom = ogr.CreateGeometryFromWkt(wkt) feat = ogr.Feature(layernew.GetLayerDefn()) feat.SetField('id', idx) feat.SetField('name', '标注') feat.SetGeometry(geom) layernew.CreateFeature(feat) idx += 1 newds.Destroy()
def gen_line(): extfile = get_tmp_file(__file__, '_line', 'shp') driver = ogr.GetDriverByName("ESRI Shapefile") if os.access(extfile, os.F_OK): driver.DeleteDataSource(extfile) newds = driver.CreateDataSource(extfile) layernew = newds.CreateLayer('point', None, ogr.wkbLineString) fieldcnstr = ogr.FieldDefn("id", ogr.OFTInteger) # fieldcnstr.SetWidth(32) layernew.CreateField(fieldcnstr) fieldf = ogr.FieldDefn("name", ogr.OFTString) layernew.CreateField(fieldf) point_coors_arr = [ [0, 0, 1, 2, 3, -2, 6, 0], [7, 0, 8, 2, 10, -2, 13, 0], [14, 0, 15, 2, 17, -2, 20, 0]] idx = 1 for point_coors in point_coors_arr: wkt = 'LINESTRING (%f %f, %f %f, %f %f, %f %f)' % ( point_coors[0], point_coors[1], point_coors[2], point_coors[3], point_coors[4], point_coors[5], point_coors[6], point_coors[7]) # wkt = 'LINESTRING(3 4,10 50,20 25)' print(wkt) geom = ogr.CreateGeometryFromWkt(wkt) feat = ogr.Feature(layernew.GetLayerDefn()) feat.SetField('id', idx) feat.SetField('name', 'road') feat.SetGeometry(geom) layernew.CreateFeature(feat) idx += 1 newds.Destroy()
def renderit(shpfile='', sig=''): poly_sym = mapnik.PolygonSymbolizer() poly_sym.fill = mapnik.Color('#f2eff9') # mapnik.Color('y') m = mapnik.Map(600, 300, "+proj=latlong +datum=WGS84") # m.background = mapnik.Color('steelblue') s = mapnik.Style() r = mapnik.Rule() # polygon_symbolizer = mapnik.PolygonSymbolizer(mapnik.Color('#f2eff9')) # polygon_symbolizer = mapnik.PolygonSymbolizer(mapnik.Color('blue')) r.symbols.append(poly_sym) # line_symbolizer = mapnik.LineSymbolizer(mapnik.Color('rgb(50%,50%,50%)'),0.1) line_symbolizer = mapnik.LineSymbolizer() line_symbolizer.stroke = mapnik.Color('#000000') line_symbolizer.stroke_linecap = mapnik.stroke_linecap.ROUND_CAP line_symbolizer.stroke_width = 1.2 # line_symbolizer.stroke_width = 0.1 # line_symbolizer.stroke_dasharray( [5,10]) r.symbols.append(line_symbolizer) s.rules.append(r) m.append_style('My Style', s) lyr = mapnik.Layer('world', "+proj=latlong +datum=WGS84") lyr.datasource = mapnik.Shapefile(file=shpfile) lyr.styles.append('My Style') m.layers.append(lyr) # bbox = mapnik.Box2d(70, 20, 135, 57) m.zoom_all() # mapnik.render_to_file(m, 'xx_world_fk.png', 'png') mapnik.render_to_file(m, get_tmp_file(__file__, sig)) mapnik.render_to_file(m, get_tmp_file(__file__, sig, file_ext='pdf'), 'pdf')
# -*- coding: utf-8 -*- print('=' * 40) print(__file__) from helper.textool import get_tmp_file ################################################################################ import os, mapnik from gispy_helper import renderit line_symbolizer = mapnik.LineSymbolizer() m = renderit(line_sym=line_symbolizer) # mapnik.render_to_file(m, 'xx_mnik_out.png') mapnik.render_to_file(m, get_tmp_file(__file__, '1'), 'png') mapnik.render_to_file(m, get_tmp_file(__file__, '1', file_ext='pdf'), 'pdf') ################################################################################ line_symbolizer.stroke = mapnik.Color('rgb(50%,50%,50%)') line_symbolizer.stroke_width = 15.0 m = renderit(line_sym=line_symbolizer) # mapnik.render_to_file(m, 'xx_mnik_out2.png') mapnik.render_to_file(m, get_tmp_file(__file__, '2'), 'png') mapnik.render_to_file(m, get_tmp_file(__file__, '2', file_ext='pdf'), 'pdf') ################################################################################ line_symbolizer.stroke_opacity = 0.8 m = renderit(line_sym=line_symbolizer) # mapnik.render_to_file(m, 'xx_mnik_out3.png')
} cfg2 = { 'shape': 'ellipse', 'fixedsize': True, 'style': 'rounded,filled', 'width': 2, 'fontname': 'Arial', 'fontsize': 10, 'concentrate': True, } for node_name in ['stats_conty.shp', 'xx_couties.shp']: G.add_node(node_name, fillcolor="#ffffff", **cfg) cfg['style'] = 'filled' for node_name in ['stats_conty', 'new_county', 'new_county2']: G.add_node(node_name, fillcolor="#99ffff", **cfg) # for node_name in ['stats_conty', 'new_county', 'new_county2']: # G.add_node(node_name, fillcolor="#99ffff", **cfg2) G.add_edge('stats_conty.shp', 'stats_conty', label='ogr2ogr') G.add_edge('stats_conty', 'xx_couties.shp', label='.dump') G.add_edge('xx_couties.shp', 'new_county', '.loadshp') G.add_edge('xx_couties.shp', 'new_county2', '.loadshp') G.layout(prog='dot') # default to neato G.draw(get_tmp_file(__file__, '1', file_ext='pdf')) G.draw(get_tmp_file(__file__, '1', file_ext='png'))
lat_0=0, lon_0=70, resolution='l', area_thresh=10000.0) my_map.drawcoastlines() # my_map.drawcountries() # my_map.fillcontinents(color='coral') # my_map.drawmapboundary() my_map.drawmeridians(np.arange(0, 360, 30)) my_map.drawparallels(np.arange(-90, 90, 30)) p2 = plt.subplot(122) my_map2 = Basemap(projection='ortho', lat_0=0, lon_0=-110, resolution='l', area_thresh=10000.0) my_map2.drawcoastlines() # my_map2.drawcountries() # my_map2.fillcontinents(color='coral') # my_map2.drawmapboundary() my_map2.drawmeridians(np.arange(0, 360, 30)) my_map2.drawparallels(np.arange(-90, 90, 30)) # plt.show() plt.savefig(get_tmp_file(__file__, '1', file_ext='png')) plt.savefig(get_tmp_file(__file__, '1', file_ext='pdf')) plt.clf()
print(__file__) from helper.textool import get_tmp_file ################################################################################ import mapnik m = mapnik.Map(600, 500, "+proj=latlong +datum=WGS84") symbol = mapnik.RasterSymbolizer() ################################################################################ s = mapnik.Style() r = mapnik.Rule() r.symbols.append(symbol) s.rules.append(r) m.append_style('My Style', s) datasource = mapnik.Gdal(file='/gdata/geotiff_file.tif') layer = mapnik.Layer("myLayer") layer.datasource = datasource layer.styles.append('My Style') m.layers.append(layer) ################################################################################ layer.envelope() ################################################################################ m.zoom_to_box(layer.envelope()) # mapnik.render_to_file(m, 'xx_mapnik_result.png', 'png') mapnik.render_to_file(m, get_tmp_file(__file__, '1'), 'png') mapnik.render_to_file(m, get_tmp_file(__file__, '1',file_ext='pdf'), 'pdf')
ax = fig.add_subplot(223) # Make the polygon into a patch and add it to the subplot patch = PolygonPatch(multipolygon, facecolor='#cccccc', edgecolor='#999999') ax.add_patch(patch) # Fit the figure around the polygon's bounds, render, and save minx, miny, maxx, maxy = polygon.bounds w, h = maxx - minx, maxy - miny ax.set_xlim(minx - 0.2 * w, maxx + 0.2 * w) ax.set_ylim(miny - 0.2 * h, maxy + 0.2 * h) ax.set_aspect(1) # 4 # Create a subplot ax = fig.add_subplot(224) # Turn the GeoJSON-ish dict form of the polygon from #1 into a patch geo = multipolygon.__geo_interface__ patch = PolygonPatch(geo, facecolor='#cccccc', edgecolor='#999999') ax.add_patch(patch) # Fit the figure around the polygon's bounds, render, and save minx, miny, maxx, maxy = polygon.bounds w, h = maxx - minx, maxy - miny ax.set_xlim(minx - 0.2 * w, maxx + 0.2 * w) ax.set_ylim(miny - 0.2 * h, maxy + 0.2 * h) ax.set_aspect(1) # fig.savefig('patches.png') # plt.show() plt.savefig(get_tmp_file(__file__, '2'), bbox_inches='tight') plt.savefig(get_tmp_file(__file__, '2', file_ext='pdf'), bbox_inches='tight') plt.clf()
# -*- coding: utf-8 -*- import mapnik from helper.textool import get_tmp_file import os stylesheet = os.path.splitext(os.path.realpath(__file__))[0] + '.xml' m = mapnik.Map(600, 200) mapnik.load_map(m, stylesheet) m.zoom_all() env = m.envelope() print(env) box = mapnik.Box2d(env.minx - .3, env.miny - .3, env.maxx + .3, env.maxy + .3) m.zoom_to_box(box) mapnik.render_to_file(m, get_tmp_file(__file__, 3)) mapnik.render_to_file(m, get_tmp_file(__file__, 3, file_ext='pdf'))
dem_tif = '/gdata/sample_files/dem.tiff' ################################################################################ p1 = plt.subplot(121) mymap = Basemap(**para) ds = gdal.Open(dem_tif) data = ds.ReadAsArray() x = linspace(0, mymap.urcrnrx, data.shape[1]) y = linspace(0, mymap.urcrnry, data.shape[0]) xx, yy = meshgrid(x, y) cs = mymap.contour(xx, yy, data, range(400, 1500, 100), cmap=plt.cm.cubehelix) ################################################################################ p2 = plt.subplot(122) mymap = Basemap(**para) ds = gdal.Open(dem_tif) data = ds.ReadAsArray() x = linspace(0, mymap.urcrnrx, data.shape[1]) y = linspace(0, mymap.urcrnry, data.shape[0]) xx, yy = meshgrid(x, y) cs = mymap.contour(xx, yy, data, range(400, 1500, 100), cmap=plt.cm.cubehelix) plt.clabel(cs, inline=True, fmt='%1.0f', fontsize=8, colors='k') # plt.show() plt.savefig(get_tmp_file(__file__, '1'), bbox_inches='tight', dpi=600) plt.savefig(get_tmp_file(__file__, '1', file_ext='pdf'), bbox_inches='tight', dpi=600) plt.clf()
plot_line(ax, line) dilated = line.buffer(0.5) patch1 = PolygonPatch(dilated, fc=BLUE, ec=BLUE, alpha=0.5, zorder=2) ax.add_patch(patch1) #2 ax = fig.add_subplot(122) patch2a = PolygonPatch(dilated, fc=GRAY, ec=GRAY, alpha=0.5, zorder=1) ax.add_patch(patch2a) eroded = dilated.buffer(-0.3) # GeoJSON-like data works as well polygon = eroded.__geo_interface__ # >>> geo['type'] # 'Polygon' # >>> geo['coordinates'][0][:2] # ((0.50502525316941682, 0.78786796564403572), (0.5247963548222736, 0.8096820147509064)) patch2b = PolygonPatch(polygon, fc=BLUE, ec=BLUE, alpha=0.5, zorder=2) ax.add_patch(patch2b) # pyplot.savefig('figs/xx_shapely_buffer.png') pyplot.savefig(get_tmp_file(__file__, '1'), bbox_inches='tight') pyplot.savefig(get_tmp_file(__file__, '1', file_ext='pdf'), bbox_inches='tight') pyplot.clf()
import os # from pygraphviz import Digraph # from graphviz import Digraph, Graph from helper.textool import get_tmp_file import pygraphviz as pgv G = pgv.AGraph(directed=True) # g = Graph(format='png') # dot = Digraph() # dot.format = 'png' # # # dot = Digraph(comment='The Round Table') G.add_edge('Symbol', 'Rule', label=" r.symbols.append()", style="dash") G.add_edge('Rule', 'Style', label=" s.rules.append()", style="dash") G.add_edge('Style', 'Map', label=" m.append_style()", style="dash") G.add_edge('Map', 'NamedStyle') G.add_edge('NamedStyle', 'Layer', label=" lyr.styles.append()") G.add_edge('Layer', 'Map', label=" m.layers.append()") G.add_edge('Map', 'Output', label=" render()") G.layout(prog='dot') # default to neato outpng = get_tmp_file(__file__) G.draw(outpng) outpdf = get_tmp_file(__file__, file_ext='pdf') G.draw(outpdf)
]) polys2 = gpd.GeoSeries([Polygon([(3, .5), (7, .5), (7, 2.5), (3, 2.5)])]) df1 = gpd.GeoDataFrame({'geometry': polys1, 'df1': [1, 2]}) df2 = gpd.GeoDataFrame({'geometry': polys2, 'df2': [1]}) ################################################################################ ax = df1.plot(color='red') df2.plot(ax=ax, color='green') import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt # plt.show() plt.savefig(get_tmp_file(__file__, '1'), bbox_inches='tight', dpi=600) plt.savefig(get_tmp_file(__file__, '1', file_ext='pdf'), bbox_inches='tight', dpi=600) plt.clf() ################################################################################ res_union = gpd.overlay(df1, df2, how='union') ################################################################################ ax = res_union.plot() df1.plot(ax=ax, facecolor='none') df2.plot(ax=ax, facecolor='none') # plt.show()
################################################################# xx, yy = get_box(22, 82, 2) # plt.plot(xx, yy, color_gray) # use pylab to plot x and y xx, yy = get_box(20, 80, 2) plt.plot(xx, yy, color_gray) # use pylab to plot x and y xx, yy = get_box(20, 80) plt.plot(xx, yy, color_gray) # use pylab to plot x and y ####################################################################### xx = [30, 70, 70, 30, 30] yy = [30, 30, 45, 45, 30] plt.plot(xx, yy, color_gray) # use pylab to plot x and y xx = [30, 70, 70, 30, 30] yy = [55, 55, 70, 70, 55] plt.plot(xx, yy, color_gray) # use pylab to plot x and y plt.text(-8, 105, 'Map') plt.text(2, 95, 'Layer') plt.text(12, 85, 'Style') plt.text(22, 75, 'Rule') plt.text(32, 65, 'Filter (optional)') plt.text(32, 40, 'Symbolizer') # plt.show() # show the plot on the screen plot.savefig(get_tmp_file(__file__)) plot.savefig(get_tmp_file(__file__, file_ext='pdf'))