Beispiel #1
0
    def compute(self):
        variable = self.getInputFromPort('tvariable')
        filename = self.getInputFromPort('filename')

        print "convert to vtk image data"
        cv = PVCDMSReader()
        image_data = cv.convert(variable.data)

        print "convert to poly data"
        geom = vtk.vtkImageDataGeometryFilter()
        geom.SetInputData(image_data)
        geom.ReleaseDataFlagOn()

        print "Convert to GeoJSON"
        gw = vtk.vtkGeoJSONWriter()
        gw.SetInputConnection(geom.GetOutputPort())
        gw.WriteToOutputStringOn()
        gw.Write()
        gj = str(gw.RegisterAndGetOutputString()).replace('\n','')
        outf = open(filename, 'w')
        outf.write(gj)
Beispiel #2
0
    def region(self, latBounds, lonBounds, i, userkey):

        starttime()

        cdmsVar = userdata[userkey]['var']
        latCoords = userdata[userkey]['latCoords']
        lonCoords = userdata[userkey]['lonCoords']
        clevs = userdata[userkey]['clevs']

        # self.debug("get data for only this region")
        # need to expand bounds by one due to the difference in how
        # basemap and cdms work with bounds
        t = len(latCoords) - 1
        n = len(lonCoords) - 1
        a, b, c, d = latBounds[0], latBounds[1], lonBounds[0], lonBounds[1]
        regiondata = cdmsVar[:,
                             (a - 1 if a > 0 else a):(b + 1 if b < t else b),
                             (c - 1 if c > 0 else c):(d + 1 if d < n else d)]

        # self.debug("perform pre on data")
        for util in userdata[userkey]['pre_cdutils']:
            if hasattr(cdutil, util):
                f = getattr(cdutil, util)
                f(regiondata)

        for util in userdata[userkey]['cdutils']:
            functionName = util[0]
            if hasattr(cdutil, functionName):
                f = getattr(cdutil, functionName)
                args = util[1]
                kwargs = util[2]
                regiondata = f(regiondata, *args, **kwargs)

#        base64png = self.matPlotContour(regiondata, a, b, c, d, lonCoords,
#                                        lonBounds, latCoords, latBounds, clevs)
#        self.signal('streammaster', 'region', base64png, i, userkey)

        # print "convert to vtk image data"
        cv = PVCDMSReader()
        image_data = cv.convert(regiondata)

        # convert to poly data
        geom = vtk.vtkImageDataGeometryFilter()
        geom.SetInputData(image_data)
        geom.ReleaseDataFlagOn()

        # print "Convert to GeoJSON"
        gw = vtk.vtkGeoJSONWriter()
        gw.SetInputConnection(geom.GetOutputPort())
        gw.WriteToOutputStringOn()
        gw.Write()

        # // @todo: add pretty print option for JSON writer
        gj = str(gw.RegisterAndGetOutputString()).replace('\n', '')

        self.signal('streammaster', 'region', gj, i, userkey)

        # cleanup
        plt.clf()

        stoptime('region')

        return None