我的问题是,使用 ogr2ogr,我将 shp 文件解析为 gml 文件。
然后我想在我的 C 函数中解析这个文件。
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");
当我执行程序时,由于未定义命名空间前缀(gml 或 ogr),每一行都有一个错误)
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>
你知道如何让程序知道这些新的命名空间吗?
编辑:
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;
}
`
添加您给我的内容时,我遇到了 Seg Fault,我真的不知道它来自哪里,但似乎我越来越接近答案了。
你知道我哪里错了吗?