コード例 #1
0
ファイル: ogr_proj.cpp プロジェクト: jeroenooms/rgdal
SEXP ogrAutoIdentifyEPSG(SEXP p4s) {

    OGRSpatialReference hSRS = NULL;
    OGRErr thisOGRErr;
    SEXP ans;

    installErrorHandler();
    if (hSRS.importFromProj4(CHAR(STRING_ELT(p4s, 0))) != OGRERR_NONE) {
        uninstallErrorHandlerAndTriggerError();
	error("Can't parse PROJ.4-style parameter string");
    }
    uninstallErrorHandlerAndTriggerError();
    PROTECT(ans=NEW_CHARACTER(1));

    installErrorHandler();
    thisOGRErr = hSRS.AutoIdentifyEPSG();
    uninstallErrorHandlerAndTriggerError();

    if (thisOGRErr == OGRERR_NONE) {
        installErrorHandler();
        SET_STRING_ELT(ans, 0,
            COPY_TO_USER_STRING(hSRS.GetAuthorityCode(NULL)));
        uninstallErrorHandlerAndTriggerError();
    } else if (thisOGRErr == OGRERR_UNSUPPORTED_SRS) {
        SET_STRING_ELT(ans, 0,
            COPY_TO_USER_STRING("OGRERR_UNSUPPORTED_SRS"));
    }

    UNPROTECT(1);

    return(ans);
}
コード例 #2
0
ファイル: sigdemdataset.cpp プロジェクト: OSGeo/gdal
static OGRSpatialReference* BuildSRS(const char* pszWKT) {
    OGRSpatialReference* poSRS = new OGRSpatialReference(pszWKT);
    if (poSRS->morphFromESRI() != OGRERR_NONE) {
        delete poSRS;
        return nullptr;
    } else {
        if (poSRS->AutoIdentifyEPSG() != OGRERR_NONE) {
            int nEntries = 0;
            int* panConfidence = nullptr;
            OGRSpatialReferenceH* pahSRS = poSRS->FindMatches(nullptr,
                    &nEntries, &panConfidence);
            if (nEntries == 1 && panConfidence[0] == 100) {
                poSRS->Release();
                poSRS = reinterpret_cast<OGRSpatialReference*>(pahSRS[0]);
                CPLFree(pahSRS);
            } else {
                OSRFreeSRSArray(pahSRS);
            }
            CPLFree(panConfidence);
        }
        return poSRS;
    }
}
コード例 #3
0
TeOGRDriver::TeOGRDriver(const std::string& fileName, const bool &writing, const std::string &OGRDriverName) :
TeGeoDataDriver(),
fileName_(fileName),
ogrLayer_(0)
{
	// Registers all format drivers built into OGR.
	OGRRegisterAll();
	ogrDS_=0;
	if(writing == false)
	{
		// Open OGR Datasource
		ogrDS_ = OGRSFDriverRegistrar::Open(fileName.c_str());
		/* TODO: The data set may has one or more layers. Is necessary a revision on TeGeoDataDriver interface?
			 For while, only first layer is considered by the driver. */
		if(ogrDS_ != 0)
		{
			int nLayers = ogrDS_->GetLayerCount();
			if(nLayers > 0)
			{
				ogrLayer_ = ogrDS_->GetLayer(0); // Gets the first layer
			}
		}

		if(ogrLayer_ == 0)
		{
			std::string errorMessage = CPLGetLastErrorMsg();
			return;
		}

		// Number of objects
		numObjects_ = ogrLayer_->GetFeatureCount();

		// Box
		OGREnvelope* env = new OGREnvelope();
		ogrLayer_->GetExtent(env);
		box_ = Convert2TerraLib(env);
		delete env;
	
		// Geometric representation
		rep_ = Convert2TerraLib(ogrLayer_->GetLayerDefn()->GetGeomType());

		attrList_ = Convert2TerraLib(ogrLayer_->GetLayerDefn());
	}
	else
	{
		OGRSFDriver* poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(OGRDriverName.c_str());
		ogrDS_ = poDriver->CreateDataSource(fileName.c_str(), NULL );
	}

	if(ogrLayer_ == 0)
	{
		dataProjection_ = new TeNoProjection();
		return;
	}

	OGRSpatialReference* srs = ogrLayer_->GetSpatialRef();
	if(srs == 0)
	{
		dataProjection_ = new TeNoProjection();
		return;
	}

	if(srs->AutoIdentifyEPSG() == OGRERR_UNSUPPORTED_SRS)
	{
		dataProjection_ = new TeNoProjection();
		return;
	}

	dataProjection_ = TeProjectionFactory::make(atoi(srs->GetAuthorityCode(0)));
}
コード例 #4
0
int OGRGeoPackageDataSource::GetSrsId(const OGRSpatialReference * cpoSRS)
{
    char *pszWKT = NULL;
    char *pszSQL = NULL;
    int nSRSId = UNDEFINED_SRID;
    const char* pszAuthorityName;
    int nAuthorityCode = 0;
    OGRErr err;
    OGRBoolean bCanUseAuthorityCode = FALSE;

    if( cpoSRS == NULL )
        return UNDEFINED_SRID;

    OGRSpatialReference *poSRS = cpoSRS->Clone();

    poSRS->morphFromESRI();
    pszAuthorityName = poSRS->GetAuthorityName(NULL);

    if ( pszAuthorityName == NULL || strlen(pszAuthorityName) == 0 )
    {
        // Try to force identify an EPSG code                                    
        poSRS->AutoIdentifyEPSG();

        pszAuthorityName = poSRS->GetAuthorityName(NULL);
        if (pszAuthorityName != NULL && EQUAL(pszAuthorityName, "EPSG"))
        {
            const char* pszAuthorityCode = poSRS->GetAuthorityCode(NULL);
            if ( pszAuthorityCode != NULL && strlen(pszAuthorityCode) > 0 )
            {
                /* Import 'clean' SRS */
                poSRS->importFromEPSG( atoi(pszAuthorityCode) );

                pszAuthorityName = poSRS->GetAuthorityName(NULL);
            }
        }
    }
    // Check whether the EPSG authority code is already mapped to a
    // SRS ID.                                                         
    if ( pszAuthorityName != NULL && strlen(pszAuthorityName) > 0 )
    {
        // For the root authority name 'EPSG', the authority code
        // should always be integral
        nAuthorityCode = atoi( poSRS->GetAuthorityCode(NULL) );

        pszSQL = sqlite3_mprintf(
                         "SELECT srs_id FROM gpkg_spatial_ref_sys WHERE "
                         "upper(organization) = upper('%q') AND organization_coordsys_id = %d",
                         pszAuthorityName, nAuthorityCode );
        
        nSRSId = SQLGetInteger(m_poDb, pszSQL, &err);
        sqlite3_free(pszSQL);
        
        // Got a match? Return it!
        if ( OGRERR_NONE == err )
        {
            delete poSRS;
            return nSRSId;
        }
        
        // No match, but maybe we can use the nAuthorityCode as the nSRSId?
        pszSQL = sqlite3_mprintf(
                         "SELECT Count(*) FROM gpkg_spatial_ref_sys WHERE "
                         "srs_id = %d", nAuthorityCode );
        
        // Yep, we can!
        if ( ! SQLGetInteger(m_poDb, pszSQL, &err) && err == OGRERR_NONE )
            bCanUseAuthorityCode = TRUE;
    }

    // Translate SRS to WKT.                                           
    if( poSRS->exportToWkt( &pszWKT ) != OGRERR_NONE )
    {
        delete poSRS;
        CPLFree(pszWKT);
        return UNDEFINED_SRID;
    }

    // Reuse the authority code number as SRS_ID if we can
    if ( bCanUseAuthorityCode )
    {
        nSRSId = nAuthorityCode;
    }
    // Otherwise, generate a new SRS_ID number (max + 1)
    else
    {
        // Get the current maximum srid in the srs table.                  
        int nMaxSRSId = SQLGetInteger(m_poDb, "SELECT MAX(srs_id) FROM gpkg_spatial_ref_sys", &err);
        if ( OGRERR_NONE != err )
        {
            CPLFree(pszWKT);
            delete poSRS;
            return UNDEFINED_SRID;        
        }

        nSRSId = nMaxSRSId + 1;
    }
    
    // Add new SRS row to gpkg_spatial_ref_sys
    if( pszAuthorityName != NULL && nAuthorityCode > 0 )
    {
        pszSQL = sqlite3_mprintf(
                 "INSERT INTO gpkg_spatial_ref_sys "
                 "(srs_name,srs_id,organization,organization_coordsys_id,definition) "
                 "VALUES ('%s', %d, upper('%s'), %d, '%q')",
                 GetSrsName(poSRS), nSRSId, pszAuthorityName, nAuthorityCode, pszWKT
                 );
    }
    else
    {
        pszSQL = sqlite3_mprintf(
                 "INSERT INTO gpkg_spatial_ref_sys "
                 "(srs_name,srs_id,organization,organization_coordsys_id,definition) "
                 "VALUES ('%s', %d, upper('%s'), %d, '%q')",
                 GetSrsName(poSRS), nSRSId, "NONE", nSRSId, pszWKT
                 );
    }

    // Add new row to gpkg_spatial_ref_sys
    err = SQLCommand(m_poDb, pszSQL);

    // Free everything that was allocated.
    CPLFree(pszWKT);    
    sqlite3_free(pszSQL);
    delete poSRS;
    
    return nSRSId;
}