Question

My problem is that, using ogr2ogr, I parse a shp file into a gml one.

Then I want to parse this file in my C function.

sprintf(buffer, "PATH=/Library/Frameworks/GDAL.framework/Programs:$PATH:/usr/local/bin ogr2ogr -f \"GML\" files/Extraction/coord.gml %s", lectureFichier);
system(buffer);

sprintf(buff, "sed \"2s/.*/\\<ogr:FeatureCollection\\>/\" files/Extraction/coord.gml | sed '3,6d' > files/Extraction/temp.xml");
system(buff);

FILE *fichier = NULL;
FILE *final = NULL;
fichier = fopen("files/Extraction/temporaire.csv", "w+");

xmlDocPtr doc;
xmlChar *xpath = (xmlChar*) "//keyword";
xmlNodeSetPtr nodeset;
xmlXPathContextPtr context;
xmlXPathObjectPtr result;
int i;

doc = xmlParseFile("files/Extraction/temp.xml");

When I execute the program, I have an error for every line because of the namespace prefix (gml or ogr) that are not defined)

Example of temp.xml

<ogr:FeatureCollection>
<gml:boundedBy>
<gml:Box>
<gml:coord><gml:X>847001.4933830451</gml:X><gml:Y>6298087.567566251</gml:Y></gml:coord>
<gml:coord><gml:X>859036.8755179688</gml:X><gml:Y>6309720.622619263</gml:Y></gml:coord>
</gml:Box>
</gml:boundedBy>                           
<gml:featureMember>

Do you have an idea of how to make the program know these new namespace?

EDIT:

xmlDocPtr doc;
xmlChar *xpath = (xmlChar*) "//keyword";
xmlNodeSetPtr nodeset;
xmlXPathContextPtr context;
xmlXPathRegisterNs(context, "ogr", "http://ogr.maptools.org/");
    xmlXPathRegisterNs(context, "gml", "http://www.opengis.net/gml");
xmlXPathObjectPtr result;   
int i;
doc = xmlParseFile("files/Extraction/temp.xml");
if (doc == NULL ) {
    fprintf(stderr,"Document not parsed successfully. \n");
    return 0;
}

context = xmlXPathNewContext(doc);
if (context == NULL) {
    printf("Error in xmlXPathNewContext\n");
    return 0;
}

xpath = "//gml:coordinates/text()";
result = xmlXPathEvalExpression(xpath, context);
xmlXPathFreeContext(context);
if (result == NULL) {
    printf("Error in xmlXPathEvalExpression\n");
    return 0;
}

if(xmlXPathNodeSetIsEmpty(result->nodesetval)){
    xmlXPathFreeObject(result);
    printf("No result\n");
    return 0;
}

`

When adding what you've given me, I'm having a Seg Fault and I really don't know where it's from, but it seems i'm getting closer to the answer.

Do you have an idea where I'm wrong?

Was it helpful?

Solution

I would think you just need to add the namespace declarations to the FeatureCollection element, so it looks like this:

<ogr:FeatureCollection
   xmlns:ogr="http://ogr.maptools.org/"
   xmlns:gml="http://www.opengis.net/gml">

You can assumedly do that in your sed script.

When trying to query namespaced elements with xpath you need to register your namespaces first. So you might need to do something like this:

xmlXPathRegisterNs(context, "ogr", "http://ogr.maptools.org/")
xmlXPathRegisterNs(context, "gml", "http://www.opengis.net/gml")

Then when you're trying to query a gml or ogr element, you would do so like this:

xpath = "//gml:coordinates/text()"; 
xmlXPathEvalExpression(xpath, context);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top