Ejemplo n.º 1
0
    def computeFilledContours(self):
        gx, gy, gz = self._gridData
        levels = self.getLevels()
        #cs = plt.tricontourf(x, y, z, levels, extend=extend)
        CS = plt.contourf(gx, gy, gz, levels, extend='neither')
        polygons = list()
        for i, polygon in enumerate(CS.collections):
            mpoly = []
            for path in polygon.get_paths():
                path.should_simplify = False
                poly = path.to_polygons()
                exterior = []
                holes = []
                if len(poly) > 0:
                    exterior = poly[0]  # and interiors (holes) are in poly[1:]
                    # Crazy correction of one vertice polygon,
                    #  mpl doesn't care about it
                    if len(exterior) == 1:
                        p0 = exterior[0]
                        exterior = np.vstack(exterior, self.epsi_point(p0),
                                             self.epsi_point(p0))
                    if len(poly) > 1:  # There's some holes
                        for h in poly[1:]:
                            if len(h) > 2:
                                holes.append(h)

                mpoly.append([exterior, holes])
            polygons.append([i, levels[i], levels[i + 1], mpoly])
        self._polygons = polygons
Ejemplo n.º 2
0
 def contourf(self,
              col=-1,
              colours=None,
              levels=None,
              percentiles=None,
              multiple=None,
              absolute=False,
              returnContours=False):
     label, data = self.getData(col)
     if multiple:
         data = data * multiple
     if absolute:
         data = np.abs(data)
     levels = self._calcLevels(data, levels, percentiles)
     if not colours:
         colours = ('cyan', 'blue')
     lon = self.array[:, :, 0]
     lat = self.array[:, :, 1]
     cs = plt.contourf(lon, lat, data, levels, colors=colours, label=label)
     result = levels
     if returnContours:
         id = 0
         result = []
         for i, c in enumerate(cs.collections):
             level = levels[i]
             for path in c.get_paths():
                 path.should_simplify = False
                 poly = path.to_polygons()
                 if len(poly[0]) < 3:
                     continue
                 result.append(ContourPoly(level, [poly[0], poly[1:]]))
     return result
Ejemplo n.º 3
0
    def createInnerOutertempLayer(self, triplotcontourf):
        fet = QgsFeature()
        for collection in triplotcontourf.collections:
            vl1temp1 = QgsVectorLayer("Multipolygon?crs=" + str(self.slf_crs),
                                      "temporary_poly_outer ", "memory")
            pr1 = vl1temp1.dataProvider()
            vl2temp1 = QgsVectorLayer("Multipolygon?crs=" + str(self.slf_crs),
                                      "temporary_poly_inner ", "memory")
            pr2 = vl2temp1.dataProvider()
            vl1temp1.startEditing()
            vl2temp1.startEditing()
            for path in collection.get_paths():
                for polygon in path.to_polygons():
                    if len(polygon) >= 3:
                        fet.setGeometry(self.get_outerinner(polygon)[0])
                        fet.setAttributes([])

                        if np.cross(polygon, np.roll(polygon, -1,
                                                     axis=0)).sum() / 2.0 > 0:
                            pr1.addFeatures([fet])
                            vl1temp1.commitChanges()
                        else:
                            pr2.addFeatures([fet])
                            vl2temp1.commitChanges()
        index2 = QgsSpatialIndex(vl2temp1.getFeatures())
        return (vl1temp1, vl2temp1, index2)
Ejemplo n.º 4
0
    def computeFilledContours(self):
        gx, gy, gz = self._gridData
        levels = self.getLevels()
        #cs = plt.tricontourf(x, y, z, levels, extend=extend)
        CS = plt.contourf(gx, gy, gz, levels, extend='neither')
        polygons = list()
        for i, polygon in enumerate(CS.collections):
            mpoly = []
            for path in polygon.get_paths():
                path.should_simplify = False
                poly = path.to_polygons()
                exterior = []
                holes = []
                if len(poly) > 0:
                    exterior = poly[0]  # and interiors (holes) are in poly[1:]
                    # Crazy correction of one vertice polygon,
                    #  mpl doesn't care about it
                    if len(exterior) == 1:
                        p0 = exterior[0]
                        exterior = np.vstack(exterior, self.epsi_point(p0),
                                             self.epsi_point(p0))
                    if len(poly) > 1:  # There's some holes
                        for h in poly[1:]:
                            if len(h) > 2:
                                holes.append(h)

                mpoly.append([exterior, holes])
            polygons.append([i, levels[i], levels[i + 1], mpoly])
        self._polygons = polygons
