예제 #1
0
    def testConsistentResults(self):

        ulx = 94.2
        uly = 19.4
        lrx = 94.6
        lry = 19.1
        srs = SpatialReference()
        srs.ImportFromEPSG(4326)
        srs.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)

        env = Envelope()
        env.addPoint(ulx, uly, 0, srs)
        env.addPoint(lrx, lry, 0, srs)

        fpq = FootprintsQuery(FootprintsQueryTestCase._logger)
        fpq.addAoI(env)
        numScenes = 27
        fpq.setMaximumScenes(numScenes)
        fpScenes1 = fpq.getScenes()
        fpScenes2 = fpq.getScenes()

        self.assertEqual(len(fpScenes1), numScenes)
        self.assertEqual(len(fpScenes1), len(fpScenes2))

        for i in range(len(fpScenes1)):
            self.assertEqual(fpScenes1[i].fileName(), fpScenes2[i].fileName())
예제 #2
0
    def _extractVars(files, variables, envelope, outDir):

        srs = SpatialReference()
        srs.ImportFromEPSG(4326)
        srs.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)
        subFiles = []

        for f in files:

            geoFile = GeospatialImageFile(f, spatialReference=srs)
            subs = geoFile.getDataset().GetSubDatasets()
            foundVariables = []

            # Look for a variable name in the subdataset name.
            for sub in subs:

                datasetName = sub[0]
                var = datasetName.split(':')[2]

                if var in variables:

                    foundVariables.append(var)

                    # ---
                    # Copy the original file before operating on it, unless
                    # it already exists in the output directory.
                    # ---
                    name = os.path.basename(os.path.splitext(f)[0]) + \
                        '_' + \
                        var + \
                        '.nc'

                    workingCopy = os.path.join(outDir, name)

                    if not os.path.exists(workingCopy):

                        shutil.copyfile(f, workingCopy)

                        # Extract and clip the subdataset.
                        copyGeoFile = GeospatialImageFile(workingCopy, srs)
                        copyGeoFile.clipReproject(envelope, None, sub[0])

                    else:
                        copyGeoFile = GeospatialImageFile(workingCopy, srs)

                    subFiles.append(copyGeoFile.fileName())

                    # Stop, when all variables are found.
                    if len(foundVariables) == len(variables):
                        break

            # Are any variables missing?
            if len(foundVariables) != len(variables):

                missing = [v for v in variables if v not in foundVariables]
                msg = 'Variables not found in ' + str(f) + ': ' + str(missing)
                raise RuntimeError(msg)

        return subFiles
예제 #3
0
    def srs(self):

        srs = self._dataset.GetSpatialRef()

        if not srs:
            srs = SpatialReference()
            srs.ImportFromEPSG(4326)
            srs.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)

        return srs
예제 #4
0
def get_coords(geo_ref_points, spatial_ref: osr.SpatialReference):
    """
    Returns transformed coordinates in latitude and longitude from input
    reference points and spatial reference
    """
    if _NEWER_GDAL_CONVENTIONS:
        spatial_ref.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)

    t = osr.CoordinateTransformation(spatial_ref, spatial_ref.CloneGeogCS())

    def transform(p):
        lon, lat, z = t.TransformPoint(p["x"], p["y"])
        return {"lon": lon, "lat": lat}

    return {key: transform(p) for key, p in geo_ref_points.items()}
예제 #5
0
    def testSrs(self):

        # Build the test file.
        imageFile = self._createTestFile()

        # Check the srs.
        expectedSRS = SpatialReference()
        expectedSRS.ImportFromEPSG(4326)

        # https://github.com/OSGeo/gdal/blob/release/3.0/gdal/MIGRATION_GUIDE.TXT
        expectedSRS.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)

        self.assertTrue(imageFile.srs().IsSame(expectedSRS))

        # Delete the test file.
        os.remove(imageFile.fileName())
예제 #6
0
    def _createTestFile(self, createUTM=False):

        # ---
        # Set up a logger because serialization was causing loggers to point
        # to the SpatialReferences.
        # ---
        logger = logging.getLogger()
        logger.setLevel(logging.INFO)
        ch = logging.StreamHandler(sys.stdout)
        ch.setLevel(logging.INFO)
        logger.addHandler(ch)

        testFile = None

        if createUTM:

            testFile = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                    'gsenm_250m_eucl_dist_streams.tif')

            workingCopy = tempfile.mkstemp(suffix='.tif')[1]
            shutil.copyfile(testFile, workingCopy)

            srs = SpatialReference()
            srs.ImportFromEPSG(32612)

        else:
            testFile = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                    'TSURF.nc')

            workingCopy = tempfile.mkstemp(suffix='.nc')[1]
            shutil.copyfile(testFile, workingCopy)

            # ---
            # https://github.com/OSGeo/gdal/blob/release/3.0/gdal/
            # MIGRATION_GUIDE.TXT
            # ---
            srs = SpatialReference()
            srs.ImportFromEPSG(4326)
            srs.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)

        return GeospatialImageFile(workingCopy,
                                   spatialReference=srs,
                                   logger=logger)
