コード例 #1
0
def loadShapefile(shapePath, multispectralImage, withGeoToPixelConversion=True):
    # Get wktGeometries
    spatialReference, wktGeometries = geometry_store.load(shapePath)[:2]
    # Make sure spatialReferences match
    # if spatialReference != multispectralImage.getSpatialReference():
        # raise IOError('Spatial references do not match:\n\t%s\n\t%s' % (shapePath, multispectralImage.getPath()))
    # Define
    pattern_polygon = re.compile(r'POLYGON \(\((.*)\)\)')
    def convertFromWktGeometry(wktGeometry):
        # Parse geoPolygon
        string = pattern_polygon.match(wktGeometry).group(1)
        polygon = [[float(x) for x in line.split()] for line in string.split(',')]
        # Make sure there are exactly four points
        # if len(polygon) != 4:
            # raise IOError('Each polygon must have exactly four points: %s' % shapePath)
        # Convert
        if withGeoToPixelConversion:
            polygon = multispectralImage.convertGeoLocationsToPixelLocations(polygon)
        # Unpack
        (left1, top1), (right1, top2), (right2, bottom1), (left2, bottom2) = polygon[:4]
        left = min(left1, left2)
        right = max(right1, right2)
        top = min(top1, top2)
        bottom = max(bottom1, bottom2)
        # Return
        return min(left, right), min(top, bottom), max(left, right), max(top, bottom)
    # Return
    return map(convertFromWktGeometry, wktGeometries)
コード例 #2
0
    def test(self):
        'Run tests'

        print('Save and load a SHP file without attributes')
        path = self.getPath('.shp')
        geometry_store.save(path, geometry_store.proj4LL, shapelyGeometries)
        result = geometry_store.load(path)
        self.assertEqual(result[0].strip(), geometry_store.proj4LL)
        self.assertEqual(len(result[1]), len(shapelyGeometries))

        print('Save and load a SHP file with attributes')
        path = self.getPath('.shp')
        geometry_store.save(
            path, geometry_store.proj4LL, shapelyGeometries, fieldPacks,
            fieldDefinitions)
        result = geometry_store.load(path)
        self.assertEqual(len(result[2]), len(fieldPacks))
        for shapelyGeometry, fieldPack in zip(result[1], result[2]):
            print()
            for fieldValue, (
                fieldName, fieldType
            ) in zip(fieldPack, result[3]):
                print('%s = %s' % (fieldName, fieldValue))
            print(shapelyGeometry)

        print('Save a SHP file with attributes with different targetProj4')
        path = self.getPath('.shp')
        geometry_store.save(
            path, geometry_store.proj4LL, shapelyGeometries, fieldPacks,
            fieldDefinitions, targetProj4=geometry_store.proj4SM)
        result = geometry_store.load(path)
        self.assertNotEqual(result[0].strip(), geometry_store.proj4LL)

        print('Load a SHP file with attributes with different targetProj4')
        path = self.getPath('.shp')
        geometry_store.save(
            path, geometry_store.proj4LL, shapelyGeometries, fieldPacks,
            fieldDefinitions)
        result = geometry_store.load(path, targetProj4=geometry_store.proj4SM)
        self.assertNotEqual(result[0].strip(), geometry_store.proj4LL)

        print('Save and load a ZIP file without attributes using save')
        path = self.getPath('.shp.zip')
        geometry_store.save(path, geometry_store.proj4LL, shapelyGeometries)
        result = geometry_store.load(path)
        self.assertEqual(result[0].strip(), geometry_store.proj4LL)
        self.assertEqual(len(result[1]), len(shapelyGeometries))

        print('Save and load a ZIP file with attributes using save')
        path = self.getPath('.shp.zip')
        geometry_store.save(
            path, geometry_store.proj4LL, shapelyGeometries, fieldPacks,
            fieldDefinitions)
        result = geometry_store.load(path)
        self.assertEqual(len(result[2]), len(fieldPacks))

        print('Test saving and loading ZIP files of point coordinates')
        path = self.getPath('.shp.zip')
        geometry_store.save_points(
            path, geometry_store.proj4LL, [(0, 0)], fieldPacks,
            fieldDefinitions)
        result = geometry_store.load_points(path)
        self.assertEqual(result[1], [(0, 0)])

        print('Test get_transform_point')
        transform_point0 = geometry_store.get_transform_point(
            geometry_store.proj4LL, geometry_store.proj4LL)
        transform_point1 = geometry_store.get_transform_point(
            geometry_store.proj4LL, geometry_store.proj4SM)
        self.assertNotEqual(transform_point0(0, 0), transform_point1(0, 0))

        print('Test get_transform_geometry')
        transform_geometry = geometry_store.get_transform_geometry(
            geometry_store.proj4LL, geometry_store.proj4SM)
        self.assertEqual(type(transform_geometry(
            geometry.Point(0, 0))), type(geometry.Point(0, 0)))
        self.assertEqual(type(transform_geometry(ogr.CreateGeometryFromWkt(
            'POINT (0 0)'))), type(ogr.CreateGeometryFromWkt('POINT (0 0)')))

        print('Test get_coordinateTransformation')
        geometry_store.get_coordinateTransformation(
            geometry_store.proj4LL, geometry_store.proj4SM)

        print('Test get_spatialReference')
        geometry_store.get_spatialReference(geometry_store.proj4LL)
        with self.assertRaises(geometry_store.GeometryError):
            geometry_store.get_spatialReference('')

        print('Test get_geometryType')
        geometry_store.get_geometryType(shapelyGeometries)

        print('Test save() when a fieldPack has fewer fields than definitions')
        with self.assertRaises(geometry_store.GeometryError):
            path = self.getPath('.shp')
            geometry_store.save(
                path, geometry_store.proj4LL, shapelyGeometries,
                [x[1:] for x in fieldPacks], fieldDefinitions)

        print('Test save() when a fieldPack has more fields than definitions')
        with self.assertRaises(geometry_store.GeometryError):
            path = self.getPath('.shp')
            geometry_store.save(
                path, geometry_store.proj4LL, shapelyGeometries,
                [x * 2 for x in fieldPacks], fieldDefinitions)

        print('Test save() when the driverName is unrecognized')
        with self.assertRaises(geometry_store.GeometryError):
            path = self.getPath('.shp')
            geometry_store.save(
                path, geometry_store.proj4LL, shapelyGeometries, driverName='')

        print('Test load() when format is unrecognized')
        with self.assertRaises(geometry_store.GeometryError):
            path = self.getPath('')
            geometry_store.load(path)
