def __DrawPolygon(self, dc, georef, pen, brush, scale, x, y, mx, my): """绘制多边形 @type georef: GDAL Geometry @param georef: 要绘制的几何形状 @type scale: float @param scale: XY需要缩放的倍数 @type x,y: number @param x,y: 返回图像的X,Y @type mx,my: number @param mx,my: 在地图矩形中的图像的X,Y """ #star = time.time() geom = georef.GetGeometryRef(0) points = self.__GetPoints(geom, scale, x, y, mx, my) dc.DrawPolygon(points, 0, 0) #end = time.time() #self.ctime = self.ctime+end-star if self.label and len(self.labstrnow) > 0: dcpsPolygon = CreatePolygon(points) if dcpsPolygon.GetArea() < 100: return dcRect = GetGeoRectGeometry(self.dcrect) dcmPolygon = dcRect.Intersection(dcpsPolygon) if dcmPolygon.GetGeometryType() == ogr.wkbPolygon: cid = dcmPolygon.Centroid() cidx, cidy = cid.GetX(), cid.GetY() self.labelpls.append([[int(cidx), int(cidy)], self.labstrnow]) else: gc = dcmPolygon.GetGeometryCount() for i in range(gc): g = dcmPolygon.GetGeometryRef(i) cid = g.Centroid() cidx, cidy = cid.GetX(), cid.GetY() self.labelpls.append([[int(cidx), int(cidy)], self.labstrnow])
def __DrawLine(self, dc, georef, pen, brush, scale, x, y, mx, my): """绘制线 @type georef: GDAL Geometry @param georef: 要绘制的几何形状 @type scale: float @param scale: XY需要缩放的倍数 @type x,y: number @param x,y: 返回图像的X,Y @type mx,my: number @param mx,my: 在地图矩形中的图像的X,Y """ #star = time.time() points = self.__GetPoints(georef, scale, x, y, mx, my) #end = time.time() #self.ctime += (end-star) dc.DrawLines(points, 0, 0) if self.label and len(self.labstrnow) > 0: dcpsLine = CreateLineString(points) dcRect = GetGeoRectGeometry(self.dcrect) dcmLine = dcRect.Intersection(dcpsLine) if dcmLine.GetGeometryType() == ogr.wkbLineString: lp = LineStringParser(dcmLine) x, y = lp.GetMidPoint() self.labells.append([lp, self.labstrnow]) else: gc = dcmLine.GetGeometryCount() for i in range(gc): g = dcmLine.GetGeometryRef(i) lp = LineStringParser(g) self.labells.append([lp, self.labstrnow])
class HLLayerCanvas(VectorCanvas): """选择高亮feaure绘制的画板 """ def __init__(self, layer): VectorCanvas.__init__(self, layer) self.where = "" self.codec = None def Draw(self, dc, wprect, wgrect): """绘制图层,往后的子类都要继承这个方法 @type dc: wxDC @param dc: 绘制可用的DC @type wprect: L{geosings.core.GeoRect} @param wprect: DC的范围,主要用于分辨率判断 @type wgrect: L{geosings.core.GeoRect} @param wgrect: 地图的地理范围 """ try: x, y, w, h, bw, bh, mx, my = self.layer.CacDrawArgs(wprect, wgrect) scale = bh * 1.0 / h except Exception, args: err('CacDrawArgs false:', args) return self.dcrect = wprect dataset = self.layer.DataSet() exgeom = GetGeoRectGeometry(wgrect) log("Attribute Filter:" + self.where) self.layer.SetAttributeFilter(self.where, self.codec) dataset.SetSpatialFilter(exgeom) featurecount = dataset.GetFeatureCount() dataset.ResetReading() gc = GDC(dc) #换到GC,支持透明 layerGeomT = dataset.GetLayerDefn().GetGeomType() self.symbol = CreateHLSymbol(layerGeomT) pen = getPanSymbol(self.symbol) #wx.BLACK_PEN brush = getBrushSymbol(self.symbol) #wx.RED_BRUSH self.symsize = getSizeSymbol(self.symbol) gc.SetPen(pen) gc.SetBrush(brush) self.minx = self.layer.DataGeoExt.GetLeft() self.maxy = self.layer.DataGeoExt.GetTop() #开始循环绘制Feature feature = dataset.GetNextFeature() while feature: geomref = feature.GetGeometryRef() geomreftype = geomref.GetGeometryType() drawFun = self.featureDrawMap[geomreftype] drawFun(gc, geomref, pen, brush, scale, x, y, mx, my) feature = dataset.GetNextFeature() self.layer.SetAttributeFilter("")
class VectorCanvas(LayerCanvas): """矢量图层的画板 """ def __init__(self, layer): """初始化画板 @type layer: L{geosings.core.Layer} @param layer: 初始化画板的图层 """ self.layer = layer FeatureDrawMap = { ogr.wkbPoint: self.__DrawPoint, ogr.wkbMultiPoint: self.__DrawPoints, ogr.wkbLineString: self.__DrawLine, ogr.wkbMultiLineString: self.__DrawLines, ogr.wkbPolygon: self.__DrawPolygon, ogr.wkbMultiPolygon: self.__DrawPolygons } self.featureDrawMap = FeatureDrawMap self.labelCanvas = None self.label = None def Draw(self, dc, wprect, wgrect): """绘制图层 @type dc: wxDC @param dc: 绘制可用的DC @type wprect: L{geosings.core.GeoRect} @param wprect: DC的范围,主要用于分辨率判断 @type wgrect: L{geosings.core.GeoRect} @param wgrect: 地图的地理范围 """ try: x, y, w, h, bw, bh, mx, my = self.layer.CacDrawArgs(wprect, wgrect) scale = bh * 1.0 / h except Exception, args: err('CacDrawArgs false:', args) return self.dcrect = wprect star = time.time() self.ctime = 0 self.symbol = self.layer.symbol debug(self.symbol) self.label = label = self.layer.labelProps #要不要画Label if label: self.labelps = labelps = [] self.labells = labells = [] self.labelpls = labelpls = [] dataset = self.layer.DataSet() exgeom = GetGeoRectGeometry(wgrect) dataset.SetSpatialFilter(exgeom) featurecount = dataset.GetFeatureCount() dataset.ResetReading() layerGeomT = dataset.GetLayerDefn().GetGeomType() #if layerGeomT==ogr.wkbUnknown: pen = getPanSymbol(self.symbol) #wx.BLACK_PEN brush = getBrushSymbol(self.symbol) #wx.RED_BRUSH self.symsize = getSizeSymbol(self.symbol) dc.SetPen(pen) dc.SetBrush(brush) self.minx = self.layer.DataGeoExt.GetLeft() self.maxy = self.layer.DataGeoExt.GetTop() #开始循环绘制Feature feature = dataset.GetNextFeature() while feature: if label: try: self.labstrnow = feature.GetField(label.field) if not self.labstrnow: self.labstrnow = u"" else: self.labstrnow = astr2utf8(self.labstrnow) except Exception, e: self.label = label = self.layer.labelProps = None raise FieldNotFoundErr(e) geomref = feature.GetGeometryRef() geomreftype = geomref.GetGeometryType() drawFun = self.featureDrawMap[geomreftype] drawFun(dc, geomref, pen, brush, scale, x, y, mx, my) feature = dataset.GetNextFeature()
def __DrawPolygon(self, dc, georef, pen, brush, scale, x, y, mx, my): """绘制多边形 @type georef: GDAL Geometry @param georef: 要绘制的几何形状 @type scale: float @param scale: XY需要缩放的倍数 @type x,y: number @param x,y: 返回图像的X,Y @type mx,my: number @param mx,my: 在地图矩形中的图像的X,Y """ #star = time.time() georef.CloseRings() if georef.GetGeometryCount() > 1: oldpen = dc.GetPen() dc.SetPen(wx.TRANSPARENT_PEN) geom = georef.GetGeometryRef(0) points = self._GetPoints(geom, scale, x, y, mx, my) if points[0] != points[-1]: points = concatenate([points, [points[0]]]) for i in range(georef.GetGeometryCount()): if i == 0: continue path = georef.GetGeometryRef(i) ppoints = self._GetPoints(path, scale, x, y, mx, my) points = concatenate([points, ppoints]) if points[0] != points[-1]: points = concatenate([points, [points[0]]]) dc.DrawPolygon(points, 0, 0) if georef.GetGeometryCount() > 1: dc.SetPen(oldpen) for i in range(georef.GetGeometryCount()): geom = georef.GetGeometryRef(i) points = self._GetPoints(geom, scale, x, y, mx, my) dc.DrawLines(points, 0, 0) #end = time.time() #self.ctime = self.ctime+end-star if self.label and len(self.labstrnow) > 0: dcpsPolygon = CreatePolygon(points) fu = FontUtils(self.label.symbol) w, h = fu.GetFontSize(self.labstrnow) dcRect = GetGeoRectGeometry(self.dcrect) dcmPolygon = dcRect.Intersection(dcpsPolygon) if dcmPolygon.GetGeometryType() == ogr.wkbPolygon: e = dcmPolygon.GetEnvelope() if math.fabs(e[0] - e[1]) < w or math.fabs(e[2] - e[3]) < h: return cid = GetBetterCentroid(dcmPolygon, (w, h)) cidx, cidy = cid.GetX(), cid.GetY() self.labelpls.append([[int(cidx), int(cidy)], self.labstrnow]) else: gc = dcmPolygon.GetGeometryCount() for i in range(gc): g = dcmPolygon.GetGeometryRef(i) e = g.GetEnvelope() if math.fabs(e[0] - e[1]) < w or math.fabs(e[2] - e[3]) < h: continue #cid = g.Centroid() cid = GetBetterCentroid(g, (w, h)) cidx, cidy = cid.GetX(), cid.GetY() self.labelpls.append([[int(cidx), int(cidy)], self.labstrnow])