예제 #7
0
    def testEnvelope(self):

        # Create a test image.
        imageFile = self._createTestFile()

        # Test envelope.
        srs = SpatialReference()
        srs.ImportFromEPSG(4326)
        # https://github.com/OSGeo/gdal/blob/release/3.0/gdal/MIGRATION_GUIDE.TXT
        srs.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)

        expectedEnvelope = Envelope()
        expectedEnvelope.addPoint(-125.3125000, 50.25, 0, srs)
        expectedEnvelope.addPoint(-65.9375000, 23.75, 0, srs)

        self.assertTrue(imageFile.envelope().Equals(expectedEnvelope))

        # Delete the test file.
        os.remove(imageFile.fileName())
예제 #8
0
    def testRun(self):

        ulx = -71
        uly = 40
        lrx = -70
        lry = 39
        srs = SpatialReference()
        srs.ImportFromEPSG(4326)
        srs.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)
        env = Envelope()
        env.addPoint(ulx, uly, 0, srs)
        env.addPoint(lrx, lry, 0, srs)

        dateRange = pandas.date_range('2010-11-11', '2011-01-12')
        outDir = tempfile.mkdtemp()

        files = MerraRequest.run(env, dateRange, MerraRequest.MONTHLY,
                                 ['m2t1nxslv'], ['QV2M', 'TS'], ['avg'],
                                 outDir)

        expected = [
            os.path.join(outDir, 'm2t1nxslv_avg_2010_month11_QV2M.nc'),
            os.path.join(outDir, 'm2t1nxslv_avg_2010_month11_TS.nc'),
            os.path.join(outDir, 'm2t1nxslv_avg_2010_month12_QV2M.nc'),
            os.path.join(outDir, 'm2t1nxslv_avg_2010_month12_TS.nc'),
            os.path.join(outDir, 'm2t1nxslv_avg_2011_month01_QV2M.nc'),
            os.path.join(outDir, 'm2t1nxslv_avg_2011_month01_TS.nc')
        ]

        self.assertEqual(len(expected), len(files))
        self.assertEqual(expected, sorted(files))

        clipped = GeospatialImageFile(os.path.join(
            outDir, 'm2t1nxslv_avg_2010_month11_QV2M.nc'),
                                      spatialReference=srs)

        self.assertTrue(clipped.envelope().Equal(env))

        # Delete the clipped files.
        for f in files:
            os.remove(f)
예제 #9
0
    def testInit(self):

        testFile1 = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                 'gsenm_250m_eucl_dist_streams.tif')

        workingCopy1 = tempfile.mkstemp(suffix='.tif')[1]
        shutil.copyfile(testFile1, workingCopy1)

        # Test valid SRS within the file.
        GeospatialImageFile(workingCopy1)

        # Test invalid SRS within the file.
        testFile2 = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                 'TSURF.nc')

        workingCopy2 = tempfile.mkstemp(suffix='.nc')[1]
        shutil.copyfile(testFile2, workingCopy2)

        with self.assertRaisesRegex(RuntimeError, 'Spatial reference for '):
            GeospatialImageFile(workingCopy2)

        # Test passing an SRS.
        srs = SpatialReference()
        srs.ImportFromEPSG(4326)
        imageFile = GeospatialImageFile(testFile2, spatialReference=srs)

        expectedSRS = SpatialReference()
        expectedSRS.ImportFromEPSG(4326)
        expectedSRS.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)
        self.assertTrue(imageFile.srs().IsSame(expectedSRS))

        # Test passing an invalid SRS.
        srs = SpatialReference()

        with self.assertRaisesRegex(RuntimeError, 'Spatial reference for '):
            imageFile = GeospatialImageFile(testFile2, spatialReference=srs)

        os.remove(workingCopy1)
        os.remove(workingCopy2)
