Skip to content

Instantly share code, notes, and snippets.

@refactor
Last active January 27, 2019 01:49
Show Gist options
  • Save refactor/614d79911296cf42ca7d021536676ae2 to your computer and use it in GitHub Desktop.
Save refactor/614d79911296cf42ca7d021536676ae2 to your computer and use it in GitHub Desktop.

Revisions

  1. refactor revised this gist Jan 27, 2019. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions autowarp_test.c
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,9 @@
    #include <ogr_srs_api.h>
    #include <gdalwarper.h>

    /*
    * https://www.gdal.org/warptut.html
    */
    int main()
    {
    GDALAllRegister();
  2. refactor revised this gist Jan 20, 2019. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions autowarp_test.c
    Original file line number Diff line number Diff line change
    @@ -22,13 +22,15 @@ int main()
    0.0,
    NULL);

    // save as xml file, just for edit the VRT as a xml file,
    GDALDriverH hDriver = GDALGetDatasetDriver(hDstDS);
    GDALDatasetH hOutDS = GDALCreateCopy(hDriver,
    "my-dem.vrt", hDstDS,
    GDALDatasetH hOutDS = GDALCreateCopy(hDriver, "my-dem.vrt", hDstDS,
    FALSE, NULL, NULL, NULL);

    GDALClose(hOutDS);

    // do the work with hDstDS, as the warp VRT dataset,
    // ...

    CPLFree(pszDstWKT);
    GDALClose( hDstDS );
    GDALClose( hSrcDS );
  3. refactor revised this gist Jan 20, 2019. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions autowarp_test.c
    Original file line number Diff line number Diff line change
    @@ -13,6 +13,7 @@ int main()
    OSRImportFromEPSG(dstSRS, 3857);
    char *pszDstWKT = NULL;
    OSRExportToWkt(dstSRS, &pszDstWKT);
    OSRDestroySpatialReference(dstSRS);
    printf("pass...\r\n%s\r\n", pszDstWKT);

    GDALDatasetH hDstDS = GDALAutoCreateWarpedVRT(hSrcDS,
  4. refactor revised this gist Jan 20, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion autowarp_test.c
    Original file line number Diff line number Diff line change
    @@ -4,9 +4,9 @@

    int main()
    {
    // Open input and output files.
    GDALAllRegister();

    // Open input and output files.
    GDALDatasetH hSrcDS = GDALOpenShared( "cq-dem.tif", GA_ReadOnly );

    OGRSpatialReferenceH dstSRS = OSRNewSpatialReference(NULL);
  5. refactor revised this gist Jan 20, 2019. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions autowarp_test.c
    Original file line number Diff line number Diff line change
    @@ -21,10 +21,10 @@ int main()
    0.0,
    NULL);

    // GDALGetMetadata(hDstDS,
    GDALDriverH hDriver = GDALGetDatasetDriver(hDstDS);
    GDALDatasetH hOutDS = GDALCreateCopy(hDriver, "my-dem.vrt",
    hDstDS, FALSE, NULL, NULL, NULL);
    GDALDatasetH hOutDS = GDALCreateCopy(hDriver,
    "my-dem.vrt", hDstDS,
    FALSE, NULL, NULL, NULL);

    GDALClose(hOutDS);

  6. refactor revised this gist Jan 20, 2019. No changes.
  7. refactor renamed this gist Jan 20, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  8. refactor revised this gist Jan 20, 2019. 1 changed file with 24 additions and 27 deletions.
    51 changes: 24 additions & 27 deletions warp_test.c
    Original file line number Diff line number Diff line change
    @@ -1,39 +1,36 @@
    #include <cpl_conv.h>
    #include <ogr_srs_api.h>
    #include <gdalwarper.h>

    int main()
    {
    // Open input and output files.
    GDALAllRegister();

    GDALDatasetH hSrcDS = GDALOpen( "cq-dem.tif", GA_ReadOnly );
    GDALDatasetH hDstDS = GDALOpen( "out.tif", GA_Update );

    // Setup warp options.
    GDALWarpOptions *psWarpOptions = GDALCreateWarpOptions();
    psWarpOptions->hSrcDS = hSrcDS;
    psWarpOptions->hDstDS = hDstDS;
    psWarpOptions->nBandCount = 1;
    psWarpOptions->panSrcBands = (int *) CPLMalloc(sizeof(int) * psWarpOptions->nBandCount );
    psWarpOptions->panSrcBands[0] = 1;
    psWarpOptions->panDstBands = (int *) CPLMalloc(sizeof(int) * psWarpOptions->nBandCount );
    psWarpOptions->panDstBands[0] = 1;
    psWarpOptions->pfnProgress = GDALTermProgress;
    // Establish reprojection transformer.
    psWarpOptions->pTransformerArg =
    GDALCreateGenImgProjTransformer( hSrcDS, GDALGetProjectionRef(hSrcDS),
    hDstDS, GDALGetProjectionRef(hDstDS),
    FALSE, 0.0, 1 );
    psWarpOptions->pfnTransformer = GDALGenImgProjTransform;
    // Initialize and execute the warp operation.
    GDALWarpOperationH hOperation = GDALCreateWarpOperation( psWarpOptions );
    GDALChunkAndWarpImage(hOperation, 0, 0,
    GDALGetRasterXSize( hDstDS ),
    GDALGetRasterYSize( hDstDS ) );

    GDALDestroyGenImgProjTransformer( psWarpOptions->pTransformerArg );
    GDALDestroyWarpOptions( psWarpOptions );
    GDALDatasetH hSrcDS = GDALOpenShared( "cq-dem.tif", GA_ReadOnly );

    OGRSpatialReferenceH dstSRS = OSRNewSpatialReference(NULL);
    OSRImportFromEPSG(dstSRS, 3857);
    char *pszDstWKT = NULL;
    OSRExportToWkt(dstSRS, &pszDstWKT);
    printf("pass...\r\n%s\r\n", pszDstWKT);

    GDALDatasetH hDstDS = GDALAutoCreateWarpedVRT(hSrcDS,
    GDALGetProjectionRef(hSrcDS), pszDstWKT,
    GRA_NearestNeighbour,
    0.0,
    NULL);

    // GDALGetMetadata(hDstDS,
    GDALDriverH hDriver = GDALGetDatasetDriver(hDstDS);
    GDALDatasetH hOutDS = GDALCreateCopy(hDriver, "my-dem.vrt",
    hDstDS, FALSE, NULL, NULL, NULL);

    GDALClose(hOutDS);

    CPLFree(pszDstWKT);
    GDALClose( hDstDS );
    GDALClose( hSrcDS );

    return 0;
    }
  9. refactor created this gist Jan 20, 2019.
    39 changes: 39 additions & 0 deletions warp_test.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    #include <cpl_conv.h>
    #include <gdalwarper.h>

    int main()
    {
    // Open input and output files.
    GDALAllRegister();

    GDALDatasetH hSrcDS = GDALOpen( "cq-dem.tif", GA_ReadOnly );
    GDALDatasetH hDstDS = GDALOpen( "out.tif", GA_Update );

    // Setup warp options.
    GDALWarpOptions *psWarpOptions = GDALCreateWarpOptions();
    psWarpOptions->hSrcDS = hSrcDS;
    psWarpOptions->hDstDS = hDstDS;
    psWarpOptions->nBandCount = 1;
    psWarpOptions->panSrcBands = (int *) CPLMalloc(sizeof(int) * psWarpOptions->nBandCount );
    psWarpOptions->panSrcBands[0] = 1;
    psWarpOptions->panDstBands = (int *) CPLMalloc(sizeof(int) * psWarpOptions->nBandCount );
    psWarpOptions->panDstBands[0] = 1;
    psWarpOptions->pfnProgress = GDALTermProgress;
    // Establish reprojection transformer.
    psWarpOptions->pTransformerArg =
    GDALCreateGenImgProjTransformer( hSrcDS, GDALGetProjectionRef(hSrcDS),
    hDstDS, GDALGetProjectionRef(hDstDS),
    FALSE, 0.0, 1 );
    psWarpOptions->pfnTransformer = GDALGenImgProjTransform;
    // Initialize and execute the warp operation.
    GDALWarpOperationH hOperation = GDALCreateWarpOperation( psWarpOptions );
    GDALChunkAndWarpImage(hOperation, 0, 0,
    GDALGetRasterXSize( hDstDS ),
    GDALGetRasterYSize( hDstDS ) );

    GDALDestroyGenImgProjTransformer( psWarpOptions->pTransformerArg );
    GDALDestroyWarpOptions( psWarpOptions );
    GDALClose( hDstDS );
    GDALClose( hSrcDS );
    return 0;
    }