GDALWSpatialInfo GDALWGetSpatialInfo(GDALWConnection conn) { GDALWSpatialInfo spatialInfo; OGRSpatialReferenceH spatialRef = OGR_L_GetSpatialRef(conn.layer); char * proj4, * srsText, * srid; OSRExportToProj4(spatialRef, &proj4); OSRExportToWkt(spatialRef, &srsText); srid = (char *) OSRGetAttrValue(spatialRef, "AUTHORITY", 1); if (srid == NULL) { spatialInfo.epsg = 4326; } else { spatialInfo.epsg = atoi(OSRGetAttrValue(spatialRef, "AUTHORITY", 1)); } spatialInfo.authName = OSRGetAttrValue(spatialRef, "AUTHORITY", 0); if (spatialInfo.authName == NULL) { spatialInfo.authName = "EPSG"; } spatialInfo.proj4Text = proj4; spatialInfo.srsText = srsText; return spatialInfo; }
int readoutlets(char *outletsds,char *lyrname, int uselayername,int outletslyr,OGRSpatialReferenceH hSRSRaster,int *noutlets, double*& x, double*& y,int*& id) { // initializing datasoruce,layer,feature, geomtery, spatial reference OGRSFDriverH driver; OGRDataSourceH hDS1; OGRLayerH hLayer1; OGRFeatureDefnH hFDefn1; OGRFieldDefnH hFieldDefn1; OGRFeatureH hFeature1; OGRGeometryH geometry, line; OGRSpatialReferenceH hRSOutlet; // register all ogr driver related to OGR OGRRegisterAll(); // open data source hDS1 = OGROpen(outletsds, FALSE, NULL ); if( hDS1 == NULL ) { printf( "Error Opening OGR Data Source .\n" ); return 1; } //get layer from layer name if(uselayername==1) { hLayer1 = OGR_DS_GetLayerByName(hDS1,lyrname);} //get layerinfo from layer number else { hLayer1 = OGR_DS_GetLayer(hDS1,outletslyr);} // get layerinfo from layername if(hLayer1 == NULL)getlayerfail(hDS1,outletsds,outletslyr); OGRwkbGeometryType gtype; gtype=OGR_L_GetGeomType(hLayer1); // Test that the type is a point if(gtype != wkbPoint)getlayerfail(hDS1,outletsds,outletslyr); const char* RasterProjectionName; const char* sprs; const char* sprso; const char* OutletProjectionName; int pj_raster,pj_outlet; // Spatial reference of outlet hRSOutlet = OGR_L_GetSpatialRef(hLayer1); if(hSRSRaster!=NULL){ pj_raster=OSRIsProjected(hSRSRaster); // find if projected or not if(pj_raster==0) {sprs="GEOGCS";} else { sprs="PROJCS"; } RasterProjectionName = OSRGetAttrValue(hSRSRaster,sprs,0); // get projection name } if(hRSOutlet!=NULL){ pj_outlet=OSRIsProjected(hRSOutlet); if(pj_outlet==0) {sprso="GEOGCS";} else { sprso="PROJCS"; } OutletProjectionName = OSRGetAttrValue(hRSOutlet,sprso,0); } //Write warnings where projections may not match if(hRSOutlet!=NULL && hSRSRaster!=NULL){ if (pj_raster==pj_outlet){ int rc= strcmp(RasterProjectionName,OutletProjectionName); // compare string if(rc!=0){ printf( "Warning: Projection of Outlet feature and Raster data may be different.\n" ); printf("Projection of Raster datasource %s.\n",RasterProjectionName); printf("Projection of Outlet feature %s.\n",OutletProjectionName); } } else { printf( "Warning: Spatial References of Outlet feature and Raster data are different.\n" ); printf("Projection of Raster datasource %s.\n",RasterProjectionName); printf("Projection of Outlet feature %s.\n",OutletProjectionName); } } else if(hSRSRaster==NULL && hRSOutlet!=NULL) { printf( "Warning: Spatial Reference of Raster is missing.\n" ); printf("Projection of Outlet feature %s.\n",OutletProjectionName); } else if(hSRSRaster!=NULL && hRSOutlet==NULL) { printf( "Warning: Spatial Reference of Outlet feature is missing.\n" ); printf("Projection of Raster datasource %s.\n",RasterProjectionName); } else { printf( "Warning: Spatial References of Outlet feature and Raster data are missing.\n" ); } long countPts=0; // count number of feature countPts=OGR_L_GetFeatureCount(hLayer1,0); // get schema i.e geometry, properties (e.g. ID) hFDefn1 = OGR_L_GetLayerDefn(hLayer1); x = new double[countPts]; y = new double[countPts]; int iField; int nxy=0; id = new int[countPts]; // loop through each feature and get lat,lon and id information OGR_L_ResetReading(hLayer1); while( (hFeature1 = OGR_L_GetNextFeature(hLayer1)) != NULL ) { //hFeature1=OGR_L_GetFeature(hLayer1,j); // get feature info geometry = OGR_F_GetGeometryRef(hFeature1); // get geometry x[nxy] = OGR_G_GetX(geometry, 0); y[nxy] = OGR_G_GetY(geometry, 0); int idfld =OGR_F_GetFieldIndex(hFeature1,"id"); if (idfld >= 0) { hFieldDefn1 = OGR_FD_GetFieldDefn( hFDefn1,idfld); // get field definiton based on index if( OGR_Fld_GetType(hFieldDefn1) == OFTInteger ) { id[nxy] =OGR_F_GetFieldAsInteger( hFeature1, idfld );} // get id value } else { id[nxy]=1;// if there is no id field } nxy++; // count number of outlets point OGR_F_Destroy( hFeature1 ); // destroy feature } *noutlets=nxy; OGR_DS_Destroy( hDS1); // destroy data source return 0; }
static int RasterliteInsertSRID(OGRDataSourceH hDS, const char* pszWKT) { CPLString osSQL; int nAuthorityCode = 0; CPLString osAuthorityName, osProjCS, osProj4; if (pszWKT != NULL && strlen(pszWKT) != 0) { OGRSpatialReferenceH hSRS = OSRNewSpatialReference(pszWKT); if (hSRS) { const char* pszAuthorityName = OSRGetAuthorityName(hSRS, NULL); if (pszAuthorityName) osAuthorityName = pszAuthorityName; const char* pszProjCS = OSRGetAttrValue(hSRS, "PROJCS", 0); if (pszProjCS) osProjCS = pszProjCS; const char* pszAuthorityCode = OSRGetAuthorityCode(hSRS, NULL); if (pszAuthorityCode) nAuthorityCode = atoi(pszAuthorityCode); char *pszProj4 = NULL; if( OSRExportToProj4( hSRS, &pszProj4 ) != OGRERR_NONE ) pszProj4 = CPLStrdup(""); osProj4 = pszProj4; CPLFree(pszProj4); } OSRDestroySpatialReference(hSRS); } int nSRSId = -1; if (nAuthorityCode != 0 && osAuthorityName.size() != 0) { osSQL.Printf ("SELECT srid FROM spatial_ref_sys WHERE auth_srid = %d", nAuthorityCode); OGRLayerH hLyr = OGR_DS_ExecuteSQL(hDS, osSQL.c_str(), NULL, NULL); if (hLyr == NULL) { nSRSId = nAuthorityCode; if ( osProjCS.size() != 0 ) osSQL.Printf( "INSERT INTO spatial_ref_sys " "(srid, auth_name, auth_srid, ref_sys_name, proj4text) " "VALUES (%d, '%s', '%d', '%s', '%s')", nSRSId, osAuthorityName.c_str(), nAuthorityCode, osProjCS.c_str(), osProj4.c_str() ); else osSQL.Printf( "INSERT INTO spatial_ref_sys " "(srid, auth_name, auth_srid, proj4text) " "VALUES (%d, '%s', '%d', '%s')", nSRSId, osAuthorityName.c_str(), nAuthorityCode, osProj4.c_str() ); OGR_DS_ExecuteSQL(hDS, osSQL.c_str(), NULL, NULL); } else { OGRFeatureH hFeat = OGR_L_GetNextFeature(hLyr); if (hFeat) { nSRSId = OGR_F_GetFieldAsInteger(hFeat, 0); OGR_F_Destroy(hFeat); } OGR_DS_ReleaseResultSet(hDS, hLyr); } } return nSRSId; }
OGRSpatialReferenceH GPJ_grass_to_osr(struct Key_Value * proj_info, struct Key_Value * proj_units) { struct pj_info pjinfo; char *proj4, *proj4mod, *wkt, *modwkt, *startmod, *lastpart; OGRSpatialReferenceH hSRS, hSRS2; OGRErr errcode; struct gpj_datum dstruct; struct gpj_ellps estruct; size_t len; const char *ellpskv, *unit, *unfact; char *ellps, *ellpslong, *datum, *params, *towgs84, *datumlongname, *start, *end; const char *sysname, *osrunit, *osrunfact; double a, es, rf; int haveparams = 0; if ((proj_info == NULL) || (proj_units == NULL)) return NULL; hSRS = OSRNewSpatialReference(NULL); if (pj_get_kv(&pjinfo, proj_info, proj_units) < 0) { G_warning(_("Unable parse GRASS PROJ_INFO file")); return NULL; } if ((proj4 = pj_get_def(pjinfo.pj, 0)) == NULL) { G_warning(_("Unable get PROJ.4-style parameter string")); return NULL; } pj_free(pjinfo.pj); unit = G_find_key_value("unit", proj_units); unfact = G_find_key_value("meters", proj_units); if (unfact != NULL && (strcmp(pjinfo.proj, "ll") != 0)) G_asprintf(&proj4mod, "%s +to_meter=%s", proj4, unfact); else proj4mod = G_store(proj4); pj_dalloc(proj4); if ((errcode = OSRImportFromProj4(hSRS, proj4mod)) != OGRERR_NONE) { G_warning(_("OGR can't parse PROJ.4-style parameter string: " "%s (OGR Error code was %d)"), proj4mod, errcode); return NULL; } G_free(proj4mod); if ((errcode = OSRExportToWkt(hSRS, &wkt)) != OGRERR_NONE) { G_warning(_("OGR can't get WKT-style parameter string " "(OGR Error code was %d)"), errcode); return NULL; } ellpskv = G_find_key_value("ellps", proj_info); GPJ__get_ellipsoid_params(proj_info, &a, &es, &rf); haveparams = GPJ__get_datum_params(proj_info, &datum, ¶ms); if(ellpskv != NULL) ellps = G_store(ellpskv); else ellps = NULL; if ((datum == NULL) || (GPJ_get_datum_by_name(datum, &dstruct) < 0)) { datumlongname = G_store("unknown"); if (ellps == NULL) ellps = G_store("unnamed"); } else { datumlongname = G_store(dstruct.longname); if (ellps == NULL) ellps = G_store(dstruct.ellps); GPJ_free_datum(&dstruct); } G_free(datum); if (GPJ_get_ellipsoid_by_name(ellps, &estruct) > 0) { ellpslong = G_store(estruct.longname); DatumNameMassage(&ellpslong); GPJ_free_ellps(&estruct); } else ellpslong = G_store(ellps); startmod = strstr(wkt, "GEOGCS"); lastpart = strstr(wkt, "PRIMEM"); len = strlen(wkt) - strlen(startmod); wkt[len] = '\0'; if (haveparams == 2) { /* Only put datum params into the WKT if they were specifically * specified in PROJ_INFO */ char *paramkey, *paramvalue; paramkey = strtok(params, "="); paramvalue = params + strlen(paramkey) + 1; if (G_strcasecmp(paramkey, "towgs84") == 0) G_asprintf(&towgs84, ",TOWGS84[%s]", paramvalue); else towgs84 = G_store(""); G_free(params); } else towgs84 = G_store(""); sysname = OSRGetAttrValue(hSRS, "PROJCS", 0); if (sysname == NULL) { /* Not a projected co-ordinate system */ start = G_store(""); end = G_store(""); } else { if ((strcmp(sysname, "unnamed") == 0) && (G_find_key_value("name", proj_info) != NULL)) G_asprintf(&start, "PROJCS[\"%s\",", G_find_key_value("name", proj_info)); else start = G_store(wkt); osrunit = OSRGetAttrValue(hSRS, "UNIT", 0); osrunfact = OSRGetAttrValue(hSRS, "UNIT", 1); if ((unfact == NULL) || (G_strcasecmp(osrunit, "unknown") != 0)) end = G_store(""); else { char *buff; double unfactf = atof(unfact); G_asprintf(&buff, ",UNIT[\"%s\",", osrunit); startmod = strstr(lastpart, buff); len = strlen(lastpart) - strlen(startmod); lastpart[len] = '\0'; G_free(buff); if (unit == NULL) unit = "unknown"; G_asprintf(&end, ",UNIT[\"%s\",%.16g]]", unit, unfactf); } } OSRDestroySpatialReference(hSRS); G_asprintf(&modwkt, "%sGEOGCS[\"%s\",DATUM[\"%s\",SPHEROID[\"%s\",%.16g,%.16g]%s],%s%s", start, ellps, datumlongname, ellpslong, a, rf, towgs84, lastpart, end); hSRS2 = OSRNewSpatialReference(modwkt); G_free(modwkt); CPLFree(wkt); G_free(start); G_free(ellps); G_free(datumlongname); G_free(ellpslong); G_free(towgs84); G_free(end); return hSRS2; }
int GPJ_osr_to_grass(struct Cell_head *cellhd, struct Key_Value **projinfo, struct Key_Value **projunits, OGRSpatialReferenceH hSRS, int datumtrans) { struct Key_Value *temp_projinfo; char *pszProj4 = NULL, *pszRemaining; char *pszProj = NULL; char *datum = NULL; struct gpj_datum dstruct; if (hSRS == NULL) goto default_to_xy; /* Set finder function for locating OGR csv co-ordinate system tables */ SetCSVFilenameHook(GPJ_set_csv_loc); /* Hopefully this doesn't do any harm if it wasn't in ESRI format * to start with... */ OSRMorphFromESRI(hSRS); /* -------------------------------------------------------------------- */ /* Set cellhd for well known coordinate systems. */ /* -------------------------------------------------------------------- */ if (!OSRIsGeographic(hSRS) && !OSRIsProjected(hSRS)) goto default_to_xy; if (cellhd) { int bNorth; if (OSRIsGeographic(hSRS)) { cellhd->proj = PROJECTION_LL; cellhd->zone = 0; } else if (OSRGetUTMZone(hSRS, &bNorth) != 0) { cellhd->proj = PROJECTION_UTM; cellhd->zone = OSRGetUTMZone(hSRS, &bNorth); if (!bNorth) cellhd->zone *= -1; } else { cellhd->proj = PROJECTION_OTHER; cellhd->zone = 0; } } /* -------------------------------------------------------------------- */ /* Get the coordinate system definition in PROJ.4 format. */ /* -------------------------------------------------------------------- */ if (OSRExportToProj4(hSRS, &pszProj4) != OGRERR_NONE) goto default_to_xy; /* -------------------------------------------------------------------- */ /* Parse the PROJ.4 string into key/value pairs. Do a bit of */ /* extra work to "GRASSify" the result. */ /* -------------------------------------------------------------------- */ temp_projinfo = G_create_key_value(); /* Create "local" copy of proj4 string so we can modify and free it * using GRASS functions */ pszRemaining = G_store(pszProj4); CPLFree(pszProj4); pszProj4 = pszRemaining; while ((pszRemaining = strstr(pszRemaining, "+")) != NULL) { char *pszToken, *pszValue; pszRemaining++; /* Advance pszRemaining to end of this token[=value] pair */ pszToken = pszRemaining; while (*pszRemaining != ' ' && *pszRemaining != '\0') pszRemaining++; if (*pszRemaining == ' ') { *pszRemaining = '\0'; pszRemaining++; } /* parse token, and value */ if (strstr(pszToken, "=") != NULL) { pszValue = strstr(pszToken, "="); *pszValue = '\0'; pszValue++; } else pszValue = "defined"; if (G_strcasecmp(pszToken, "proj") == 0) { /* The ll projection is known as longlat in PROJ.4 */ if (G_strcasecmp(pszValue, "longlat") == 0) pszValue = "ll"; pszProj = pszValue; continue; } /* Ellipsoid and datum handled separately below */ if (G_strcasecmp(pszToken, "ellps") == 0 || G_strcasecmp(pszToken, "a") == 0 || G_strcasecmp(pszToken, "b") == 0 || G_strcasecmp(pszToken, "es") == 0 || G_strcasecmp(pszToken, "rf") == 0 || G_strcasecmp(pszToken, "datum") == 0) continue; /* We will handle units separately */ if (G_strcasecmp(pszToken, "to_meter") == 0 || G_strcasecmp(pszToken, "units") == 0) continue; G_set_key_value(pszToken, pszValue, temp_projinfo); } *projinfo = G_create_key_value(); /* -------------------------------------------------------------------- */ /* Derive the user name for the projection. */ /* -------------------------------------------------------------------- */ if (pszProj) { char path[4095]; char name[80]; sprintf(path, "%s/etc/projections", G_gisbase()); if (G_lookup_key_value_from_file(path, pszProj, name, sizeof(name)) > 0) G_set_key_value("name", name, *projinfo); else G_set_key_value("name", pszProj, *projinfo); G_set_key_value("proj", pszProj, *projinfo); } else G_warning(_("No projection name! Projection parameters likely to be meaningless.")); /* -------------------------------------------------------------------- */ /* Find the GRASS datum name and choose parameters either */ /* interactively or not. */ /* -------------------------------------------------------------------- */ { const char *pszDatumNameConst = OSRGetAttrValue(hSRS, "DATUM", 0); struct datum_list *list, *listhead; char *dum1, *dum2, *pszDatumName; int paramspresent = GPJ__get_datum_params(temp_projinfo, &dum1, &dum2); if (pszDatumNameConst) { /* Need to make a new copy of the string so we don't mess * around with the memory inside the OGRSpatialReferenceH? */ pszDatumName = G_store(pszDatumNameConst); DatumNameMassage(&pszDatumName); list = listhead = read_datum_table(); while (list != NULL) { if (G_strcasecmp(pszDatumName, list->longname) == 0) { datum = G_store(list->name); break; } list = list->next; } free_datum_list(listhead); if (datum == NULL) { if (paramspresent < 2) /* Only give warning if no parameters present */ G_warning(_("Datum <%s> not recognised by GRASS and no parameters found"), pszDatumName); } else { G_set_key_value("datum", datum, *projinfo); if (paramspresent < 2) { /* If no datum parameters were imported from the OSR * object then we should use the set specified by datumtrans */ char *params, *chosenparams = NULL; int paramsets; paramsets = GPJ_get_default_datum_params_by_name(datum, ¶ms); if (paramsets < 0) G_warning(_("Datum <%s> apparently recognised by GRASS but no parameters found. " "You may want to look into this."), datum); else if (datumtrans > paramsets) { G_warning(_("Invalid transformation number %d; valid range is 1 to %d. " "Leaving datum transform parameters unspecified."), datumtrans, paramsets); datumtrans = 0; } if (paramsets > 0) { struct gpj_datum_transform_list *list, *old; list = GPJ_get_datum_transform_by_name(datum); if (list != NULL) { do { if (list->count == datumtrans) chosenparams = G_store(list->params); old = list; list = list->next; GPJ_free_datum_transform(old); } while (list != NULL); } } if (chosenparams != NULL) { char *paramkey, *paramvalue; paramkey = strtok(chosenparams, "="); paramvalue = chosenparams + strlen(paramkey) + 1; G_set_key_value(paramkey, paramvalue, *projinfo); G_free(chosenparams); } if (paramsets > 0) G_free(params); } } G_free(pszDatumName); } } /* -------------------------------------------------------------------- */ /* Determine an appropriate GRASS ellipsoid name if possible, or */ /* else just put a and es values into PROJ_INFO */ /* -------------------------------------------------------------------- */ if ((datum != NULL) && (GPJ_get_datum_by_name(datum, &dstruct) > 0)) { /* Use ellps name associated with datum */ G_set_key_value("ellps", dstruct.ellps, *projinfo); GPJ_free_datum(&dstruct); G_free(datum); } else { /* If we can't determine the ellipsoid from the datum, derive it * directly from "SPHEROID" parameters in WKT */ const char *pszSemiMajor = OSRGetAttrValue(hSRS, "SPHEROID", 1); const char *pszInvFlat = OSRGetAttrValue(hSRS, "SPHEROID", 2); if (pszSemiMajor != NULL && pszInvFlat != NULL) { char *ellps = NULL; struct ellps_list *list, *listhead; double a = atof(pszSemiMajor), invflat = atof(pszInvFlat), flat; double es; /* Allow for incorrect WKT describing a sphere where InvFlat * is given as 0 rather than inf */ if (invflat > 0) flat = 1 / invflat; else flat = 0; es = flat * (2.0 - flat); list = listhead = read_ellipsoid_table(0); while (list != NULL) { /* Try and match a and es against GRASS defined ellipsoids; * accept first one that matches. These numbers were found * by trial and error and could be fine-tuned, or possibly * a direct comparison of IEEE floating point values used. */ if ((a == list->a || fabs(a - list->a) < 0.1 || fabs(1 - a / list->a) < 0.0000001) && ((es == 0 && list->es == 0) || /* Special case for sphere */ (invflat == list->rf || fabs(invflat - list->rf) < 0.0000001))) { ellps = G_store(list->name); break; } list = list->next; } if (listhead != NULL) free_ellps_list(listhead); if (ellps == NULL) { /* If we weren't able to find a matching ellps name, set * a and es values directly from WKT-derived data */ char es_str[100]; G_set_key_value("a", (char *)pszSemiMajor, *projinfo); sprintf(es_str, "%.16g", es); G_set_key_value("es", es_str, *projinfo); } else { /* else specify the GRASS ellps name for readability */ G_set_key_value("ellps", ellps, *projinfo); G_free(ellps); } } } /* -------------------------------------------------------------------- */ /* Finally append the detailed projection parameters to the end */ /* -------------------------------------------------------------------- */ { int i; for (i = 0; i < temp_projinfo->nitems; i++) G_set_key_value(temp_projinfo->key[i], temp_projinfo->value[i], *projinfo); G_free_key_value(temp_projinfo); } G_free(pszProj4); /* -------------------------------------------------------------------- */ /* Set the linear units. */ /* -------------------------------------------------------------------- */ *projunits = G_create_key_value(); if (OSRIsGeographic(hSRS)) { /* We assume degrees ... someday we will be wrong! */ G_set_key_value("unit", "degree", *projunits); G_set_key_value("units", "degrees", *projunits); G_set_key_value("meters", "1.0", *projunits); } else { char szFormatBuf[256]; char *pszUnitsName = NULL; double dfToMeters; char *pszUnitsPlural, *pszStringEnd; dfToMeters = OSRGetLinearUnits(hSRS, &pszUnitsName); /* Workaround for the most obvious case when unit name is unknown */ if ((G_strcasecmp(pszUnitsName, "unknown") == 0) && (dfToMeters == 1.)) G_asprintf(&pszUnitsName, "meter"); G_set_key_value("unit", pszUnitsName, *projunits); /* Attempt at plural formation (WKT format doesn't store plural * form of unit name) */ pszUnitsPlural = G_malloc(strlen(pszUnitsName) + 3); strcpy(pszUnitsPlural, pszUnitsName); pszStringEnd = pszUnitsPlural + strlen(pszUnitsPlural) - 4; if (G_strcasecmp(pszStringEnd, "foot") == 0) { /* Special case for foot - change two o's to e's */ pszStringEnd[1] = 'e'; pszStringEnd[2] = 'e'; } else if (G_strcasecmp(pszStringEnd, "inch") == 0) { /* Special case for inch - add es */ pszStringEnd[4] = 'e'; pszStringEnd[5] = 's'; pszStringEnd[6] = '\0'; } else { /* For anything else add an s at the end */ pszStringEnd[4] = 's'; pszStringEnd[5] = '\0'; } G_set_key_value("units", pszUnitsPlural, *projunits); G_free(pszUnitsPlural); sprintf(szFormatBuf, "%.16g", dfToMeters); G_set_key_value("meters", szFormatBuf, *projunits); } return 2; /* -------------------------------------------------------------------- */ /* Fallback to returning an ungeoreferenced definition. */ /* -------------------------------------------------------------------- */ default_to_xy: if (cellhd != NULL) { cellhd->proj = PROJECTION_XY; cellhd->zone = 0; } *projinfo = NULL; *projunits = NULL; return 1; }
bool rspfOgcWktTranslator::toOssimKwl( const rspfString& wktString, rspfKeywordlist &kwl, const char *prefix)const { static const char MODULE[] = "rspfOgcWktTranslator::toOssimKwl"; if(traceDebug()) { rspfNotify(rspfNotifyLevel_DEBUG) << MODULE << " entered...\n"; } const char* wkt = wktString.c_str(); OGRSpatialReferenceH hSRS = NULL; rspfDpt falseEastingNorthing; hSRS = OSRNewSpatialReference(NULL); if( OSRImportFromWkt( hSRS, (char **) &wkt ) != OGRERR_NONE ) { OSRDestroySpatialReference( hSRS ); return false; } rspfString rspfProj = ""; const char* epsg_code = OSRGetAttrValue( hSRS, "AUTHORITY", 1 ); if(traceDebug()) { rspfNotify(rspfNotifyLevel_DEBUG) << "epsg_code: " << (epsg_code?epsg_code:"null") << "\n"; } const char* units = NULL; OGR_SRSNode* node = ((OGRSpatialReference *)hSRS)->GetRoot(); int nbChild = node->GetChildCount(); for (int i = 0; i < nbChild; i++) { OGR_SRSNode* curChild = node->GetChild(i); if (strcmp(curChild->GetValue(), "UNIT") == 0) { units = curChild->GetChild(0)->GetValue(); } } if(traceDebug()) { rspfNotify(rspfNotifyLevel_DEBUG) << "units: " << (units?units:"null") << "\n"; } rspfString rspf_units; bool bGeog = OSRIsGeographic(hSRS); if ( bGeog == false ) { rspf_units = "meters"; if ( units != NULL ) { rspfString s = units; s.downcase(); if( ( s == rspfString("us survey foot") ) || ( s == rspfString("u.s. foot") ) || ( s == rspfString("foot_us") ) ) { rspf_units = "us_survey_feet"; } else if( s == rspfString("degree") ) { rspf_units = "degrees"; } else if( ( s == rspfString("meter") ) || ( s == rspfString("metre") ) ) { rspf_units = "meters"; } } } else { rspf_units = "degrees"; } if(traceDebug()) { rspfNotify(rspfNotifyLevel_DEBUG) << "rspf_units: " << rspf_units << "\n"; } if (epsg_code) { rspfString epsg_spec ("EPSG:"); epsg_spec += rspfString::toString(epsg_code); rspfProjection* proj = rspfEpsgProjectionFactory::instance()->createProjection(epsg_spec); if (proj) rspfProj = proj->getClassName(); delete proj; } if(rspfProj == "") { const char* pszProjection = OSRGetAttrValue( hSRS, "PROJECTION", 0 ); if(pszProjection) { rspfProj = wktToOssimProjection(pszProjection); } else { rspfString localCs = OSRGetAttrValue( hSRS, "LOCAL_CS", 0 ); localCs = localCs.upcase(); if(localCs == "GREATBRITAIN_GRID") { rspfProj = "rspfBngProjection"; } else if (rspf_units.contains("degree")) { rspfProj = "rspfEquDistCylProjection"; } } } if(rspfProj == "rspfEquDistCylProjection" ) rspf_units = "degrees"; kwl.add(prefix, rspfKeywordNames::UNITS_KW, rspf_units, true); if (traceDebug()) { rspfNotify(rspfNotifyLevel_DEBUG) << MODULE << "DEBUG:" << "\nrspfProj = " << rspfProj << endl; } kwl.add(prefix, rspfKeywordNames::TYPE_KW, rspfProj.c_str(), true); falseEastingNorthing.x = OSRGetProjParm(hSRS, SRS_PP_FALSE_EASTING, 0.0, NULL); falseEastingNorthing.y = OSRGetProjParm(hSRS, SRS_PP_FALSE_NORTHING, 0.0, NULL); if (epsg_code) { kwl.add(prefix, rspfKeywordNames::PCS_CODE_KW, epsg_code, true); } if(rspfProj == "rspfBngProjection") { kwl.add(prefix, rspfKeywordNames::TYPE_KW, "rspfBngProjection", true); } else if(rspfProj == "rspfCylEquAreaProjection") { kwl.add(prefix, rspfKeywordNames::STD_PARALLEL_1_KW, OSRGetProjParm(hSRS, SRS_PP_STANDARD_PARALLEL_1, 0.0, NULL), true); kwl.add(prefix, rspfKeywordNames::ORIGIN_LATITUDE_KW, OSRGetProjParm(hSRS, SRS_PP_STANDARD_PARALLEL_1, 0.0, NULL), true); rspfUnitType units = static_cast<rspfUnitType>(rspfUnitTypeLut::instance()-> getEntryNumber(rspf_units.c_str())); if ( units == RSPF_METERS || units == RSPF_FEET || units == RSPF_US_SURVEY_FEET ) { kwl.add(prefix, rspfKeywordNames::FALSE_EASTING_NORTHING_KW, falseEastingNorthing.toString(), true); kwl.add(prefix, rspfKeywordNames::FALSE_EASTING_NORTHING_UNITS_KW, rspf_units, true); } } else if(rspfProj == "rspfEquDistCylProjection") { kwl.add(prefix, rspfKeywordNames::TYPE_KW, "rspfEquDistCylProjection", true); rspfUnitType units = static_cast<rspfUnitType>(rspfUnitTypeLut::instance()-> getEntryNumber(rspf_units.c_str())); if ( units == RSPF_METERS || units == RSPF_FEET || units == RSPF_US_SURVEY_FEET ) { kwl.add(prefix, rspfKeywordNames::FALSE_EASTING_NORTHING_KW, falseEastingNorthing.toString(), true); kwl.add(prefix, rspfKeywordNames::FALSE_EASTING_NORTHING_UNITS_KW, rspf_units, true); } kwl.add(prefix, rspfKeywordNames::ORIGIN_LATITUDE_KW, OSRGetProjParm(hSRS, SRS_PP_LATITUDE_OF_ORIGIN, 0.0, NULL), true); kwl.add(prefix, rspfKeywordNames::CENTRAL_MERIDIAN_KW, OSRGetProjParm(hSRS, SRS_PP_CENTRAL_MERIDIAN, 0.0, NULL), true); } else if( (rspfProj == "rspfLambertConformalConicProjection") || (rspfProj == "rspfAlbersProjection") ) { kwl.add(prefix, rspfKeywordNames::TYPE_KW, rspfProj.c_str(), true); kwl.add(prefix, rspfKeywordNames::FALSE_EASTING_NORTHING_KW, falseEastingNorthing.toString(), true); kwl.add(prefix, rspfKeywordNames::FALSE_EASTING_NORTHING_UNITS_KW, rspf_units, true); kwl.add(prefix, rspfKeywordNames::ORIGIN_LATITUDE_KW, OSRGetProjParm(hSRS, SRS_PP_LATITUDE_OF_ORIGIN, 0.0, NULL), true); kwl.add(prefix, rspfKeywordNames::CENTRAL_MERIDIAN_KW, OSRGetProjParm(hSRS, SRS_PP_CENTRAL_MERIDIAN, 0.0, NULL), true); kwl.add(prefix, rspfKeywordNames::STD_PARALLEL_1_KW, OSRGetProjParm(hSRS, SRS_PP_STANDARD_PARALLEL_1, 0.0, NULL), true); kwl.add(prefix, rspfKeywordNames::STD_PARALLEL_2_KW, OSRGetProjParm(hSRS, SRS_PP_STANDARD_PARALLEL_2, 0.0, NULL), true); } else if(rspfProj == "rspfMercatorProjection") { kwl.add(prefix, rspfKeywordNames::TYPE_KW, "rspfMercatorProjection", true); kwl.add(prefix, rspfKeywordNames::ORIGIN_LATITUDE_KW, OSRGetProjParm(hSRS, SRS_PP_LATITUDE_OF_ORIGIN, 0.0, NULL), true); kwl.add(prefix, rspfKeywordNames::CENTRAL_MERIDIAN_KW, OSRGetProjParm(hSRS, SRS_PP_CENTRAL_MERIDIAN, 0.0, NULL), true); kwl.add(prefix, rspfKeywordNames::FALSE_EASTING_NORTHING_KW, falseEastingNorthing.toString(), true); kwl.add(prefix, rspfKeywordNames::FALSE_EASTING_NORTHING_UNITS_KW, rspf_units, true); } else if(rspfProj == "rspfSinusoidalProjection") { kwl.add(prefix, rspfKeywordNames::TYPE_KW, "rspfSinusoidalProjection", true); kwl.add(prefix, rspfKeywordNames::CENTRAL_MERIDIAN_KW, OSRGetProjParm(hSRS, SRS_PP_CENTRAL_MERIDIAN, 0.0, NULL), true); kwl.add(prefix, rspfKeywordNames::FALSE_EASTING_NORTHING_KW, falseEastingNorthing.toString(), true); kwl.add(prefix, rspfKeywordNames::FALSE_EASTING_NORTHING_UNITS_KW, rspf_units, true); } else if(rspfProj == "rspfTransMercatorProjection") { int bNorth; int nZone = OSRGetUTMZone( hSRS, &bNorth ); if( nZone != 0 ) { kwl.add(prefix, rspfKeywordNames::TYPE_KW, "rspfUtmProjection", true); kwl.add(prefix, rspfKeywordNames::ZONE_KW, nZone, true); if( bNorth ) { kwl.add(prefix, rspfKeywordNames::HEMISPHERE_KW, "N", true); } else { kwl.add(prefix, rspfKeywordNames::HEMISPHERE_KW, "S", true); } } else { kwl.add(prefix, rspfKeywordNames::TYPE_KW, "rspfTransMercatorProjection", true); kwl.add(prefix, rspfKeywordNames::SCALE_FACTOR_KW, OSRGetProjParm(hSRS, SRS_PP_SCALE_FACTOR, 1.0, NULL), true); kwl.add(prefix, rspfKeywordNames::ORIGIN_LATITUDE_KW, OSRGetProjParm(hSRS, SRS_PP_LATITUDE_OF_ORIGIN, 0.0, NULL), true); kwl.add(prefix, rspfKeywordNames::CENTRAL_MERIDIAN_KW, OSRGetProjParm(hSRS, SRS_PP_CENTRAL_MERIDIAN, 0.0, NULL), true); kwl.add(prefix, rspfKeywordNames::FALSE_EASTING_NORTHING_KW, falseEastingNorthing.toString(), true); kwl.add(prefix, rspfKeywordNames::FALSE_EASTING_NORTHING_UNITS_KW, rspf_units, true); } } else { if (traceDebug()) { rspfNotify(rspfNotifyLevel_DEBUG) << "rspfOgcWktTranslator::toOssimKwl DEBUG:\n" << "Projection conversion to RSPF not supported !!!!!!!!!\n" << "Please send the following string to the development staff\n" << "to be added to the transaltion to RSPF\n" << wkt << endl; } return false; } const char *datum = OSRGetAttrValue( hSRS, "DATUM", 0 ); rspfString oDatum = "WGE"; if( datum ) { oDatum = wktToOssimDatum(datum); if(oDatum == "") { oDatum = "WGE"; } } kwl.add(prefix, rspfKeywordNames::DATUM_KW, oDatum, true); OSRDestroySpatialReference( hSRS ); if (traceDebug()) { rspfNotify(rspfNotifyLevel_DEBUG) << MODULE << " exit status = true" << std::endl; } return true; }
bool GDALImageFileType::read( Image *OSG_GDAL_ARG(pImage), const Char8 *OSG_GDAL_ARG(fileName)) { #ifdef OSG_WITH_GDAL bool returnValue = false; GDALDataset *pDataset; pDataset = static_cast<GDALDataset *>(GDALOpen(fileName, GA_ReadOnly)); if(pDataset != NULL) { GeoReferenceAttachmentUnrecPtr pGeoRef = GeoReferenceAttachment::create(); pImage->addAttachment(pGeoRef); double adfGeoTransform[6]; if(pDataset->GetGeoTransform(adfGeoTransform) == CE_None) { pGeoRef->editOrigin().setValues(adfGeoTransform[0], adfGeoTransform[3]); pGeoRef->editPixelSize().setValues(adfGeoTransform[1], adfGeoTransform[5]); if(GDALGetProjectionRef(pDataset) != NULL) { OGRSpatialReferenceH hSRS; Char8 *szProjection = const_cast<char *>(GDALGetProjectionRef(pDataset)); hSRS = OSRNewSpatialReference(NULL); if(OSRImportFromWkt(hSRS, &szProjection) == CE_None) { pGeoRef->editEllipsoidAxis().setValues( OSRGetSemiMajor(hSRS, NULL), OSRGetSemiMinor(hSRS, NULL)); const Char8 *szDatum = OSRGetAttrValue(hSRS, "DATUM", 0); if(szDatum != NULL && 0 == strcmp(szDatum, "WGS_1984")) { pGeoRef->editDatum() = GeoReferenceAttachment::WGS84; } else { fprintf(stderr, "Unknow datum %s\n", szDatum); pGeoRef->editDatum() = GeoReferenceAttachment::UnknownDatum; } } OSRDestroySpatialReference(hSRS); } } GDALRasterBand *pBand; int nBlockXSize, nBlockYSize; int bGotMin, bGotMax; double adfMinMax[2]; pBand = pDataset->GetRasterBand( 1 ); pBand->GetBlockSize( &nBlockXSize, &nBlockYSize ); adfMinMax[0] = pBand->GetMinimum( &bGotMin ); adfMinMax[1] = pBand->GetMaximum( &bGotMax ); if(!(bGotMin && bGotMax)) GDALComputeRasterMinMax(GDALRasterBandH(pBand), TRUE, adfMinMax); pBand = pDataset->GetRasterBand(1); if(pBand != NULL) { Image::PixelFormat ePF = Image::OSG_INVALID_PF; switch(pDataset->GetRasterCount()) { case 1: ePF = Image::OSG_L_PF; break; case 2: ePF = Image::OSG_LA_PF; break; case 3: ePF = Image::OSG_RGB_PF; break; case 4: ePF = Image::OSG_RGBA_PF; break; } Image::Type eDT = Image::OSG_INVALID_IMAGEDATATYPE; switch(pBand->GetRasterDataType()) { case GDT_Byte: eDT = Image::OSG_UINT8_IMAGEDATA; break; case GDT_UInt16: eDT = Image::OSG_UINT16_IMAGEDATA; break; case GDT_Int16: eDT = Image::OSG_INT16_IMAGEDATA; break; case GDT_UInt32: eDT = Image::OSG_UINT32_IMAGEDATA; break; case GDT_Int32: eDT = Image::OSG_INT32_IMAGEDATA; break; case GDT_Float32: eDT = Image::OSG_FLOAT32_IMAGEDATA; break; case GDT_Float64: case GDT_CInt16: case GDT_CInt32: case GDT_CFloat32: case GDT_CFloat64: default: GDALClose(pDataset); return returnValue; break; } pImage->set(ePF, pDataset->GetRasterXSize(), pDataset->GetRasterYSize(), 1, 1, 1, 0.0, NULL, eDT); UChar8 *dst = pImage->editData(); pBand->RasterIO(GF_Read, 0, 0, pDataset->GetRasterXSize(), pDataset->GetRasterYSize(), dst, pDataset->GetRasterXSize(), pDataset->GetRasterYSize(), pBand->GetRasterDataType(), 0, 0); pGeoRef->setNoDataValue(pBand->GetNoDataValue()); returnValue = true; } GDALClose(pDataset); } return returnValue; #else SWARNING << getMimeType() << " read is not compiled into the current binary " << std::endl; return false; #endif // OSG_WITH_GDAL }
void GDALBlockAccessor::open(const Char8 *szFilename) { #ifdef OSG_WITH_GDAL _pDataset = static_cast<GDALDataset *>(GDALOpen(szFilename, GA_ReadOnly)); if(_pDataset != NULL) { _pGeoRef = GeoReferenceAttachment::create(); double adfGeoTransform[6]; if(_pDataset->GetGeoTransform(adfGeoTransform) == CE_None) { _pGeoRef->editOrigin().setValues(adfGeoTransform[0], adfGeoTransform[3]); _pGeoRef->editPixelSize().setValues(adfGeoTransform[1], adfGeoTransform[5]); if(GDALGetProjectionRef(_pDataset) != NULL) { OGRSpatialReferenceH hSRS; Char8 *szProjection = const_cast<char *>(GDALGetProjectionRef(_pDataset)); hSRS = OSRNewSpatialReference(NULL); if(OSRImportFromWkt(hSRS, &szProjection) == CE_None) { _pGeoRef->editEllipsoidAxis().setValues( OSRGetSemiMajor(hSRS, NULL), OSRGetSemiMinor(hSRS, NULL)); const Char8 *szDatum = OSRGetAttrValue(hSRS, "DATUM", 0); if(szDatum != NULL && 0 == strcmp(szDatum, "WGS_1984")) { _pGeoRef->editDatum() = GeoReferenceAttachment::WGS84; } else { fprintf(stderr, "Unknow datum %s\n", szDatum); _pGeoRef->editDatum() = GeoReferenceAttachment::UnknownDatum; } } OSRDestroySpatialReference(hSRS); } } int nBlockXSize, nBlockYSize; int bGotMin, bGotMax; double adfMinMax[2]; _pBand = _pDataset->GetRasterBand(1); _pBand->GetBlockSize(&nBlockXSize, &nBlockYSize); adfMinMax[0] = _pBand->GetMinimum(&bGotMin); adfMinMax[1] = _pBand->GetMaximum(&bGotMax); if(!(bGotMin && bGotMax)) { GDALComputeRasterMinMax(GDALRasterBandH(_pBand), TRUE, adfMinMax); } if(_pBand != NULL) { _eImgFormat = Image::OSG_INVALID_PF; switch(_pDataset->GetRasterCount()) { case 1: _eImgFormat = Image::OSG_L_PF; break; case 2: _eImgFormat = Image::OSG_LA_PF; break; case 3: _eImgFormat = Image::OSG_RGB_PF; break; case 4: _eImgFormat = Image::OSG_RGBA_PF; break; } _eImgType = Image::OSG_INVALID_IMAGEDATATYPE; switch(_pBand->GetRasterDataType()) { case GDT_Byte: _eImgType = Image::OSG_UINT8_IMAGEDATA; break; case GDT_UInt16: _eImgType = Image::OSG_UINT16_IMAGEDATA; break; case GDT_Int16: _eImgType = Image::OSG_INT16_IMAGEDATA; break; case GDT_UInt32: _eImgType = Image::OSG_UINT32_IMAGEDATA; break; case GDT_Int32: _eImgType = Image::OSG_INT32_IMAGEDATA; break; case GDT_Float32: _eImgType = Image::OSG_FLOAT32_IMAGEDATA; break; case GDT_Float64: case GDT_CInt16: case GDT_CInt32: case GDT_CFloat32: case GDT_CFloat64: default: GDALClose(_pDataset); _pDataset = NULL; break; } _vSize[0] = _pDataset->GetRasterXSize(); _vSize[1] = _pDataset->GetRasterYSize(); _fNoDataValue = _pBand->GetNoDataValue(); _pGeoRef->setNoDataValue(_fNoDataValue); } } #endif }