int QGisImporter::getGeometryType(QString const & path) { int ret = -1; // Well, this code is legacy, but fsck it :-P // Should not be compatible GDAL >= 2.0 // TODO: Anyway, Mapserver code should provide similar mechanisms, // I'm probably re-inventing the wheel here. OGRDataSourceH hDS; hDS = OGROpen(path.toStdString().c_str(), 0, NULL); if(hDS == NULL) { return -1; } int layerCount = OGR_DS_GetLayerCount(hDS); if (layerCount <= 0) { OGRReleaseDataSource( hDS ); return -1; } OGRLayerH layer = OGR_DS_GetLayer (hDS, 0); OGRwkbGeometryType geomType = OGR_L_GetGeomType(layer); // TODO: Might be a little naïve ... switch(geomType) { case wkbUnknown: ret = -1; break; case wkbPoint: case wkbMultiPoint: ret = MS_LAYER_POINT; break; case wkbLineString: case wkbMultiLineString: ret = MS_LAYER_LINE; break; case wkbPolygon: case wkbMultiPolygon: case wkbGeometryCollection: ret = MS_LAYER_POLYGON; break; default: ret = -1; } OGRReleaseDataSource(hDS); return ret; }
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 GDALDatasetH CreateOutputDataset(std::vector<OGRLayerH> ahLayers, OGRSpatialReferenceH hSRS, int bGotBounds, OGREnvelope sEnvelop, GDALDriverH hDriver, const char* pszDstFilename, int nXSize, int nYSize, double dfXRes, double dfYRes, int bTargetAlignedPixels, int nBandCount, GDALDataType eOutputType, char** papszCreateOptions, std::vector<double> adfInitVals, int bNoDataSet, double dfNoData) { int bFirstLayer = TRUE; char* pszWKT = NULL; GDALDatasetH hDstDS = NULL; unsigned int i; for( i = 0; i < ahLayers.size(); i++ ) { OGRLayerH hLayer = ahLayers[i]; if (!bGotBounds) { OGREnvelope sLayerEnvelop; if (OGR_L_GetExtent(hLayer, &sLayerEnvelop, TRUE) != OGRERR_NONE) { fprintf(stderr, "Cannot get layer extent\n"); exit(2); } /* When rasterizing point layers and that the bounds have */ /* not been explicitely set, voluntary increase the extent by */ /* a half-pixel size to avoid missing points on the border */ if (wkbFlatten(OGR_L_GetGeomType(hLayer)) == wkbPoint && !bTargetAlignedPixels && dfXRes != 0 && dfYRes != 0) { sLayerEnvelop.MinX -= dfXRes / 2; sLayerEnvelop.MaxX += dfXRes / 2; sLayerEnvelop.MinY -= dfYRes / 2; sLayerEnvelop.MaxY += dfYRes / 2; } if (bFirstLayer) { sEnvelop.MinX = sLayerEnvelop.MinX; sEnvelop.MinY = sLayerEnvelop.MinY; sEnvelop.MaxX = sLayerEnvelop.MaxX; sEnvelop.MaxY = sLayerEnvelop.MaxY; if (hSRS == NULL) hSRS = OGR_L_GetSpatialRef(hLayer); bFirstLayer = FALSE; } else { sEnvelop.MinX = MIN(sEnvelop.MinX, sLayerEnvelop.MinX); sEnvelop.MinY = MIN(sEnvelop.MinY, sLayerEnvelop.MinY); sEnvelop.MaxX = MAX(sEnvelop.MaxX, sLayerEnvelop.MaxX); sEnvelop.MaxY = MAX(sEnvelop.MaxY, sLayerEnvelop.MaxY); } } else { if (bFirstLayer) { if (hSRS == NULL) hSRS = OGR_L_GetSpatialRef(hLayer); bFirstLayer = FALSE; } } } if (dfXRes == 0 && dfYRes == 0) { dfXRes = (sEnvelop.MaxX - sEnvelop.MinX) / nXSize; dfYRes = (sEnvelop.MaxY - sEnvelop.MinY) / nYSize; } else if (bTargetAlignedPixels && dfXRes != 0 && dfYRes != 0) { sEnvelop.MinX = floor(sEnvelop.MinX / dfXRes) * dfXRes; sEnvelop.MaxX = ceil(sEnvelop.MaxX / dfXRes) * dfXRes; sEnvelop.MinY = floor(sEnvelop.MinY / dfYRes) * dfYRes; sEnvelop.MaxY = ceil(sEnvelop.MaxY / dfYRes) * dfYRes; } double adfProjection[6]; adfProjection[0] = sEnvelop.MinX; adfProjection[1] = dfXRes; adfProjection[2] = 0; adfProjection[3] = sEnvelop.MaxY; adfProjection[4] = 0; adfProjection[5] = -dfYRes; if (nXSize == 0 && nYSize == 0) { nXSize = (int)(0.5 + (sEnvelop.MaxX - sEnvelop.MinX) / dfXRes); nYSize = (int)(0.5 + (sEnvelop.MaxY - sEnvelop.MinY) / dfYRes); } hDstDS = GDALCreate(hDriver, pszDstFilename, nXSize, nYSize, nBandCount, eOutputType, papszCreateOptions); if (hDstDS == NULL) { fprintf(stderr, "Cannot create %s\n", pszDstFilename); exit(2); } GDALSetGeoTransform(hDstDS, adfProjection); if (hSRS) OSRExportToWkt(hSRS, &pszWKT); if (pszWKT) GDALSetProjection(hDstDS, pszWKT); CPLFree(pszWKT); int iBand; /*if( nBandCount == 3 || nBandCount == 4 ) { for(iBand = 0; iBand < nBandCount; iBand++) { GDALRasterBandH hBand = GDALGetRasterBand(hDstDS, iBand + 1); GDALSetRasterColorInterpretation(hBand, (GDALColorInterp)(GCI_RedBand + iBand)); } }*/ if (bNoDataSet) { for(iBand = 0; iBand < nBandCount; iBand++) { GDALRasterBandH hBand = GDALGetRasterBand(hDstDS, iBand + 1); GDALSetRasterNoDataValue(hBand, dfNoData); } } if (adfInitVals.size() != 0) { for(iBand = 0; iBand < MIN(nBandCount,(int)adfInitVals.size()); iBand++) { GDALRasterBandH hBand = GDALGetRasterBand(hDstDS, iBand + 1); GDALFillRaster(hBand, adfInitVals[iBand], 0); } } return hDstDS; }