예제 #10
0
    def testTransformTo(self):

        ulx = 626002.2463251714  # 94.19
        uly = 2145525.859757114  # 19.40
        lrx = 668316.2848759613  # 94.59
        lry = 2112661.4394026464  # 19.10

        srs = SpatialReference()
        srs.ImportFromEPSG(32646)

        env = Envelope()
        env.addPoint(ulx, uly, 0, srs)
        env.addPoint(lrx, lry, 0, srs)

        targetSrs = SpatialReference()
        targetSrs.ImportFromEPSG(4326)
        targetSrs.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)

        env.TransformTo(targetSrs)

        self.assertAlmostEqual(94.199, env.ulx(), places=2)
        self.assertAlmostEqual(19.400, env.uly(), places=2)
        self.assertAlmostEqual(94.599, env.lrx(), places=2)
        self.assertAlmostEqual(19.100, env.lry(), places=2)
예제 #11
0
    def testClipReproject(self):

        # ---
        # Test only clipping.
        # ---
        imageFile = self._createTestFile()

        # https://github.com/OSGeo/gdal/blob/release/3.0/gdal/MIGRATION_GUIDE.TXT
        srs = SpatialReference()
        srs.ImportFromEPSG(4326)
        srs.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)

        ulx = -120
        uly = 48
        lrx = -119
        lry = 47
        env = Envelope()
        env.addPoint(ulx, uly, 0, srs)
        env.addPoint(lrx, lry, 0, srs)

        imageFile.clipReproject(env)

        # Check the result.
        xform = imageFile.getDataset().GetGeoTransform()
        xScale = xform[1]
        yScale = xform[5]
        width = imageFile.getDataset().RasterXSize
        height = imageFile.getDataset().RasterYSize
        clippedUlx = xform[0]
        clippedUly = xform[3]
        clippedLrx = clippedUlx + width * xScale
        clippedLry = clippedUly + height * yScale

        self.assertAlmostEqual(clippedUlx, ulx)
        self.assertAlmostEqual(clippedUly, uly)
        self.assertAlmostEqual(clippedLrx, lrx)
        self.assertAlmostEqual(clippedLry, lry)

        os.remove(imageFile.fileName())

        # ---
        # Test only reprojection.
        # ---
        imageFile = self._createTestFile(createUTM=True)
        targetSRS = SpatialReference()
        targetSRS.ImportFromEPSG(4326)
        targetSRS.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)
        imageFile.clipReproject(None, targetSRS)

        # gdaltransform confirms these expected values.
        self.assertTrue(imageFile.srs().IsSame(targetSRS))

        self.assertAlmostEqual(imageFile.envelope().ulx(),
                               -112.5144042,
                               places=7)

        self.assertAlmostEqual(imageFile.envelope().uly(),
                               38.0308143,
                               places=7)

        self.assertAlmostEqual(imageFile.envelope().lrx(),
                               -110.8938763,
                               places=7)

        self.assertAlmostEqual(imageFile.envelope().lry(),
                               36.9934700,
                               places=7)

        os.remove(imageFile.fileName())

        # ---
        # Test clipping and reprojection.
        # ---
        imageFile = self._createTestFile()

        # Build the envelope.
        srs = SpatialReference()
        srs.ImportFromEPSG(4326)
        srs.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)

        ulx = -120
        uly = 48
        lrx = -119
        lry = 47
        env = Envelope()
        env.addPoint(ulx, uly, 0, srs)
        env.addPoint(lrx, lry, 0, srs)

        # Reprojection parameter
        targetSRS = SpatialReference()
        targetSRS.ImportFromEPSG(32611)

        # Clip, reproject and resample.
        imageFile.clipReproject(env, targetSRS)

        # Check the results.
        xform = imageFile.getDataset().GetGeoTransform()
        xScale = xform[1]
        yScale = xform[5]
        width = imageFile.getDataset().RasterXSize
        height = imageFile.getDataset().RasterYSize
        clippedUlx = xform[0]
        clippedUly = xform[3]
        clippedLrx = clippedUlx + width * xScale
        clippedLry = clippedUly + height * yScale

        self.assertTrue(imageFile.srs().IsSame(targetSRS))
        self.assertAlmostEqual(clippedUlx, 271930.435, places=3)
        self.assertAlmostEqual(clippedUly, 5318235.614, places=3)
        self.assertAlmostEqual(clippedLrx, 350812.125, places=3)
        self.assertAlmostEqual(clippedLry, 5209532.848, places=3)

        # Delete the test file.
        os.remove(imageFile.fileName())
