Пример #1
0
/**********************************************************************
 *                          DumpCoordsys()
 *
 * Open a .TAB file and dump coordsys info
 **********************************************************************/
static int DumpCoordsys(const char *pszFname)
{
    IMapInfoFile  *poFile;
    double dXMin, dYMin, dXMax, dYMax;

    /*---------------------------------------------------------------------
     * Try to open source file
     *--------------------------------------------------------------------*/
    if ((poFile = IMapInfoFile::SmartOpen(pszFname)) == NULL)
    {
        printf("Failed to open %s\n", pszFname);
        return -1;
    }

    OGRSpatialReference *poSRS = poFile->GetSpatialRef();
    char *pszCoordSys = MITABSpatialRef2CoordSys(poSRS);
    char *pszProjString=NULL;

    printf("CoordSys %s\n", pszCoordSys?pszCoordSys:"(null)");

    if (poFile->GetBounds(dXMin, dYMin, dXMax, dYMax) == 0)
    {
        printf("  Proj. Bounds (%.15g %.15g) (%.15g %.15g)\n", dXMin, dYMin, dXMax, dYMax);
        printf("    dX dY = %.15g %.15g\n", dXMax - dXMin, dYMax - dYMin);

    }
    else
    {
        printf("  Projection Bounds not available!\n");
    }

    OGREnvelope oEnv;
    if (poFile->GetExtent(&oEnv, TRUE) == 0)
    {
        printf("  Data Extents (%.15g %.15g) (%.15g %.15g)\n", oEnv.MinX, oEnv.MinY, oEnv.MaxX, oEnv.MaxY);

    }
    else
    {
        printf("  Data Extents not available!\n");
    }

    if (poSRS)
    {

//        poSRS->exportToWkt(&pszProjString);
//        printf("  WKT SRS = %s\n", pszProjString);
//        CPLFree(pszProjString);

        poSRS->exportToProj4(&pszProjString);
        printf("  PROJ4 SRS = %s\n", pszProjString);

        // Write bounds to a file and launch 'proj' to convert them to LAT/LON
        FILE *fpOut;
        if (pszProjString &&
            (fpOut = fopen("/tmp/tttbounds.txt", "w")))
        {
            fprintf(fpOut, "%.15g %.15g\n", dXMin, dYMin);
            fprintf(fpOut, "%.15g %.15g\n", dXMax, dYMax);
            fclose(fpOut);

            fflush(stdout);

            system(CPLSPrintf("proj -I %s /tmp/tttbounds.txt", pszProjString));
        }

    }

    /*---------------------------------------------------------------------
     * Cleanup and exit.
     *--------------------------------------------------------------------*/
    CPLFree(pszProjString);
    poFile->Close();

    delete poFile;

    return 0;
}
Пример #2
0
/**********************************************************************
 *                          DumpCoordsysStruct()
 *
 * Open a .TAB file and dump coordsys info in a format usable to build
 * C array of MapInfoBoundsInfo[]
 **********************************************************************/
static int DumpCoordsysStruct(const char *pszFname)
{
    IMapInfoFile  *poFile;
    double dXMin, dYMin, dXMax, dYMax;

    /*---------------------------------------------------------------------
     * Try to open source file
     *--------------------------------------------------------------------*/
    if ((poFile = IMapInfoFile::SmartOpen(pszFname)) == NULL)
    {
        printf("Failed to open %s\n", pszFname);
        return -1;
    }

    TABProjInfo sProjInfo;

    if (poFile->GetProjInfo(&sProjInfo) != 0)
    {
        printf("Cannot fetch TABProjInfo from %s\n", pszFname);
        return -1;
    }

    if (sProjInfo.nProjId == 0)
    {
        printf("Nonearth coordsys in %s\n", pszFname);
        return 0;
    }

    if (poFile->GetBounds(dXMin, dYMin, dXMax, dYMax) == 0)
    {
        printf("{{%d, %d, %d, "
               "{%.15g,%.15g,%.15g,%.15g,%.15g,%.15g}, "
               "%d,%.15g,%.15g,%.15g, "
               "{%.15g,%.15g,%.15g,%.15g,%.15g}}, ", 
               sProjInfo.nProjId,
               sProjInfo.nEllipsoidId,
               sProjInfo.nUnitsId,
               sProjInfo.adProjParams[0],
               sProjInfo.adProjParams[1],
               sProjInfo.adProjParams[2],
               sProjInfo.adProjParams[3],
               sProjInfo.adProjParams[4],
               sProjInfo.adProjParams[5],
               sProjInfo.nDatumId,
               sProjInfo.dDatumShiftX,
               sProjInfo.dDatumShiftY,
               sProjInfo.dDatumShiftZ,
               sProjInfo.adDatumParams[0],
               sProjInfo.adDatumParams[1],
               sProjInfo.adDatumParams[2],
               sProjInfo.adDatumParams[3],
               sProjInfo.adDatumParams[4] );
        

        printf(" %.15g, %.15g, %.15g, %.15g},\n", dXMin, dYMin, dXMax, dYMax);

    }
    else
    {
        printf("  Bounds struct cannot be generated!\n");
    }


    /*---------------------------------------------------------------------
     * Cleanup and exit.
     *--------------------------------------------------------------------*/
    poFile->Close();

    delete poFile;

    return 0;
}
Пример #3
0
/**********************************************************************
 *                          Tab2Tab()
 *
 * Copy features from source dataset to a new dataset
 **********************************************************************/