Ejemplo n.º 5
0
    def _computeFilledContoursForLevel(self,
                                       levels,
                                       extend,
                                       polygons,
                                       oneLevelOnly=False):
        data = self.getData()
        if not data:
            return
        x, y, z = data
        if self._isMPLOk(
        ) == True:  # If so, we can use the tricontour fonction
            try:
                cs = plt.tricontourf(x, y, z, levels, extend=extend)
            except:
                raise ContourGenerationError()
        else:
            gx = x.reshape(self._nr, self._nc)
            gy = y.reshape(self._nr, self._nc)
            gz = z.reshape(self._nr, self._nc)
            try:
                cs = plt.contourf(gx, gy, gz, levels, extend=extend)
            except:
                raise ContourGenerationError()
        levels = [float(l) for l in cs.levels]
        if extend == 'min' or extend == BOTH:
            levels = np.append([
                -np.inf,
            ], levels)
        if extend == 'max' or extend == BOTH:
            levels = np.append(levels, [
                np.inf,
            ])
        # self.progressBar.setRange(0, len(cs.collections))
        for i, polygon in enumerate(cs.collections):
            mpoly = []
            for path in polygon.get_paths():
                path.should_simplify = False
                poly = path.to_polygons()
                exterior = []
                holes = []
                if len(poly) > 0:
                    exterior = poly[0]  #and interiors (holes) are in poly[1:]
                    #Crazy correction of one vertice polygon, mpl doesn't care about it
                    if len(exterior) < 2:
                        continue
                    p0 = exterior[0]
                    exterior = np.vstack(
                        (exterior, self.epsi_point(p0), self.epsi_point(p0)))
                    if len(poly) > 1:  #There's some holes
                        for h in poly[1:]:
                            if len(h) > 2:
                                holes.append(h)

                mpoly.append([exterior, holes])
            if len(mpoly) > 0:
                polygons.append([i, levels[i], levels[i + 1], mpoly])
            if oneLevelOnly:
                break
Ejemplo n.º 6
0
    def _computeFilledContoursForLevel(self,levels,extend,polygons,oneLevelOnly=False):
        x, y, z = self._data
        if self._isMPLOk()==True: # If so, we can use the new tricontour fonction
            cs = plt.tricontourf(x, y, z, levels, extend=extend)
        else:
            gx = x.reshape(self._nr,self._nc)
            gy = y.reshape(self._nr,self._nc)
            gz = z.reshape(self._nr,self._nc)
            cs = plt.contourf(gx, gy, gz, levels, extend=extend)
        levels = [float(l) for l in cs.levels]
        if extend=='min' or extend=='both':
            levels = np.append([-np.inf,], levels)
        if extend=='max' or extend=='both':
            levels = np.append(levels, [np.inf,])
        # self.progressBar.setRange(0, len(cs.collections))
        for i, polygon in enumerate(cs.collections):
            mpoly = []
            for path in polygon.get_paths():
                path.should_simplify = False
                poly = path.to_polygons()
                exterior = []
                holes = []
                if len(poly)>0:
                    exterior = poly[0] #and interiors (holes) are in poly[1:]
                    #Crazy correction of one vertice polygon, mpl doesn't care about it
                    if len(exterior) < 2:
                        continue
                    p0 = exterior[0]
                    exterior = np.vstack((exterior, self.epsi_point(p0), self.epsi_point(p0)))
                    if len(poly)>1: #There's some holes
                        for h in poly[1:]:
                            if len(h)>2:
                                holes.append(h)

                mpoly.append([exterior, holes])
            if len(mpoly) > 0:
                polygons.append([i, levels[i], levels[i+1], mpoly])
            if oneLevelOnly:
                break
 def createInnerOutertempLayer(self,triplotcontourf):
     fet = QgsFeature()
     for collection in triplotcontourf.collections:
         vl1temp1 = QgsVectorLayer("Multipolygon?crs=" + str(self.slf_crs), "temporary_poly_outer ", "memory")
         pr1 = vl1temp1.dataProvider()
         index1 = QgsSpatialIndex()
         vl2temp1 = QgsVectorLayer("Multipolygon?crs=" + str(self.slf_crs), "temporary_poly_inner " , "memory")
         pr2 = vl2temp1.dataProvider()                
         index2 = QgsSpatialIndex()
         vl1temp1.startEditing()
         vl2temp1.startEditing()
         for path in collection.get_paths():
             for polygon in path.to_polygons(): 
                 if len(polygon)>=3 : 
                     fet.setGeometry(self.get_outerinner(polygon)[0])
                     fet.setAttributes([])
                     
                     if  ( np.cross(polygon, np.roll(polygon, -1, axis=0)).sum() / 2.0 >0 ):
                         pr1.addFeatures( [ fet ] )
                         vl1temp1.commitChanges() 
                     else :
                         pr2.addFeatures( [ fet ] )
                         vl2temp1.commitChanges() 
     return (vl1temp1,vl2temp1,index2)