1

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?

chrki
  • 6,143
  • 6
  • 35
  • 55

1 Answers1

0

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);
James Holderness
  • 22,721
  • 2
  • 40
  • 52
  • In fact, the sed script I use is to delete the content because it seems that, when I try to parse the gml file, the first node become empty – user2550416 Jul 10 '13 at 11:57
  • Why does the temp.xml example you showed in your question still have `` if the sed script deleted it? – James Holderness Jul 10 '13 at 12:18
  • the Sed script delele the content of the `` In fact the first file contains : `` But it seems to work anyway despite the errors, I've waited for the execution to finish and the file created is a good one, but it'll be better without the errors. If somebody knows ? Thanks anyway James Holderness :) – user2550416 Jul 10 '13 at 12:51
  • I think you mean it deletes the attributes of the `FeatureCollection` element. And that is exactly what your problem is. It's probably ok to delete the `xmlns:xsi` attribute and `xsi:schemaLocation` if they aren't needed, but deleting the `xmlns:ogr` and `xmlns:gml` namespace attributes is the reason why you're getting namespace errors. Have you tried without that part of the sed script? – James Holderness Jul 10 '13 at 13:10
  • `doc = xmlParseFile("files/Extraction/temp.xml"); if (doc == NULL ) { return EXIT_FAILURE; } context = xmlXPathNewContext(doc); if (context == NULL) { return EXIT_FAILURE; } xpath = "//coordinates/text()"; result = xmlXPathEvalExpression(xpath, context); xmlXPathFreeContext(context); if (result == NULL) { return EXIT_FAILURE; } if(xmlXPathNodeSetIsEmpty(result->nodesetval)){ xmlXPathFreeObject(result); printf("No result\n"); return EXIT_FAILURE; }` That's what's after the code. If I try with the attributes, I'm getting an "No result" error. – user2550416 Jul 10 '13 at 13:20
  • Ps : I'm sorry for the code without the \n, I should mb reply with an answer instead of a comment .. – user2550416 Jul 10 '13 at 13:23
  • The reason you probably aren't getting any results from that query is that the xpath expression is trying to match an element without specifying the namespace. I'll update my answer with more details. – James Holderness Jul 10 '13 at 13:28
  • You are using the *context* variable (in the `xmlXPathRegisterNs` calls) before you've initialised it. You need to move those calls down in your code until after the line where you call `context = xmlXPathNewContext(doc);`. – James Holderness Jul 10 '13 at 14:08
  • When I'm moving down the calls, I get the same error that I had before (the namespace doesn't exist) ... :( – user2550416 Jul 10 '13 at 14:22
  • So the `xmlXPathRegisterNs` calls are after `xmlXPathNewContext` but before `xmlXPathEvalExpression`? And your temp.xml file has the `xmlns:ogr` and `xmlns:gml` attributes on the `FeatureCollection` element? And your xml also include the element `gml:coordinates` somewhere and that element is a text node (i.e. not child elements)? If all of that is true, it should work. I've just tried that code and it works for me. – James Holderness Jul 10 '13 at 14:59
  • It works ! I had forgotten to leave the xmlns attributes on the temp.xml file. Many thanks ! – user2550416 Jul 10 '13 at 15:13