static int Tab2Tab(const char *pszSrcFname, const char *pszDstFname,
                   int nMaxFeatures, 
                   GBool bQuickSpatialIndexMode, GBool bOptSpatialIndexMode)
{
    IMapInfoFile *poSrcFile = NULL, *poDstFile = NULL;
    int      nFeatureId, iField, numFeatures=0;
    TABFeature *poFeature;
    double dXMin, dYMin, dXMax, dYMax;

    /*---------------------------------------------------------------------
     * If there is a "micdsys.txt" in current directory then load it
     *--------------------------------------------------------------------*/
    MITABLoadCoordSysTable("micdsys.txt");

    /*---------------------------------------------------------------------
     * Try to open source file
     *--------------------------------------------------------------------*/
    if ((poSrcFile = IMapInfoFile::SmartOpen(pszSrcFname)) == NULL)
    {
        printf("Failed to open %s\n", pszSrcFname);
        return -1;
    }

    OGRFeatureDefn *poDefn = poSrcFile->GetLayerDefn();

    /*---------------------------------------------------------------------
     * The extension of the output filename tells us if we should create
     * a MIF or a TAB file for output.
     *--------------------------------------------------------------------*/
    if (EQUAL(".mif", pszDstFname + strlen(pszDstFname)-4) ||
        EQUAL(".mid", pszDstFname + strlen(pszDstFname)-4) )
    {
        // Create a MIF file
        poDstFile = new MIFFile;
    }
    else
    {
        /*-----------------------------------------------------------------
         * Create a TAB dataset.
         * Find out if the file contains at least 1 unique field... if so we
         * will create a TABView instead of a TABFile
         *----------------------------------------------------------------*/
        GBool    bFoundUniqueField = FALSE;
        for(iField=0; iField< poDefn->GetFieldCount(); iField++)
        {
            if (poSrcFile->IsFieldUnique(iField))
                bFoundUniqueField = TRUE;
        }

        if (bFoundUniqueField)
            poDstFile = new TABView;
        else
            poDstFile = new TABFile;
    }

    /*---------------------------------------------------------------------
     * Try to open destination file
     *--------------------------------------------------------------------*/
    if (poDstFile->Open(pszDstFname, "wb") != 0)
    {
        printf("Failed to open %s\n", pszDstFname);
        poSrcFile->Close();
        delete poSrcFile;
        delete poDstFile;
        return -1;
    }

    if ( (bQuickSpatialIndexMode && 
          poDstFile->SetQuickSpatialIndexMode(TRUE) != 0) ||
         (bOptSpatialIndexMode && 
          poDstFile->SetQuickSpatialIndexMode(FALSE) != 0) )
    {
        printf("Failed setting Quick Spatial Index Mode (-q|-o) on %s\n", pszDstFname);
        poSrcFile->Close();
        delete poSrcFile;
        poDstFile->Close();
        delete poDstFile;
        return -1;
    }

    // Pass Proj. info directly
    // TABProjInfo sProjInfo;
    // if (poSrcFile->GetProjInfo(&sProjInfo) == 0)
    //     poDstFile->SetProjInfo(&sProjInfo);

    OGRSpatialReference *poSR;

    poSR = poSrcFile->GetSpatialRef();
    if( poSR != NULL )
    {
        poDstFile->SetSpatialRef( poSR );
    }

    //  Set bounds (must be done after setting spatialref)
    if (poSrcFile->GetBounds(dXMin, dYMin, dXMax, dYMax) == 0)
        poDstFile->SetBounds(dXMin, dYMin, dXMax, dYMax);

    /*---------------------------------------------------------------------
     * Pass compplete fields information
     *--------------------------------------------------------------------*/
    for(iField=0; iField< poDefn->GetFieldCount(); iField++)
    {
        OGRFieldDefn *poFieldDefn = poDefn->GetFieldDefn(iField);

        poDstFile->AddFieldNative(poFieldDefn->GetNameRef(),
                                  poSrcFile->GetNativeFieldType(iField),
                                  poFieldDefn->GetWidth(),
                                  poFieldDefn->GetPrecision(),
                                  poSrcFile->IsFieldIndexed(iField),
                                  poSrcFile->IsFieldUnique(iField));
    }

    /*---------------------------------------------------------------------
     * Copy objects until EOF is reached
     *--------------------------------------------------------------------*/
    nFeatureId = -1;
    while ( (nFeatureId = poSrcFile->GetNextFeatureId(nFeatureId)) != -1 &&
            (nMaxFeatures < 1 || numFeatures++ < nMaxFeatures ))
    {
        poFeature = poSrcFile->GetFeatureRef(nFeatureId);
        if (poFeature)
        {
//            poFeature->DumpReadable(stdout);
//            poFeature->DumpMIF();
            poDstFile->CreateFeature(poFeature);
        }
        else
        {
            printf( "Failed to read feature %d.\n",
                    nFeatureId );
            return -1;      // GetFeatureRef() failed: Error
        }
    }

    /*---------------------------------------------------------------------
     * Cleanup and exit.
     *--------------------------------------------------------------------*/
    poDstFile->Close();
    delete poDstFile;

    poSrcFile->Close();
    delete poSrcFile;

    MITABFreeCoordSysTable();

    return 0;
}