コード例 #3
0
    def test(self):
        'Run tests'

        print('Save and load a SHP file without attributes')
        path = self.getPath('.shp')
        geometry_store.save(path, geometry_store.proj4LL, shapelyGeometries)
        result = geometry_store.load(path)
        self.assertEqual(result[0].strip(), geometry_store.proj4LL)
        self.assertEqual(len(result[1]), len(shapelyGeometries))

        print('Save and load a SHP file with attributes')
        path = self.getPath('.shp')
        geometry_store.save(path, geometry_store.proj4LL, shapelyGeometries,
                            fieldPacks, fieldDefinitions)
        result = geometry_store.load(path)
        self.assertEqual(len(result[2]), len(fieldPacks))
        for shapelyGeometry, fieldPack in zip(result[1], result[2]):
            print()
            for fieldValue, (fieldName,
                             fieldType) in zip(fieldPack, result[3]):
                print('%s = %s' % (fieldName, fieldValue))
            print(shapelyGeometry)

        print('Save a SHP file with attributes with different targetProj4')
        path = self.getPath('.shp')
        geometry_store.save(path,
                            geometry_store.proj4LL,
                            shapelyGeometries,
                            fieldPacks,
                            fieldDefinitions,
                            targetProj4=geometry_store.proj4SM)
        result = geometry_store.load(path)
        self.assertNotEqual(result[0].strip(), geometry_store.proj4LL)

        print('Load a SHP file with attributes with different targetProj4')
        path = self.getPath('.shp')
        geometry_store.save(path, geometry_store.proj4LL, shapelyGeometries,
                            fieldPacks, fieldDefinitions)
        result = geometry_store.load(path, targetProj4=geometry_store.proj4SM)
        self.assertNotEqual(result[0].strip(), geometry_store.proj4LL)

        print('Save and load a ZIP file without attributes using save')
        path = self.getPath('.shp.zip')
        geometry_store.save(path, geometry_store.proj4LL, shapelyGeometries)
        result = geometry_store.load(path)
        self.assertEqual(result[0].strip(), geometry_store.proj4LL)
        self.assertEqual(len(result[1]), len(shapelyGeometries))

        print('Save and load a ZIP file with attributes using save')
        path = self.getPath('.shp.zip')
        geometry_store.save(path, geometry_store.proj4LL, shapelyGeometries,
                            fieldPacks, fieldDefinitions)
        result = geometry_store.load(path)
        self.assertEqual(len(result[2]), len(fieldPacks))

        print('Test saving and loading ZIP files of point coordinates')
        path = self.getPath('.shp.zip')
        geometry_store.save_points(path, geometry_store.proj4LL, [(0, 0)],
                                   fieldPacks, fieldDefinitions)
        result = geometry_store.load_points(path)
        self.assertEqual(result[1], [(0, 0)])

        print('Test get_transform_point')
        transform_point0 = geometry_store.get_transform_point(
            geometry_store.proj4LL, geometry_store.proj4LL)
        transform_point1 = geometry_store.get_transform_point(
            geometry_store.proj4LL, geometry_store.proj4SM)
        self.assertNotEqual(transform_point0(0, 0), transform_point1(0, 0))

        print('Test get_transform_geometry')
        transform_geometry = geometry_store.get_transform_geometry(
            geometry_store.proj4LL, geometry_store.proj4SM)
        self.assertEqual(type(transform_geometry(geometry.Point(0, 0))),
                         type(geometry.Point(0, 0)))
        self.assertEqual(
            type(transform_geometry(ogr.CreateGeometryFromWkt('POINT (0 0)'))),
            type(ogr.CreateGeometryFromWkt('POINT (0 0)')))

        print('Test get_coordinateTransformation')
        geometry_store.get_coordinateTransformation(geometry_store.proj4LL,
                                                    geometry_store.proj4SM)

        print('Test get_spatialReference')
        geometry_store.get_spatialReference(geometry_store.proj4LL)
        with self.assertRaises(geometry_store.GeometryError):
            geometry_store.get_spatialReference('')

        print('Test get_geometryType')
        geometry_store.get_geometryType(shapelyGeometries)

        print('Test save() when a fieldPack has fewer fields than definitions')
        with self.assertRaises(geometry_store.GeometryError):
            path = self.getPath('.shp')
            geometry_store.save(path, geometry_store.proj4LL,
                                shapelyGeometries, [x[1:] for x in fieldPacks],
                                fieldDefinitions)

        print('Test save() when a fieldPack has more fields than definitions')
        with self.assertRaises(geometry_store.GeometryError):
            path = self.getPath('.shp')
            geometry_store.save(path, geometry_store.proj4LL,
                                shapelyGeometries, [x * 2 for x in fieldPacks],
                                fieldDefinitions)

        print('Test save() when the driverName is unrecognized')
        with self.assertRaises(geometry_store.GeometryError):
            path = self.getPath('.shp')
            geometry_store.save(path,
                                geometry_store.proj4LL,
                                shapelyGeometries,
                                driverName='')

        print('Test load() when format is unrecognized')
        with self.assertRaises(geometry_store.GeometryError):
            path = self.getPath('')
            geometry_store.load(path)
