第一个C++类 geodatabase 转gml
头文件
#ifndef CLASS_GEOMETRYTOSTRING
#ifndef CLASS_GEOMETRYTOSTRING
#define CLASS_GEOMETRYTOSTRING
#include <iostream>
#include <FileGDBAPI.h>
using namespace std;
class GeometryToString
{
public:
static bool getGeometryStr( FileGDBAPI::ShapeBuffer &geometry,string srid,string &geometrystr);
private:
static void getPointGML(FileGDBAPI::PointShapeBuffer *&pbuffer,string srid,string dimension, string &xml);
static void getLineGML(FileGDBAPI::MultiPartShapeBuffer *&lineGeometry,string srid,string dimension, string &xml);
static void getMultipointGML(FileGDBAPI::MultiPointShapeBuffer *&multipointGeometry,string srid,string dimension, string &xml);
static void getPolygonGML(FileGDBAPI::MultiPartShapeBuffer *&polygonGeometry,string srid,string dimension, string &xml);
};
#endif
--------------------------------
--------------------------------
#include <jni.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <fstream>
#include <time.h>
#include <sstream>
#include <FileGDBAPI.h>
#include "GeometryToString.h"
using namespace FileGDBAPI;
using namespace std;
/**
ShapeBuffer geometry format to gml
*/
bool GeometryToString::getGeometryStr( ShapeBuffer &geometry,string srid,string &geometrystr)
{
ShapeType pType;
string dimension("2");
try{
geometry.GetShapeType(pType);
if(pType==shapeNull){
return false;
}
if(geometry.HasMs(pType) || geometry.HasZs(pType)){
dimension.assign("3");
}
//点
if(pType==shapePoint){
PointShapeBuffer *pPoint = static_cast<PointShapeBuffer*>(&geometry);
getPointGML(pPoint, srid, dimension, geometrystr);
}
//复杂点 shapeMultipoint = 8, shapeMultipointM = 28, shapeMultipointZM = 18, shapeMultipointZ = 20,
else if(pType==shapeMultipoint||pType==shapeMultipointZ){
MultiPointShapeBuffer *pPoint = static_cast<MultiPointShapeBuffer*>(&geometry);
getMultipointGML(pPoint, srid, dimension, geometrystr);
}
//线
else if(pType==shapePolyline||pType==shapePolylineZ){
MultiPartShapeBuffer *pPoint = static_cast<MultiPartShapeBuffer*>(&geometry);
getLineGML(pPoint, srid, dimension, geometrystr);
}
//面 shapePolygon = 5, shapePolygonM = 25, shapePolygonZM = 15, shapePolygonZ = 19,
else if(pType==shapePolygon||pType==shapePolygonZ){
MultiPartShapeBuffer *pPoint = static_cast<MultiPartShapeBuffer*>(&geometry);
getPolygonGML(pPoint, srid, dimension, geometrystr);
}
//geometrystr = xml;
}catch(exception& e){
cout <<"格式化gml出错"<< e.what() << '\n';
return false;
}
return true;
}
/**
<gml:Polygon srsName="SDO:4269" xmlns:gml="http://www.opengis.net/gml">
<gml:exterior>
<gml:LinearRing>
<gml:posList srsDimension="2">-73.2580598287651 42.7460586400617 -73.0196951738303 42.740396678634 -72.9229974292253 42.7373644761786 -72.4557700038866 42.7258525290856 -72.4621713056891 42.7468405310805 -72.4793225257782 42.7615879021406 -72.507269529053 42.768732690401 -72.513068008801 42.7892594013537 -72.5389169708409 42.8077338159122 -72.5534284639708 42.8606431711071 -72.5248100213574 42.912614176111 -72.5202170578447 42.951672527189 -72.5042636319544 42.9655846501007 -72.4733411974969 42.9761435837694 -72.4571590376321 42.9996036974295 -72.461752087004 43.0465044640799 -72.4434642578885 43.0790393128302 -72.4376049020671 43.1162700005077 -72.4523985528512 43.1560221784821 -72.4355986440692 43.2322535514026 -72.4024188454006 43.3073827061476 -72.4102315830492 43.3234041461304 -72.3976280543775 43.3510068532968 -72.4121395531362 43.3771255999364 -72.3962478080998 43.4101565518933 -72.3825156946812 43.4846296935806 -72.3949977682121 43.5175538931663 -72.3734983899535 43.5723746289273 -72.3330851941569 43.597364792188 -72.3040399378543 43.6985301192074 -72.2600555952028 43.7353001230665 -72.219122921336 43.7506925284353 -72.2060918209679 43.7646350589214 -72.1848363730122 43.80169046066 -72.1700895247441 43.8789176964692 -72.1216496397887 43.9092173247051 -72.1132040793556 43.9391659598858 -72.0917117306645 43.9579911279466 -72.1128078470402 43.9765149671878 -72.109908766997 43.9892291134735 -72.0852043813773 44.0089239861752 -72.0769190441221 44.0320405986229 -72.0347283650035 44.0833740182692 -72.0324473746587 44.0960996192242 -72.0495148341973 44.1004520944913 -72.034919852375 44.1207459288225 -72.0447245537617 44.1564355666161 -72.0592822460624 44.1821766291117 -72.0443903804218 44.2343798441307 -72.0595660047421 44.2614940911533 -72.0354953753776 44.2994343131499 -71.9944335108703 44.327548202346 -71.9389056579791 44.3257860034125 -71.9283617527138 44.3361121851129 -71.8348159803514 44.3441994129005 -71.821197308355 44.3503600453548 -71.7977291908463 44.3841728130012 -71.7665702593917 44.3982488046659 -71.6768843632127 44.4213427403399 -71.6563990024127 44.4401373612433 -71.6477091613881 44.4691741459765 -71.6365547217831 44.4767309013869 -71.6142227691161 44.4745070427354 -71.5866189807601 44.4945375694191 -71.5752435447921 44.5258056891543 -71.5914413886211 44.5388744007984 -71.5922884113102 44.5512031068491 -71.5367908177936 44.578931263059 -71.554102635183 44.5965889130363 -71.5680271516494 44.6374468081651 -71.588749347522 44.6505994869911 -71.6076787297883 44.6778622938611 -71.6311328527304 44.741710760694 -71.583501209059 44.779196995866 -71.5751009123659 44.8160197976273 -71.5063649605901 44.8996711859762 -71.5169776077168 44.9436961331566 -71.5409270967342 44.9765632062274 -71.5053723006288 45.0133517163227 -71.9018687560566 45.0073398737601 -72.547231170846 45.0053701041526 -73.1885457846918 45.0084861445147 -73.3447234868808 45.0061387945908 -73.3507583871194 44.9819729513452 -73.336414678892 44.9326039308501 -73.3823067594393 44.847933618761 -73.3690541280725 44.8191179021752 -73.3267863194035 44.7992935709543 -73.3731585750219 44.7242364367472 -73.3581509561493 44.6803685644813 -73.3730971364166 44.661276356252 -73.3701366913554 44.6343490646186 -73.3818251037205 44.619807725515 -73.3712960298211 44.5791669569445 -73.3478119840265 44.5539715457203 -73.3344524939974 44.5443282463014 -73.2933197444992 44.4328535783628 -73.2999951630005 44.4055331645411 -73.329788093029 44.3673904680867 -73.3053256664728 44.2601422576288 -73.3773326255291 44.2012475171298 -73.3820623364064 44.1721076120789 -73.4078648304615 44.1362270392698 -73.408756830709 44.1066103535608 -73.4352152780239 44.0638978024284 -73.4360007112789 44.0456791904392 -73.4082513023357 44.0182219013789 -73.4174061301201 43.9881969457531 -73.4053345287368 43.9148075869024 -73.3751207851313 43.8859769501208 -73.3847399017653 43.8045079717314 -73.35899716813 43.7784275686935 -73.3566696765928 43.7565583405993 -73.3709893845573 43.7142811167279 -73.4229598542952 43.6321147289768 -73.4183198417113 43.5824793859982 -73.38811421923 43.569143658301 -73.3636855615672 43.6149988679746 -73.303534516911 43.6247148128503 -73.2941043006646 43.6196528756939 -73.2817362682322 43.593187249577 -73.2914024969012 43.5750335705379 -73.2599837938072 43.5593823395161 -73.2383913589494 43.5128328494146 -73.2500714436228 43.3108539907423 -73.2760052890117 42.9402941192892 -73.2795831999318 42.8371033274803 -73.2961697572314 42.8035493647592 -73.2692753169001 42.7474814329983 -73.2580598287651 42.7460586400617 </gml:posList>
</gml:LinearRing>
</gml:exterior>
</gml:Polygon>
-----------------------------------------
<gml:MultiSurface srsName="SDO:4269" xmlns:gml="http://www.opengis.net/gml">
<gml:surfaceMember>
<gml:Polygon>
<gml:exterior>
<gml:LinearRing>
<gml:posList srsDimension="2">-71.7901942031213 41.6013068793251 -71.8027434308056 41.415829054006 -71.8459956537022 41.403854541649 -71.8368696812943 41.3419614666217 -71.8477722040922 41.3253484832966 -71.866678442895 41.3227696452717 -71.7222643227053 41.327264312184 -71.4898880400564 41.3920853196253 -71.427318519639 41.4866893796324 -71.4192468515382 41.6522122329241 -71.3690125475301 41.7032911019032 -71.3935805976545 41.7611558353254 -71.3673874451962 41.7413502009834 -71.2840016520154 41.6795489704365 -71.2289761591777 41.7076939683675 -71.2666285816006 41.7497430522049 -71.3193277902704 41.7721958646079 -71.33979862314 41.784425562696 -71.3454831662469 41.8131613833438 -71.3345427095385 41.8579036075386 -71.3424931202155 41.875782891498 -71.3330859502879 41.8960311596525 -71.3839531547034 41.8884397544728 -71.3824052822434 41.9792630768654 -71.3786442228911 42.0137133195164 -71.4974303691298 42.0092535031424 -71.7978316087618 42.0042748046853 -71.7882488621949 41.721603395324 -71.7926052182918 41.6417579304639 -71.7901942031213 41.6013068793251 </gml:posList>
</gml:LinearRing>
</gml:exterior>
</gml:Polygon>
</gml:surfaceMember>
<gml:surfaceMember>
<gml:Polygon>
<gml:exterior>
<gml:LinearRing>
<gml:posList srsDimension="2">-71.1988086806703 41.6785003452844 -71.1999371652607 41.4633184834308 -71.1171327154704 41.4930619563069 -71.1412126344661 41.6552730544526 -71.1988086806703 41.6785003452844 </gml:posList>
</gml:LinearRing>
</gml:exterior>
</gml:Polygon>
</gml:surfaceMember>
<gml:surfaceMember>
<gml:Polygon>
<gml:exterior>
<gml:LinearRing>
<gml:posList srsDimension="2">-71.2691694549114 41.6212683171835 -71.3495250332551 41.445857741455 -71.288007152861 41.4836193369167 -71.2386732340455 41.4748497781273 -71.2194468005559 41.6356423127122 -71.2691694549114 41.6212683171835 </gml:posList>
</gml:LinearRing>
</gml:exterior>
</gml:Polygon>
</gml:surfaceMember>
</gml:MultiSurface>
*/
void GeometryToString::getPolygonGML(FileGDBAPI::MultiPartShapeBuffer *&polygonGeometry,string srid,string dimension, string &xml){
int numPts;
polygonGeometry->GetNumPoints(numPts);
int numParts;
polygonGeometry->GetNumParts(numParts);
int* parts;
polygonGeometry->GetParts(parts);
Point* points;
polygonGeometry->GetPoints(points);
string gmlSub("");
if(numParts==1){
xml= "<gml:Polygon srsName=\"SDO:"+srid+"\" xmlns:gml=\"http://www.opengis.net/gml\"> \
<gml:exterior> \
<gml:LinearRing> \
<gml:posList srsDimension=\""+dimension+"\">";
for (int i = 0; i < numPts; i++)
{
char buffer[32];
sprintf(buffer, "%1.14g", points[i].x);
gmlSub.append(buffer).append(" ");
sprintf(buffer, "%1.14g", points[i].y);
gmlSub.append(buffer).append(" ");
}
xml.append(gmlSub);
xml.append("</gml:posList></gml:LinearRing>\
</gml:exterior>\
</gml:Polygon>");
return;
}
if(numParts>1){
xml="<gml:MultiSurface srsName=\"SDO:"+srid+"\" xmlns:gml=\"http://www.opengis.net/gml\">";
for(int i=0;i<numParts;i++){
gmlSub.append("<gml:surfaceMember>\
<gml:Polygon>\
<gml:exterior>\
<gml:LinearRing>\
<gml:posList srsDimension=\"").append(dimension).append("\">");
int gt=0;
if( i==(numParts-1)){
gt=numPts;
}else{
gt=parts[i+1];
}
for (int j= parts[i]; j < gt; j++){
char buffer[32];
sprintf(buffer, "%1.14g", points[j].x);
gmlSub.append(buffer).append(" ");
sprintf(buffer, "%1.14g", points[j].y);
gmlSub.append(buffer).append(" ");
}
gmlSub.append("</gml:posList>\
</gml:LinearRing>\
</gml:exterior>\
</gml:Polygon>\
</gml:surfaceMember>");
}
xml.append(gmlSub);
xml.append("</gml:MultiSurface>");
return;
}
}
/**
<gml:MultiPoint srsName="SDO:2230" xmlns:gml="http://www.opengis.net/gml">
<gml:pointMember>
<gml:Point>
<gml:posList srsDimension="3">6301153.87493073 1913012.75794829 435.0 </gml:posList>
</gml:Point>
</gml:pointMember>
<gml:pointMember>
<gml:Point>
<gml:posList srsDimension="3">6301172.62522131 1913331.50796697 424.0 </gml:posList>
</gml:Point>
</gml:pointMember>
</gml:MultiPoint>
*/
void GeometryToString::getMultipointGML(FileGDBAPI::MultiPointShapeBuffer *&multipointGeometry,string srid,string dimension, string &xml){
int numPts;
multipointGeometry->GetNumPoints(numPts);
wcout << "Multipoint test:" << endl;
wcout << "Points: " << numPts << endl;
Point* points;
multipointGeometry->GetPoints(points);
double* zArray;
multipointGeometry->GetZs(zArray);
xml= "<gml:MultiPoint srsName=\"SDO:"+srid+"\" xmlns:gml=\"http://www.opengis.net/gml\"> ";
string gmlSub("");
for (int i = 0; i < numPts; i++)
{
gmlSub.append("<gml:pointMember><gml:Point><gml:posList srsDimension=\""+dimension+"\">");
char buffer[32];
sprintf(buffer, "%1.14g", points[i].x);
gmlSub.append(buffer).append(" ");
sprintf(buffer, "%1.14g", points[i].y);
gmlSub.append(buffer).append(" ");
sprintf(buffer, "%1.14g", zArray[i]);
gmlSub.append(buffer).append(" ");
gmlSub.append("</gml:posList></gml:Point></gml:pointMember>");
}
xml.append(gmlSub);
xml.append("</gml:MultiPoint>");
}
/**
<gml:Curve srsName="SDO:4269" xmlns:gml="http://www.opengis.net/gml">
<gml:segments>
<gml:LineStringSegment>
<gml:posList srsDimension="2">-91.3082592401631 40.0073363232732 -91.3055891069314 39.995346341049 -91.3139124547158 39.9690362798168 -91.3254189367963 39.9469572142013 -91.3246098997585 39.9350861975012 -91.32260281264 39.9251371873939 -91.3238158572038 39.896847138931 -91.3220157795743 39.8891571322064 -91.3241598689896 39.8853761196319 -91.3383584637069 39.873997056967 -91.3394875098243 39.8690760451917 -91.3354593387828 39.8630950480564 -91.3031399738825 39.8280070993682 -91.3022619338156 39.8090860744066 -91.2985697775187 39.7987370721841 -91.3005688605598 39.7949870596141 -91.3073361425165 39.7886050259611 -91.3117993281738 39.783596002295 -91.3119823326406 39.7676969773468 -91.3108232836762 39.7648359772821 -91.3049330369361 39.7602849923819 -91.2990957929273 39.7579610108158 -91.1947826970924 39.7164273102698 </gml:posList>
</gml:LineStringSegment>
</gml:segments>
</gml:Curve>
<gml:MultiCurve srsName="SDO:4269" xmlns:gml="http://www.opengis.net/gml">
<gml:curveMember>
<gml:Curve>
<gml:segments>
<gml:LineStringSegment>
<gml:posList srsDimension="2"> sssss</gml:posList>
</gml:LineStringSegment>
</gml:segments>
</gml:Curve>
</gml:curveMember>
<gml:curveMember>
<gml:Curve>
<gml:segments>
<gml:LineStringSegment>
<gml:posList srsDimension="2">sssss </gml:posList>
</gml:LineStringSegment>
</gml:segments>
</gml:Curve>
</gml:curveMember>
</gml:MultiCurve>
*/
void GeometryToString::getLineGML(MultiPartShapeBuffer *&lineGeometry,string srid,string dimension, string &xml)
{
int numPts,numParts;
lineGeometry->GetNumPoints(numPts);
lineGeometry->GetNumParts(numParts);
int* parts;
lineGeometry->GetParts(parts);
Point* points;
lineGeometry->GetPoints(points);
string gmlSub("");
if(numParts==1){
xml= "<gml:Curve srsName=\"SDO:"+srid+"\" xmlns:gml=\"http://www.opengis.net/gml\"> \
<gml:segments> \
<gml:LineStringSegment> \
<gml:posList srsDimension=\""+dimension+"\">";
for (int i = 0; i < numPts; i++)
{
char buffer[32];
sprintf(buffer, "%1.14g", points[i].x);
gmlSub.append(buffer).append(" ");
sprintf(buffer, "%1.14g", points[i].y);
gmlSub.append(buffer).append(" ");
}
xml.append(gmlSub);
xml.append("</gml:posList> \
</gml:LineStringSegment>\
</gml:segments> \
</gml:Curve>");
return;
}
if(numParts>1){
xml="<gml:MultiCurve srsName=\"SDO:"+srid+"\" xmlns:gml=\"http://www.opengis.net/gml\">";
for(int i=0;i<numParts;i++){
gmlSub.append("<gml:curveMember>\
<gml:Curve>\
<gml:segments>\
<gml:LineStringSegment>\
<gml:posList srsDimension=\"").append(dimension).append("\">");
int gt=0;
if( i==(numParts-1)){
gt=numPts;
}else{
gt=parts[i+1];
}
for (int j= parts[i]; j < gt; j++){
char buffer[32];
sprintf(buffer, "%1.14g", points[j].x);
gmlSub.append(buffer).append(" ");
sprintf(buffer, "%1.14g", points[j].y);
gmlSub.append(buffer).append(" ");
}
gmlSub.append("</gml:posList>\
</gml:LineStringSegment>\
</gml:segments>\
</gml:Curve>\
</gml:curveMember>");
}
xml.append(gmlSub);
xml.append("</gml:MultiCurve>");
return;
}
}
/*
<gml:Point srsName="SDO:4269" xmlns:gml="http://www.opengis.net/gml">
<gml:posList srsDimension="2">-88.0261499418109 44.4722440557492 </gml:posList>
</gml:Point>
*/
void GeometryToString::getPointGML( PointShapeBuffer *&pbuffer,string srid,string dimension, string &xml)
{
xml= "<gml:Point srsName=\"SDO:"+srid+"\" xmlns:gml=\"http://www.opengis.net/gml\"><gml:posList srsDimension=\""+dimension+"\">";
Point* points;
pbuffer->GetPoint(points);
char buffer[32];
sprintf(buffer, "%1.14g", points[0].x);
xml.append(buffer).append(" ");
sprintf(buffer, "%1.14g", points[0].y);
xml.append(buffer).append(" ");
xml.append("</gml:posList></gml:Point>");
}