예제 #12
0
def process_gml(input_filename,
                outName,
                field_mapping,
                icon_mapping,
                epsg=None):

    if epsg is not None:
        # Define the  projection system (EPSG 28992) sourcedata
        source_epsg = SpatialReference()
        source_epsg.ImportFromEPSG(epsg)
    else:
        source_epsg = SpatialReference()
        source_epsg.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER)
        source_epsg.ImportFromEPSG(4326)

    # Define the wgs84 system (EPSG 4326)
    epsg4326 = SpatialReference()
    epsg4326.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER)
    epsg4326.ImportFromEPSG(4326)

    outDriver = ogr.GetDriverByName('GPX')
    co_opts = ['GPX_USE_EXTENSIONS=yes', 'GPX_EXTENSIONS_NS="{opencpn}"']
    outDataSource = outDriver.CreateDataSource(outName, options=co_opts)
    outLayer = outDataSource.CreateLayer('waypoints',
                                         epsg4326,
                                         geom_type=ogr.wkbPoint)
    featureDefn = outLayer.GetLayerDefn()

    reader = ogr.Open(input_filename, update=0)

    print("GetLayerCount() = %d\n", reader.GetLayerCount())
    for iLayer in range(reader.GetLayerCount()):
        poLayer = reader.GetLayer(iLayer)
        # poLayer.SetSpatialFilter(epsg28992)

        line = "Layer %d: %s" % (iLayer + 1, poLayer.GetLayerDefn().GetName())
        print(line)

        line = "geocount %d: %s" % (iLayer + 1,
                                    poLayer.GetLayerDefn().GetGeomFieldCount())

        nGeomFieldCount = poLayer.GetLayerDefn().GetGeomFieldCount()
        if nGeomFieldCount > 0:
            line = line + " ("
            for iGeom in range(nGeomFieldCount):
                if iGeom > 0:
                    line = line + ", "
                poGFldDefn = poLayer.GetLayerDefn().GetGeomFieldDefn(iGeom)
                line = line + "%s" % ogr.GeometryTypeToName(
                    poGFldDefn.GetType())
            line = line + ")"

        if poLayer.GetLayerDefn().GetGeomType() != ogr.wkbUnknown:
            line = line + " (%s)" % ogr.GeometryTypeToName(
                poLayer.GetLayerDefn().GetGeomType())

        print(line)

        poFeature = poLayer.GetNextFeature()
        while poFeature is not None:
            poDefn = poFeature.GetDefnRef()
            print("OGRFeature(%s):%ld" %
                  (poDefn.GetName(), poFeature.GetFID()))
            outFeature = ogr.Feature(featureDefn)
            outFeature.SetFrom(poFeature)
            poDstGeometry: ogr.Geometry = outFeature.GetGeometryRef()
            if poDstGeometry is not None:
                print('geometry input:       ', poDstGeometry)
                srsReference: SpatialReference = poDstGeometry.GetSpatialReference(
                )
                if srsReference is None:
                    srsReference = source_epsg
                    print('no SpatialReference, using default')
                poCT = CoordinateTransformation(srsReference, epsg4326)
                if poCT is not None:
                    eErr = poDstGeometry.Transform(poCT)
                    print('geometry converted:   ', poDstGeometry)
                    if eErr != 0:
                        print(
                            "Failed to reproject feature %d (geometry probably out of source or destination SRS)."
                            % poFeature.GetFID())

            description = []

            for iField in range(poDefn.GetFieldCount()):
                poFDefn = poDefn.GetFieldDefn(iField)
                line = "  %s (%s) = " % (poFDefn.GetNameRef(),
                                         ogr.GetFieldTypeName(
                                             poFDefn.GetType()))
                if poFeature.IsFieldSet(
                        iField) and poFDefn.GetNameRef() in field_mapping:
                    try:
                        line = line + "%s" % (
                            poFeature.GetFieldAsString(iField))
                        map = field_mapping[poFDefn.GetNameRef()]
                        value = poFeature.GetFieldAsString(iField)

                        if map['isDescription']:
                            if len(
                                    value
                            ) > 0 and value != 'Niet toegewezen' and value != '#':
                                description.append(
                                    '%s : %s' %
                                    (map['dst'],
                                     poFeature.GetFieldAsString(iField)))
                        else:
                            outFeature.SetField(
                                map['dst'], poFeature.GetFieldAsString(iField))

                        # for the icons do a lookup
                        if map['dst'] == 'sym':
                            if value in icon_mapping:
                                outFeature.SetField(map['dst'],
                                                    str(icon_mapping[value]))
                            else:
                                outFeature.SetField(
                                    map['dst'],
                                    poFeature.GetFieldAsString(iField))
                                if value not in missing:
                                    missing.append(value)

                    except UnicodeEncodeError:
                        # For Python3 on non-UTF8 strings
                        print('pynonUT', errno)
                        # exit()
                        line = line + "%s" % (
                            poFeature.GetFieldAsBinary(iField))
                else:
                    line = line + "(null)"
                if debugging:
                    print(line)

            if len(description) > 0:
                outFeature.SetField('desc', '\n'.join(description))
            outLayer.CreateFeature(outFeature)
            # dereference the feature
            outFeature = None

            poFeature = poLayer.GetNextFeature()
    reader.Destroy()
    outDataSource.Destroy()