コード例 #4
0
    def test(self):
        "Run tests"

        print "Save and load a SHP file without attributes"
        path = self.getPath(".shp")
        geometry_store.save(path, geometry_store.proj4LL, shapelyGeometries)
        result = geometry_store.load(path)
        self.assertEqual(result[0].strip(), geometry_store.proj4LL)
        self.assertEqual(len(result[1]), len(shapelyGeometries))

        print "Save and load a SHP file with attributes"
        path = self.getPath(".shp")
        geometry_store.save(path, geometry_store.proj4LL, shapelyGeometries, fieldPacks, fieldDefinitions)
        result = geometry_store.load(path)
        self.assertEqual(len(result[2]), len(fieldPacks))
        for shapelyGeometry, fieldPack in itertools.izip(result[1], result[2]):
            print
            for fieldValue, (fieldName, fieldType) in itertools.izip(fieldPack, result[3]):
                print "%s = %s" % (fieldName, fieldValue)
            print shapelyGeometry

        print "Save a SHP file with attributes with different targetProj4"
        path = self.getPath(".shp")
        geometry_store.save(
            path,
            geometry_store.proj4LL,
            shapelyGeometries,
            fieldPacks,
            fieldDefinitions,
            targetProj4=geometry_store.proj4SM,
        )
        result = geometry_store.load(path)
        self.assertNotEqual(result[0].strip(), geometry_store.proj4LL)

        print "Load a SHP file with attributes with different targetProj4"
        path = self.getPath(".shp")
        geometry_store.save(path, geometry_store.proj4LL, shapelyGeometries, fieldPacks, fieldDefinitions)
        result = geometry_store.load(path, targetProj4=geometry_store.proj4SM)
        self.assertNotEqual(result[0].strip(), geometry_store.proj4LL)

        print "Save and load a ZIP file without attributes using save"
        path = self.getPath(".shp.zip")
        geometry_store.save(path, geometry_store.proj4LL, shapelyGeometries)
        result = geometry_store.load(path)
        self.assertEqual(result[0].strip(), geometry_store.proj4LL)
        self.assertEqual(len(result[1]), len(shapelyGeometries))

        print "Save and load a ZIP file with attributes using save"
        path = self.getPath(".shp.zip")
        geometry_store.save(path, geometry_store.proj4LL, shapelyGeometries, fieldPacks, fieldDefinitions)
        result = geometry_store.load(path)
        self.assertEqual(len(result[2]), len(fieldPacks))

        print "Test saving and loading ZIP files of point coordinates"
        path = self.getPath(".shp.zip")
        geometry_store.save_points(path, geometry_store.proj4LL, [(0, 0)], fieldPacks, fieldDefinitions)
        result = geometry_store.load_points(path)
        self.assertEqual(result[1], [(0, 0)])

        print "Test get_transform_point"
        transform_point0 = geometry_store.get_transform_point(geometry_store.proj4LL, geometry_store.proj4LL)
        transform_point1 = geometry_store.get_transform_point(geometry_store.proj4LL, geometry_store.proj4SM)
        self.assertNotEqual(transform_point0(0, 0), transform_point1(0, 0))

        print "Test get_transform_geometry"
        transform_geometry = geometry_store.get_transform_geometry(geometry_store.proj4LL, geometry_store.proj4SM)
        self.assertEqual(type(transform_geometry(geometry.Point(0, 0))), type(geometry.Point(0, 0)))
        self.assertEqual(
            type(transform_geometry(ogr.CreateGeometryFromWkt("POINT (0 0)"))),
            type(ogr.CreateGeometryFromWkt("POINT (0 0)")),
        )

        print "Test get_coordinateTransformation"
        geometry_store.get_coordinateTransformation(geometry_store.proj4LL, geometry_store.proj4SM)

        print "Test get_spatialReference"
        geometry_store.get_spatialReference(geometry_store.proj4LL)
        with self.assertRaises(geometry_store.GeometryError):
            geometry_store.get_spatialReference("")

        print "Test get_geometryType"
        geometry_store.get_geometryType(shapelyGeometries)

        print "Test save() when a fieldPack has fewer fields than definitions"
        with self.assertRaises(geometry_store.GeometryError):
            path = self.getPath(".shp")
            geometry_store.save(
                path, geometry_store.proj4LL, shapelyGeometries, [x[1:] for x in fieldPacks], fieldDefinitions
            )

        print "Test save() when a fieldPack has more fields than definitions"
        with self.assertRaises(geometry_store.GeometryError):
            path = self.getPath(".shp")
            geometry_store.save(
                path, geometry_store.proj4LL, shapelyGeometries, [x * 2 for x in fieldPacks], fieldDefinitions
            )

        print "Test save() when the driverName is unrecognized"
        with self.assertRaises(geometry_store.GeometryError):
            path = self.getPath(".shp")
            geometry_store.save(path, geometry_store.proj4LL, shapelyGeometries, driverName="")

        print "Test load() when format is unrecognized"
        with self.assertRaises(geometry_store.GeometryError):
            path = self.getPath("")
            geometry_store.load(path)