示例#1
0
    def test_with_arrays(self):
        fname = 'array_grid.shp'
        outfile = os.path.join(self.outputdir, fname)
        basefile = os.path.join(self.baselinedir, fname)
        iotools.saveGridShapefile(self.grid.x, self.grid.y, self.mask,
                             self.template, outfile, 'w', river=self.river,
                             elev=None)

        testing.compareShapefiles(basefile, outfile, atol=0.001)
示例#2
0
 def test_bad_mode(self):
     outfile = os.path.join(self.outputdir, 'junk.shp')
     iotools.saveGridShapefile(self.grid.x, self.grid.y, self.mask,
                          self.template, outfile, mode='junk')
示例#3
0
    def to_shapefile(self, outputfile, usemask=True, which='cells',
                     river=None, reach=0, elev=None, template=None,
                     geom='Polygon', mode='w', triangles=False):
        """
        Converts a grid to a shapefile via the *fiona* package.

        Parameters
        ----------
        outputfile : str
            The name of the shapefile where the data will be saved.
        usemask : bool, optional
            Toggles the ommission of masked values (as determined by
            :meth:`~cell_mask`.
        which : str, optional
            This can be "nodes" (default) or "cells". Specifies which
            coordinates should be used.
        river : str, optional
            Identifier of the river.
        reach : int or str, optional
            Indetifier of the reach of ``river``.
        elev : numpy.ndarray, optional
            Bathymetry data to be assigned to each record in the
            shapefile.
        template : str, optional
            The shapefile schema template. If not provided, the
            ``template`` attribute of the ModelGrid object is used.
        geom : str, optional
            The type of geometry to use. If "Point", either the grid
            nodes or the centroids of the can be used (see the
            ``which`` parameter). However, if "Polygon" is specified,
            cells will be generated from the nodes, regardless of the
            value of ``which``.
        mode : str, optional
            The mode in which ``outputfile`` will be opened. Should be
            either 'w' (write) or 'a' (append).
        triangles : bool, optional
            Toggles the inclusion of triangular cells.

            .. warning:
               This is experimental and probably buggy if it has been
               implmented at all.

        Returns
        -------
        None

        """

        if template is None:
            template = self.template

        if geom.lower() == 'point':
            x, y = self._get_x_y(which, usemask=usemask)
            iotools.savePointShapefile(x, y, template, outputfile,
                                       mode=mode, river=river, reach=reach,
                                       elev=elev)

        elif geom.lower() in ('cell', 'cells', 'grid', 'polygon'):
            if usemask:
                mask = self.cell_mask.copy()
            else:
                mask = None
            x, y = self._get_x_y('nodes', usemask=False)
            iotools.saveGridShapefile(x, y, mask, template,
                                      outputfile, mode=mode, river=river,
                                      reach=reach, elev=elev,
                                      triangles=triangles)
            if which == 'cells':
                warnings.warn("polygons always constructed from nodes")
        else:
            raise ValueError("geom must be either 'Point' or 'Polygon'")