예제 #13
0
    def rough_convert_metres_to_spatial_ref_units(
            in_spatial_ref: osr.SpatialReference, extent: list,
            distance: float) -> float:
        """Convert from meters to the units of a given spatial_ref

        DO NOT USE THIS FOR ACCURATE DISTANCES. IT'S GOOD FOR A QUICK CALCULATION
        WHEN DISTANCE PRECISION ISN'T THAT IMPORTANT

        Args:
            in_spatial_ref (osr.SpatialReference): osr.SpatialRef to use as a reference 
            extent (list): The extent of our dataset: [min_x, min_y, max_x, max_y]. We use this to find the centerpoint
            distance (float): Distance in meters to convert

        Raises:
            VectorBaseException: [description]

        Returns:
            float: Distance in the spatial_ref's units
        """

        # If the input ref uses a projected coordinate system in meters then simply return the distance.
        # If it's projected but in some other units then throw an exception.
        # If it's in degrees then continue with the code below to convert it to metres.
        if in_spatial_ref.IsProjected() == 1:
            if in_spatial_ref.GetAttrValue('unit').lower() in [
                    'meter', 'metre', 'm'
            ]:
                return distance
            else:
                raise VectorBaseException(
                    'Unhandled projected coordinate system linear units: {}'.
                    format(in_spatial_ref.GetAttrValue('unit')))

        # Get the centroid of the Shapefile spatial extent
        extent_ring = ogr.Geometry(ogr.wkbLinearRing)
        extent_ring.AddPoint(extent[0], extent[2])
        extent_ring.AddPoint(extent[1], extent[2])
        extent_ring.AddPoint(extent[1], extent[3])
        extent_ring.AddPoint(extent[0], extent[3])
        extent_ring.AddPoint(extent[0], extent[2])
        extent_poly = ogr.Geometry(ogr.wkbPolygon)
        extent_poly.AddGeometry(extent_ring)
        extent_centroid = extent_poly.Centroid()

        # Go diagonally on the extent rectangle
        pt1_orig = Point(extent[0], extent[2])
        pt2_orig = Point(extent[1], extent[3])
        orig_dist = pt1_orig.distance(pt2_orig)

        # Determine the UTM zone by locating the centroid of the shapefile extent
        # Then get the transformation required to convert to the Shapefile to this UTM zone
        utm_epsg = get_utm_zone_epsg(extent_centroid.GetX())
        in_spatial_ref.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)

        out_spatial_ref = osr.SpatialReference()
        out_spatial_ref.ImportFromEPSG(int(utm_epsg))
        out_spatial_ref.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)

        VectorBase.log.debug(
            'Original spatial reference is : \n       {0} (AxisMappingStrategy:{1})'
            .format(*VectorBase.get_srs_debug(in_spatial_ref)))
        VectorBase.log.debug(
            'Transform spatial reference is : \n       {0} (AxisMappingStrategy:{1})'
            .format(*VectorBase.get_srs_debug(out_spatial_ref)))

        transform_forward = osr.CoordinateTransformation(
            in_spatial_ref, out_spatial_ref)

        pt1_ogr = VectorBase.shapely2ogr(pt1_orig, transform_forward)
        pt2_ogr = VectorBase.shapely2ogr(pt2_orig, transform_forward)

        pt1_proj = VectorBase.ogr2shapely(pt1_ogr)
        pt2_proj = VectorBase.ogr2shapely(pt2_ogr)

        proj_dist = pt1_proj.distance(pt2_proj)

        output_distance = (orig_dist / proj_dist) * distance

        VectorBase.log.info(
            '{}m distance converts to {:.10f} using UTM EPSG {}'.format(
                distance, output_distance, utm_epsg))

        if output_distance > 360:
            raise VectorBaseException(
                'Projection Error: \'{:,}\' is larger than the maximum allowed value'
                .format(output_distance))

        return output_distance