int msContourLayerOpen(layerObj *layer) { char *decrypted_path; char szPath[MS_MAXPATHLEN]; contourLayerInfo *clinfo; if (layer->debug) msDebug("Entering msContourLayerOpen().\n"); /* If we don't have info, initialize an empty one now */ if (layer->layerinfo == NULL) msContourLayerInfoInitialize(layer); clinfo = (contourLayerInfo *) layer->layerinfo; GDALAllRegister(); /* Open the original Dataset */ msTryBuildPath3(szPath, layer->map->mappath, layer->map->shapepath, layer->data); decrypted_path = msDecryptStringTokens(layer->map, szPath); msAcquireLock(TLOCK_GDAL); if (decrypted_path) { clinfo->hOrigDS = GDALOpen(decrypted_path, GA_ReadOnly); msFree(decrypted_path); } else clinfo->hOrigDS = NULL; msReleaseLock(TLOCK_GDAL); if (clinfo->hOrigDS == NULL) { msSetError(MS_IMGERR, "Unable to open GDAL dataset.", "msContourLayerOpen()"); return MS_FAILURE; } /* Open the raster source */ if (msContourLayerReadRaster(layer, layer->map->extent) != MS_SUCCESS) return MS_FAILURE; /* Generate Contour Dataset */ if (msContourLayerGenerateContour(layer) != MS_SUCCESS) return MS_FAILURE; if (clinfo->hDS) { GDALClose(clinfo->hDS); clinfo->hDS = NULL; free(clinfo->buffer); } /* Open our virtual ogr layer */ if (clinfo->hOGRDS && (msLayerOpen(&clinfo->ogrLayer) != MS_SUCCESS)) return MS_FAILURE; return MS_SUCCESS; }
int msUVRASTERLayerGetExtent(layerObj *layer, rectObj *extent) { char szPath[MS_MAXPATHLEN]; mapObj *map = layer->map; double adfGeoTransform[6]; int nXSize, nYSize; GDALDatasetH hDS; shapefileObj *tileshpfile; int tilelayerindex = -1; CPLErr eErr = CE_Failure; char *decrypted_path; if( (!layer->data || strlen(layer->data) == 0) && layer->tileindex == NULL) { /* should we be issuing a specific error about not supporting extents for tileindexed raster layers? */ return MS_FAILURE; } if( map == NULL ) return MS_FAILURE; /* If the layer use a tileindex, return the extent of the tileindex shapefile/referenced layer */ if (layer->tileindex) { tilelayerindex = msGetLayerIndex(map, layer->tileindex); if(tilelayerindex != -1) /* does the tileindex reference another layer */ return msLayerGetExtent(GET_LAYER(map, tilelayerindex), extent); else { tileshpfile = (shapefileObj *) malloc(sizeof(shapefileObj)); MS_CHECK_ALLOC(tileshpfile, sizeof(shapefileObj), MS_FAILURE); if(msShapefileOpen(tileshpfile, "rb", msBuildPath3(szPath, map->mappath, map->shapepath, layer->tileindex), MS_TRUE) == -1) if(msShapefileOpen(tileshpfile, "rb", msBuildPath(szPath, map->mappath, layer->tileindex), MS_TRUE) == -1) return MS_FAILURE; *extent = tileshpfile->bounds; msShapefileClose(tileshpfile); free(tileshpfile); return MS_SUCCESS; } } msTryBuildPath3(szPath, map->mappath, map->shapepath, layer->data); decrypted_path = msDecryptStringTokens( map, szPath ); msAcquireLock( TLOCK_GDAL ); if( decrypted_path ) { hDS = GDALOpen(decrypted_path, GA_ReadOnly ); msFree( decrypted_path ); } else hDS = NULL; if( hDS != NULL ) { nXSize = GDALGetRasterXSize( hDS ); nYSize = GDALGetRasterYSize( hDS ); eErr = GDALGetGeoTransform( hDS, adfGeoTransform ); GDALClose( hDS ); } msReleaseLock( TLOCK_GDAL ); if( hDS == NULL || eErr != CE_None ) { return MS_FAILURE; } /* If this appears to be an ungeoreferenced raster than flip it for mapservers purposes. */ if( adfGeoTransform[5] == 1.0 && adfGeoTransform[3] == 0.0 ) { adfGeoTransform[5] = -1.0; adfGeoTransform[3] = nYSize; } extent->minx = adfGeoTransform[0]; extent->maxy = adfGeoTransform[3]; extent->maxx = adfGeoTransform[0] + nXSize * adfGeoTransform[1]; extent->miny = adfGeoTransform[3] + nYSize * adfGeoTransform[5]; return MS_SUCCESS; }
int msPOSTGRESQLJoinConnect(layerObj *layer, joinObj *join) { char *maskeddata, *temp, *sql, *column; char *conn_decrypted; int i, count, test; PGresult *query_result; msPOSTGRESQLJoinInfo *joininfo; if(join->joininfo) return MS_SUCCESS; joininfo = (msPOSTGRESQLJoinInfo *)malloc(sizeof(msPOSTGRESQLJoinInfo)); if(!joininfo) { msSetError(MS_MEMERR, "Error allocating join info struct.", "msPOSTGRESQLJoinConnect()"); return MS_FAILURE; } joininfo->conn = NULL; joininfo->row_num = 0; joininfo->query_result = NULL; joininfo->from_index = 0; joininfo->to_column = join->to; joininfo->from_value = NULL; joininfo->layer_debug = layer->debug; join->joininfo = joininfo; /* * We need three things at a minimum, the connection string, a table * name, and a column to join on. */ if(!join->connection) { msSetError(MS_QUERYERR, "No connection information provided.", "MSPOSTGRESQLJoinConnect()"); return MS_FAILURE; } if(!join->table) { msSetError(MS_QUERYERR, "No join table name found.", "msPOSTGRESQLJoinConnect()"); return MS_FAILURE; } if(!joininfo->to_column) { msSetError(MS_QUERYERR, "No join to column name found.", "msPOSTGRESQLJoinConnect()"); return MS_FAILURE; } /* Establish database connection */ conn_decrypted = msDecryptStringTokens(layer->map, join->connection); if (conn_decrypted != NULL) { joininfo->conn = PQconnectdb(conn_decrypted); free(conn_decrypted); } if(!joininfo->conn || PQstatus(joininfo->conn) == CONNECTION_BAD) { maskeddata = (char *)malloc(strlen(layer->connection) + 1); strcpy(maskeddata, join->connection); temp = strstr(maskeddata, "password="******" ") - temp); for(i = 0; i < count; i++) { strlcpy(temp, "*", (int)1); temp++; } } msSetError(MS_QUERYERR, "Unable to connect to PostgreSQL using the string %s.\n Error reported: %s\n", "msPOSTGRESQLJoinConnect()", maskeddata, PQerrorMessage(joininfo->conn)); free(maskeddata); if(!joininfo->conn) { free(joininfo->conn); } free(joininfo); join->joininfo = NULL; return MS_FAILURE; } /* Determine the number and names of columns in the join table. */ sql = (char *)malloc(36 + strlen(join->table) + 1); sprintf(sql, "SELECT * FROM %s WHERE false LIMIT 0", join->table); if(joininfo->layer_debug) { msDebug("msPOSTGRESQLJoinConnect(): executing %s.\n", sql); } query_result = PQexec(joininfo->conn, sql); if(!query_result || PQresultStatus(query_result) != PGRES_TUPLES_OK) { msSetError(MS_QUERYERR, "Error determining join items: %s.", "msPOSTGRESQLJoinConnect()", PQerrorMessage(joininfo->conn)); if(query_result) { PQclear(query_result); query_result = NULL; } free(sql); return MS_FAILURE; } free(sql); join->numitems = PQnfields(query_result); join->items = malloc(sizeof(char *) * (join->numitems)); /* We want the join-to column to be first in the list. */ test = 1; for(i = 0; i < join->numitems; i++) { column = PQfname(query_result, i); if(strcmp(column, joininfo->to_column) != 0) { join->items[i + test] = (char *)malloc(strlen(column) + 1); strcpy(join->items[i + test], column); } else { test = 0; join->items[0] = (char *)malloc(strlen(column) + 1); strcpy(join->items[0], column); } } PQclear(query_result); query_result = NULL; if(test == 1) { msSetError(MS_QUERYERR, "Unable to find join to column: %s", "msPOSTGRESQLJoinConnect()", joininfo->to_column); return MS_FAILURE; } if(joininfo->layer_debug) { for(i = 0; i < join->numitems; i++) { msDebug("msPOSTGRESQLJoinConnect(): Column %d named %s\n", i, join->items[i]); } } /* Determine the index of the join from column. */ for(i = 0; i < layer->numitems; i++) { if(strcasecmp(layer->items[i], join->from) == 0) { joininfo->from_index = i; break; } } if(i == layer->numitems) { msSetError(MS_JOINERR, "Item %s not found in layer %s.", "msPOSTGRESQLJoinConnect()", join->from, layer->name); return MS_FAILURE; } return MS_SUCCESS; }
int msContourLayerOpen(layerObj *layer) { char *decrypted_path; char szPath[MS_MAXPATHLEN]; contourLayerInfo *clinfo; if (layer->debug) msDebug("Entering msContourLayerOpen().\n"); /* If we don't have info, initialize an empty one now */ if (layer->layerinfo == NULL) msContourLayerInfoInitialize(layer); clinfo = (contourLayerInfo *) layer->layerinfo; if (layer->data == NULL && layer->tileindex == NULL ) { msSetError(MS_MISCERR, "Layer %s has neither DATA nor TILEINDEX defined.", "msContourLayerOpen()", layer->name); return MS_FAILURE; } if( layer->tileindex != NULL ) { char szTilename[MS_MAXPATHLEN]; int status; int tilelayerindex, tileitemindex, tilesrsindex; rectObj searchrect; layerObj* tlp; shapeObj tshp; char tilesrsname[1]; msInitShape(&tshp); searchrect = layer->map->extent; status = msDrawRasterSetupTileLayer(layer->map, layer, &searchrect, MS_FALSE, &tilelayerindex, &tileitemindex, &tilesrsindex, &tlp); if( status == MS_FAILURE ) { return MS_FAILURE; } status = msDrawRasterIterateTileIndex(layer, tlp, &tshp, tileitemindex, -1, szTilename, sizeof(szTilename), tilesrsname, sizeof(tilesrsname)); if( status == MS_FAILURE || status == MS_DONE ) { if( status == MS_DONE ) { if (layer->debug) msDebug("No raster matching filter.\n"); } msDrawRasterCleanupTileLayer(tlp, tilelayerindex); return MS_FAILURE; } msDrawRasterCleanupTileLayer(tlp, tilelayerindex); msDrawRasterBuildRasterPath(layer->map, layer, szTilename, szPath); decrypted_path = msStrdup(szPath); /* Cancel the time filter that might have been set on ours in case of */ /* a inline tileindex */ msFreeExpression(&layer->filter); msInitExpression(&layer->filter); } else { msTryBuildPath3(szPath, layer->map->mappath, layer->map->shapepath, layer->data); decrypted_path = msDecryptStringTokens(layer->map, szPath); } GDALAllRegister(); /* Open the original Dataset */ msAcquireLock(TLOCK_GDAL); if (decrypted_path) { clinfo->hOrigDS = GDALOpen(decrypted_path, GA_ReadOnly); msFree(decrypted_path); } else clinfo->hOrigDS = NULL; msReleaseLock(TLOCK_GDAL); if (clinfo->hOrigDS == NULL) { msSetError(MS_IMGERR, "Unable to open GDAL dataset.", "msContourLayerOpen()"); return MS_FAILURE; } /* Open the raster source */ if (msContourLayerReadRaster(layer, layer->map->extent) != MS_SUCCESS) return MS_FAILURE; /* Generate Contour Dataset */ if (msContourLayerGenerateContour(layer) != MS_SUCCESS) return MS_FAILURE; if (clinfo->hDS) { GDALClose(clinfo->hDS); clinfo->hDS = NULL; free(clinfo->buffer); } /* Open our virtual ogr layer */ if (clinfo->hOGRDS && (msLayerOpen(&clinfo->ogrLayer) != MS_SUCCESS)) return MS_FAILURE; return MS_SUCCESS; }
int msMySQLJoinConnect(layerObj *layer, joinObj *join) { #ifndef USE_MYSQL msSetError(MS_QUERYERR, "MySQL support not available (compile with --with-mysql)", "msMySQLJoinConnect()"); return(MS_FAILURE); #else int i; char qbuf[4000]; char *conn_decrypted; msMySQLJoinInfo *joininfo; MYDEBUG if (setvbuf(stdout, NULL, _IONBF , 0)){printf("Whoops...");}; if(join->joininfo) return(MS_SUCCESS); /* already open */ /* allocate a msMySQLJoinInfo struct */ joininfo = (msMySQLJoinInfo *) malloc(sizeof(msMySQLJoinInfo)); if(!joininfo) { msSetError(MS_MEMERR, "Error allocating mysql table info structure.", "msMySQLJoinConnect()"); return(MS_FAILURE); } /* initialize any members that won't get set later on in this function */ joininfo->qresult = NULL; joininfo->target = NULL; joininfo->nextrecord = 0; join->joininfo = joininfo; /* open the mysql connection */ if( join->connection == NULL ) { msSetError(MS_QUERYERR, "Error parsing MYSQL JOIN: nothing specified in CONNECTION statement.", "msMySQLJoinConnect()"); return(MS_FAILURE); } conn_decrypted = msDecryptStringTokens(layer->map, join->connection); if (conn_decrypted == NULL) { msSetError(MS_QUERYERR, "Error parsing MYSQL JOIN: unable to decrypt CONNECTION statement.", "msMySQLJoinConnect()"); return(MS_FAILURE); } delim = msStrdup(":"); DB_HOST = msStrdup(strtok(conn_decrypted, delim)); DB_USER = msStrdup(strtok(NULL, delim)); DB_PASSWD = msStrdup(strtok(NULL, delim)); DB_DATABASE = msStrdup(strtok(NULL, delim)); free(conn_decrypted); if (DB_HOST == NULL || DB_USER == NULL || DB_PASSWD == NULL || DB_DATABASE == NULL) { msSetError(MS_QUERYERR, "DB param error: at least one of HOST, USER, PASSWD or DATABASE is null!", "msMySQLJoinConnect()"); return MS_FAILURE; } if (strcmp(DB_PASSWD, "none") == 0) strcpy(DB_PASSWD, ""); #if MYSQL_VERSION_ID >= 40000 mysql_init(&(joininfo->mysql)); if (!(joininfo->conn = mysql_real_connect(&(joininfo->mysql),DB_HOST,DB_USER,DB_PASSWD,NULL, 0, NULL, 0))) #else if (!(joininfo->conn = mysql_connect(&(joininfo->mysql),DB_HOST,DB_USER,DB_PASSWD))) #endif { char tmp[4000]; snprintf( tmp, sizeof(tmp), "Failed to connect to SQL server: Error: %s\nHost: %s\nUsername:%s\nPassword:%s\n", mysql_error(joininfo->conn), DB_HOST, DB_USER, DB_PASSWD); msSetError(MS_QUERYERR, tmp, "msMYSQLLayerOpen()"); free(joininfo); return MS_FAILURE; } MYDEBUG printf("msMYSQLLayerOpen2 called<br>\n"); if (mysql_select_db(joininfo->conn,DB_DATABASE) < 0) { mysql_close(joininfo->conn); } MYDEBUG printf("msMYSQLLayerOpen3 called<br>\n"); if (joininfo->qresult != NULL) /* query leftover */ { MYDEBUG printf("msMYSQLLayerOpen4 called<br>\n"); mysql_free_result(joininfo->qresult); } MYDEBUG printf("msMYSQLLayerOpen5 called<br>\n"); snprintf(qbuf, sizeof(qbuf), "SELECT count(%s) FROM %s", join->to, join->table); MYDEBUG printf("%s<br>\n", qbuf); if ((joininfo->qresult = msMySQLQuery(qbuf, joininfo->conn))) /* There were some rows found, write 'em out for debug */ { int numrows = mysql_affected_rows(joininfo->conn); MYDEBUG printf("%d rows<br>\n", numrows); for(i=0;i<numrows;i++) { MYSQL_ROW row = mysql_fetch_row(joininfo->qresult); MYDEBUG printf("(%s)<BR>\n",row[0]); joininfo->rows = atoi(row[0]); } } else { msSetError(MS_DBFERR, "Item %s not found in table %s.", "msMySQLJoinConnect()", join->to, join->table); return(MS_FAILURE); } snprintf(qbuf, sizeof(qbuf), "EXPLAIN %s", join->table); if ((joininfo->qresult = msMySQLQuery(qbuf, joininfo->conn))) /* There were some rows found, write 'em out for debug */ { join->numitems = mysql_affected_rows(joininfo->conn); if((join->items = (char **)malloc(sizeof(char *)*join->numitems)) == NULL) { msSetError(MS_MEMERR, NULL, "msMySQLJoinConnect()"); return(MS_FAILURE); } MYDEBUG printf("%d rows<br>\n", join->numitems); for(i=0;i<join->numitems;i++) { MYSQL_ROW row = mysql_fetch_row(joininfo->qresult); MYDEBUG printf("(%s)<BR>\n",row[0]); join->items[i] = msStrdup(row[0]); } } else { msSetError(MS_DBFERR, "Item %s not found in table %s.", "msMySQLJoinConnect()", join->to, join->table); return(MS_FAILURE); } joininfo->tocolumn = msStrdup(join->to); /* get "from" item index */ for(i=0; i<layer->numitems; i++) { if(strcasecmp(layer->items[i],join->from) == 0) { /* found it */ joininfo->fromindex = i; break; } } if(i == layer->numitems) { msSetError(MS_JOINERR, "Item %s not found in layer %s.", "msMySQLJoinConnect()", join->from, layer->name); return(MS_FAILURE); } /* finally store away the item names in the XBase table */ if(!join->items) return(MS_FAILURE); return(MS_SUCCESS); #endif }