def test_reproject_geometry(self):
     world_poly_4326 = wkt_loader(
         "POLYGON((-77.12352759307811 38.969453670463515,-76.92302710479686 "
         "38.969453670463515,-76.92302710479686 38.834794725571776,-77.12352759307811 "
         "38.834794725571776,-77.12352759307811 38.969453670463515))")
     world_poly_3857 = photogram.reproject_geometry(world_poly_4326,
                                                    crs_defs.PROJ_4326,
                                                    crs_defs.PROJ_3857)
     new_poly_4326 = photogram.reproject_geometry(world_poly_3857,
                                                  crs_defs.PROJ_3857,
                                                  crs_defs.PROJ_4326)
     assert not world_poly_4326.almost_equals(world_poly_3857)
     assert world_poly_4326.almost_equals(new_poly_4326)
     print("reproject geometry test passed")
    def test_shift_points_by_meters_test(self):
        east_shift_meters = 1
        north_shift_meters = 1
        world_poly_4326 = wkt_loader(
            "POLYGON((-77.12352759307811 38.969453670463515,-76.92302710479686 "
            "38.969453670463515,-76.92302710479686 38.834794725571776,-77.12352759307811 "
            "38.834794725571776,-77.12352759307811 38.969453670463515))")
        world_poly_3857 = photogram.reproject_geometry(world_poly_4326,
                                                       crs_defs.PROJ_4326,
                                                       crs_defs.PROJ_3857)
        x_points_4326 = np.array(world_poly_4326.boundary.xy[0])
        y_points_4326 = np.array(world_poly_4326.boundary.xy[1])

        x_points_3857 = np.array(world_poly_3857.boundary.xy[0])
        y_points_3857 = np.array(world_poly_3857.boundary.xy[1])
        shifted_points_4326 = photogram.shift_points_by_meters(
            east_shift_meters, north_shift_meters, crs_defs.PROJ_4326,
            x_points_4326, y_points_4326)

        reprojected_shifted = proj_transform(crs_defs.PROJ_4326,
                                             crs_defs.PROJ_3857,
                                             shifted_points_4326[0],
                                             shifted_points_4326[1])

        assert shifted_points_4326[0][0] > x_points_4326[0]
        assert shifted_points_4326[1][0] > y_points_4326[0]

        north_shift = reprojected_shifted[0][0] - x_points_3857[0]
        east_shift = reprojected_shifted[1][0] - y_points_3857[0]

        assert np.isclose(east_shift_meters, east_shift)
        assert np.isclose(north_shift_meters, north_shift)
        print("shift by meters test passed")
def ortho_micasense_to_dem_4326():
    extent_4326 = photogrammetry_utils.reproject_geometry(extent_native, dem_native_projection, crs_defs.PROJ_4326)
    ortho_nx, ortho_ny = photogrammetry_utils.get_nx_ny_pixels_in_extent(extent_4326, gsd_lon, gsd_lat)
    print("making ortho of size: " + str(ortho_nx) + "x" + str(ortho_ny))

    output_fullpath = os.path.join(save_dir, "micasense_ortho_w_dem_4326.tif")
    ortho_tools.create_ortho_gtiff_image_world_to_sensor(micasense_image, ortho_nx, ortho_ny, extent_4326,
                                                         world_proj=crs_defs.PROJ_4326, dem=dem_image, nodata_val=0,
                                                         output_fname=output_fullpath)
    print("wrote orthorectified micasense image to " + output_fullpath)
def ortho_micasense_to_flat_earth_3857():
    extent_3857 = photogrammetry_utils.reproject_geometry(extent_native, dem_native_projection, crs_defs.PROJ_3857)
    elevation = np.nanmean(dem_image.dem_data)

    ortho_nx, ortho_ny = photogrammetry_utils.get_nx_ny_pixels_in_extent(extent_3857, gsd_m, gsd_m)
    print("making ortho of size: " + str(ortho_nx) + "x" + str(ortho_ny))

    const_elevation_dem = DemFactory.constant_elevation(elevation)
    output_fullpath = os.path.join(save_dir, "micasense_ortho_w_const_elevation_dem_3857.tif")

    ortho_tools.create_ortho_gtiff_image_world_to_sensor(micasense_image, ortho_nx, ortho_ny, extent_3857,
                                                         world_proj=crs_defs.PROJ_3857,
                                                         dem=const_elevation_dem, nodata_val=0,
                                                         output_fname=output_fullpath)
    print("wrote orthorectified micasense image to " + output_fullpath)