<span class="gmail_quote"></span>Hello<br>
<br>
I'm trying to write to a PostGIS table with OGR, but it doesn't work. I
wrote a class in C++ which would iterate over all points (contained in (Multi)LineString objects) in a table/layer and
set a new z-Coordinate. When I look at the points after running the
program, the z-Coordinates are still the old value.<div><span class="e" id="q_1096969769b078d7_1"><br>
<br>
Does anyone know what I am doing wrong?<br>
<br>
Here's my code:<br>
<br>
#include "GISFeatures.h"<br>
<br>
using namespace std;<br>
<br>
GISFeatures::GISFeatures()<br>
{<br>
OGRRegisterAll();<br>
}<br>
<br>
GISFeatures::~GISFeatures()<br>
{<br>
}<br>
<br>
bool GISFeatures::prepare(string DBname, string tableN)<br>
{<br>
tableName = tableN;<br>
string connectionS = "PG:dbname=" + DBname;<br>
//string connectionS = "str.shp";<br>
data = OGRSFDriverRegistrar::Open( connectionS.c_str() , TRUE );<br>
<br>
if( data == NULL )<br>
{<br>
cerr <<
"Opening database \'" << DBname << "\' failed. Query: "
<< connectionS << endl;<br>
return false;<br>
}<br>
<br>
cout << "Opened database \'" << DBname << "\'." << endl;<br>
<br>
//string query = "SELECT
objectid, the_geom From " + tableName + " where the_geom &&
'BOX(66799 222000, 730000 274000)'::box2d;";
<br>
string query = "SELECT objectid,
the_geom From " + tableName + " where the_geom && 'BOX(682113
247230, 684600 250000)'::box2d;"; <br>
layer = data->ExecuteSQL( query.c_str() , NULL, "" );<br>
//layer = data->GetLayerByName("str");<br>
cout << "Executed query: " << query << endl;<br>
<br>
layer->ResetReading();<br>
<br>
return true;<br>
}<br>
<br>
bool GISFeatures::nextFeature()<br>
{<br>
geometryIdx = 0;<br>
pointIdx = 0;<br>
<br>
// Delete old feature if exists<br>
if(feature != NULL)<br>
{<br>
layer->SetFeature(feature);<br>
//OGRFeature::DestroyFeature( feature );<br>
}<br>
<br>
if( (feature = layer->GetNextFeature()) != NULL )<br>
{<br>
return true;<br>
}<br>
else<br>
{<br>
return false;<br>
}<br>
}<br>
<br>
bool GISFeatures::nextFeaturePoint()<br>
{<br>
geometry = feature->GetGeometryRef();<br>
if( geometry != NULL &&
wkbFlatten(geometry->getGeometryType()) == wkbMultiLineString )<br>
{<br>
OGRGeometryCollection *collection = (OGRGeometryCollection *) geometry;<br>
int numGeometries = collection->getNumGeometries();<br>
OGRGeometry* geometry2;<br>
while(true)<br>
{<br>
if(geometryIdx < numGeometries)<br>
{<br>
geometry2 =
collection->getGeometryRef(geometryIdx);<br>
if( geometry2 != NULL &&
wkbFlatten(geometry2->getGeometryType()) == wkbLineString )<br>
{
<br>
int
numPoints = ((OGRLineString*)geometry2)->getNumPoints();<br>
if(pointIdx < numPoints)<br>
{<br>
lsWithPoint = ((OGRLineString*)geometry2);<br>
pointIdx++;<br>
return true;<br>
}<br>
else<br>
{<br>
geometryIdx++;<br>
pointIdx = 0;<br>
}<br>
}<br>
else<br>
{<br>
geometryIdx++;<br>
pointIdx = 0;<br>
}
<br>
}<br>
else<br>
{<br>
//cout << " done with this
geometry " << endl;<br>
return false;<br>
}<br>
}<br>
<br>
}<br>
else<br>
{<br>
cout <<
"No collection geometry: " << geometry->getGeometryName()
<< endl;<br>
return false;<br>
}<br>
//return false; // Only with shapefile<br>
}<br>
<br>
double GISFeatures::pointX()<br>
{<br>
return lsWithPoint->getX(pointIdx-1);<br>
}<br>
<br>
double GISFeatures::pointY()<br>
{<br>
return lsWithPoint->getY(pointIdx-1);<br>
}<br>
<br>
double GISFeatures::pointZ()<br>
{<br>
return lsWithPoint->getZ(pointIdx-1);<br>
}<br>
<br>
void GISFeatures::setPointZ(double z)<br>
{<br>
cout << "Height of ("
<< pointX() << ", " << pointY() << ", "
<< pointZ() << ") to " << z;<br>
lsWithPoint->setPoint(pointIdx-1, pointX(), pointY(), z);<br>
cout << " -> (" <<
pointX() << ", " << pointY() << ", " <<
pointZ() << ")" << endl;<br>
}<br>
<br>
void GISFeatures::close()<br>
{<br>
if(data->SyncToDisk() != OGRERR_NONE)<br>
{<br>
cout << "Error while syncing data to disk." << endl;<br>
}<br>
data->ReleaseResultSet(layer);<br>
OGRDataSource::DestroyDataSource( data );<br>
cout << "Closed data source" << endl;<br>
}<br>
<br>
main.cpp:<br>
<br>
#include <iostream><br>
#include <sys/time.h><br>
<br>
#include "GISHeightModel.h"<br>
#include "GISFeatures.h";<br>
<br>
/*<br>
* Currently supports MultiLineStrings (i. e. streets)<br>
*/<br>
<br>
<br>
using namespace std;<br>
<br>
int main (int argc, char * const argv[]) {<br>
<br>
std::cout << "GIS Add Height Data Start\n";<br>
<br>
// Check for correct amount of arguments<br>
if(argc != 4)<br>
{<br>
printf("Usage: %s
<Height-Data> <Database-Name> <Table-Name>\n",
argv[0]);<br>
exit(1);<br>
}<br>
<br>
GISFeatures* features = new GISFeatures();<br>
features->prepare(argv[2], argv[3]);<br>
<br>
<br>
GISHeightModel* hm = new GISHeightModel();<br>
hm->open(argv[1]);<br>
hm->readFile();<br>
<br>
cout << "Calculating heights." << endl;<br>
<br>
while(features->nextFeature())<br>
{<br>
//cout << "F" << endl;<br>
while(features->nextFeaturePoint())<br>
{<br>
//cout << "P" << endl;<br>
features->setPointZ(hm->getHeight(features->pointX(),
features->pointY()));<br>
}<br>
}<br>
<br>
features->close();<br>
<br>
delete hm;<br>
delete features;<br>
<br>
return 0;<br>
}<br>
<br>
Thanks for your help,<br><span>
Philippe<br>
</span>
</span></div>