Example #1
0
 def test_system_converting_coordinate_system(self):
     dest_crs_wkt = 'PROJCS["Sphere_Lambert_Conformal_Conic",GEOGCS["WGS 84",DATUM["unknown",SPHEROID["WGS84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]],PROJECTION["Lambert_Conformal_Conic_2SP"],PARAMETER["standard_parallel_1",30],PARAMETER["standard_parallel_2",60],PARAMETER["latitude_of_origin",40.0000076294],PARAMETER["central_meridian",-97],PARAMETER["false_easting",0],PARAMETER["false_northing",0],UNIT["Meter",1]]'
     sr = osr.SpatialReference()
     sr.ImportFromWkt(dest_crs_wkt)
     gm = GeometryManager('GRIDCODE', path=self.path_nhd_catchments_texas, allow_multipart=True)
     for row_orig, row_transform in zip(gm.iter_records(dest_crs=sr), gm.iter_records()):
         self.assertNotEqual(row_orig['geom'].bounds, row_transform['geom'].bounds)
Example #2
0
    def test_system_read_file_geodatabase(self):
        self.handle_no_geodatabase()

        path = self.path_nhd_seamless_file_geodatabase
        driver_kwargs = {'feature_class': 'Catchment'}
        dest_crs_wkt = 'PROJCS["Sphere_Lambert_Conformal_Conic",GEOGCS["WGS 84",DATUM["unknown",SPHEROID["WGS84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]],PROJECTION["Lambert_Conformal_Conic_2SP"],PARAMETER["standard_parallel_1",30],PARAMETER["standard_parallel_2",60],PARAMETER["latitude_of_origin",40.0000076294],PARAMETER["central_meridian",-97],PARAMETER["false_easting",0],PARAMETER["false_northing",0],UNIT["Meter",1]]'
        sr = osr.SpatialReference()
        sr.ImportFromWkt(dest_crs_wkt)

        gm = GeometryManager('GRIDCODE', path=path, allow_multipart=True, driver_kwargs=driver_kwargs)
        for ctr, row in enumerate(gm.iter_records(dest_crs=sr)):
            if ctr > 10:
                break
            self.assertIsInstance(row['geom'], (Polygon, MultiPolygon))
 def test_iter_records(self):
     # Test interior splitting is performed if requested.
     records = [{
         'geom': self.polygon_with_hole,
         'properties': {
             'GRIDCODE': 81
         }
     }]
     gm = GeometryManager('GRIDCODE',
                          records=records,
                          allow_multipart=True,
                          split_interiors=True)
     records = list(gm.iter_records())
     self.assertEqual(len(records), 1)
     self.assertEqual(len(records[0]['geom']), 4)
Example #4
0
def from_shapefile(path,
                   name_uid,
                   mesh_name='mesh',
                   path_rtree=None,
                   use_ragged_arrays=False,
                   with_connectivity=True,
                   allow_multipart=False,
                   node_threshold=None,
                   driver_kwargs=None,
                   debug=False,
                   dest_crs=None,
                   split_interiors=True):
    """
    Create a flexible mesh from a target shapefile.

    >>> path = '/input/target.shp'
    >>> name_uid = 'UID'
    >>> fm = FlexibleMesh.from_shapefile(path, name_uid)

    :param path: Path to the target shapefile.
    :type path: str
    :param name_uid: Name of the integer unique identifier in the target shapefile. This value will be maintained on
     the output mesh object.
    :type name_uid: str
    :param mesh_name: Name of the mesh catalog variable.
    :type mesh: str
    :param path_rtree: Path to a serialized spatial index object created using ``rtree``. Use :func:`pyugrid.flexible_mesh.helpers.create_rtree_file`
     to create a persistent ``rtree`` spatial index file.
    :type path_rtree: str
    :rtype: :class:`pyugrid.flexible_mesh.core.FlexibleMesh`
    """
    # tdk: update doc
    from utools.io.geom_manager import GeometryManager

    if debug:
        slc = [0, 1]
    else:
        slc = None

    log.debug('creating geometry manager')
    log.debug(('driver_kwargs', driver_kwargs))
    gm = GeometryManager(name_uid,
                         path=path,
                         path_rtree=path_rtree,
                         allow_multipart=allow_multipart,
                         node_threshold=node_threshold,
                         slc=slc,
                         driver_kwargs=driver_kwargs,
                         dest_crs=dest_crs,
                         split_interiors=split_interiors)
    log.debug('geometry manager created')

    ret = get_flexible_mesh(gm,
                            mesh_name,
                            use_ragged_arrays,
                            with_connectivity=with_connectivity)
    log.debug('mesh collection returned')

    return ret
Example #5
0
def validate_esmf_format(ds, name_uid, path_in_shp):
    # Confirm unique identifier is in fact unique.
    uid = ds.variables[name_uid][:]
    assert np.unique(uid).shape[0] == uid.shape[0]

    # Confirm each shapefile element is accounted for.
    assert ds.dimensions['elementCount'].size == len(
        GeometryManager(name_uid, path=path_in_shp))
Example #6
0
    def test_system_convert_file_geodatabase_to_esmf_format(self):
        self.handle_no_geodatabase()

        path = self.path_nhd_seamless_file_geodatabase
        driver_kwargs = {'feature_class': 'Catchment'}
        dest_crs_wkt = 'PROJCS["Sphere_Lambert_Conformal_Conic",GEOGCS["WGS 84",DATUM["unknown",SPHEROID["WGS84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]],PROJECTION["Lambert_Conformal_Conic_2SP"],PARAMETER["standard_parallel_1",30],PARAMETER["standard_parallel_2",60],PARAMETER["latitude_of_origin",40.0000076294],PARAMETER["central_meridian",-97],PARAMETER["false_easting",0],PARAMETER["false_northing",0],UNIT["Meter",1]]'
        sr = osr.SpatialReference()
        sr.ImportFromWkt(dest_crs_wkt)
        out_path = self.get_temporary_file_path('out.nc')

        gm = GeometryManager('GRIDCODE', path=path, allow_multipart=True, node_threshold=5000,
                             driver_kwargs=driver_kwargs, slc=[10, 110])
        coll = get_flexible_mesh(gm, '', True, with_connectivity=False)
        with self.nc_scope(out_path, 'w') as ds:
            convert_collection_to_esmf_format(coll, ds, polygon_break_value=-8, face_uid_name='GRIDCODE')
    def test_convert_to_esmf_format(self):
        # mpirun -n 2 nosetests -vs utools.test.test_utools.test_prep.test_prep_shapefiles:Test.test_convert_to_esmf_format

        name_uid = 'GRIDCODE'
        if MPI_RANK == 0:
            path_out_nc = self.get_temporary_file_path('out.nc')
        else:
            path_out_nc = None
        path_out_nc = MPI_COMM.bcast(path_out_nc)

        convert_to_esmf_format(path_out_nc, self.path_in_shp, name_uid)

        if MPI_RANK == 0:
            with self.nc_scope(path_out_nc) as ds:
                self.assertEqual(len(ds.variables), 6)
                self.assertEqual(
                    len(ds.variables[name_uid]),
                    len(GeometryManager(name_uid, path=self.path_in_shp)))