1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
|
unit UnitIGNWebServices;
// Interrogation du serveur d'altimétrie de l'IGN
// pour les altitudes: Z = f(Latitude, Longitude)
// Documentation
// https://geoservices.ign.fr/documentation/services/api-et-services-ogc/calcul-altimetrique-rest
// En GET:
// https://wxs.ign.fr/essentiels/alti/rest/elevation.xml?lon=-0.02500000&lat=43.08600000&zonly=false
// En POST
// https://wxs.ign.fr/essentiels/alti/wps?service=WPS&version=1.0.0
{$mode delphi} {$H+}
{$define IGN_DEBUG}
{$define USE_SYNAPSE40}
{$UNDEF USE_SYNAPSE40}
interface
uses
StructuresDonnees,
UnitListesSimplesWithGeneriques,
Classes,
SysUtils,
{$IFDEF USE_SYNAPSE40}
httpsend,
{$else}
fphttpclient,
{$endif}
DOM,
XMLRead, // pour XML
fpjson,jsonparser // pour JSON
;
// Ne pas oublier les libs OpenSSL à télécharger sur https://indy.fulgan.com/SSL/openssl-1.0.2q-i386-win32.zip
type
{ TMyIGNWebService }
TMyIGNWebService = class
private
FPOSTFormData: TStringList;
FResponse : TStringList;
{$IFDEF USE_SYNAPSE40}
FHTTP : THTTPSend;
{$else}
FHTTP : TFPHTTPClient;
{$endif}
FListePointsATraiter: TListePointsLatLonAltAcc;
procedure POST_AddAttributsEntreBalises(const Indent: integer; const K, V: string);
procedure POST_AddOWSIdentifier(const Indent: integer; const Name: string);
procedure POST_BeginLiteralData(const Indent: integer);
procedure POST_BeginWPSAction(const Indent: integer; const WPSAction: string);
procedure POST_BeginWPSInput(const Indent: integer; const AttribName: string);
procedure POST_EndLiteralData(const Indent: integer);
procedure POST_EndWPSAction(const Indent: integer; const WPSAction: string);
procedure POST_EndWPSInput(const Indent: integer; const AttribName: string);
function GetPoint(const Idx: integer): TPointLatLonAltAcc;
procedure QAfficherMessageErreur(const msg: string); inline;
procedure ClearAllHeaders();
procedure ClearAllRequestHeaders();
procedure AddAHeader(const H, V: string);
procedure AddARequestHeader(const RH: string);
function Get(const MyUrl: string): string;
public
function Initialiser(): boolean;
procedure Finaliser();
function GetAltitudeByLatLon(const QLat, QLon: double; out QAltitude, QAccuracy: double): boolean;
procedure AppendPointByLatLonAlt(const QLat, QLon: double; const QAlt: double; const QAcc: double = 0.00); overload;
procedure AppendPoint(const P: TPointLatLonAltAcc);
// Requete POST
function POST_BeginListePoints(): boolean;
function POST_Flush(): boolean;
function POST_TraiterReponseIGN(): boolean;
function GetNbPointsExtraits(): integer;
function GetPointTraite(const Idx: integer): TPointLatLonAltAcc;
function getNbRequestHeaders(): integer;
end;
implementation
uses
DGCDummyUnit,
Common,
Forms;
const
ALTITUDE_ERROR = -9999.0;
HTTP_ERR_OK = 200;
HTTP_WWW_OPENGIS_NET = 'http://www.opengis.net';
HTTP_SCHEMAS_OPENGIS_NET = 'http://schemas.opengis.net';
HTTP_WWW_W3C_ORG = 'http://www.w3.org';
IGN_WXS = 'wxs.ign.fr';
IGN_KEY = 'essentiels';
IGN_ALTI = 'alti';
IGN_REST = 'rest';
IGN_WPS = 'wps';
IGN_WFS = 'wfs';
IGN_WCS = 'wcs';
IGN_GML = 'gml';
IGN_OWS = 'ows';
IGN_OGC = 'ogc';
IGN_XLINK = 'xlink';
OWS_Identifier = 'Identifier';
WPS_Input = 'Input';
WPS_Data = 'Data';
WPS_LiteralData = 'LiteralData';
WPS_DataInputs = 'DataInputs';
WPS_RawDataOutput = 'RawDataOutput';
WPS_ResponseForm = 'ResponseForm';
IGN_SERVICE = 'service=WPS';
IGN_VERSION = '1.0.0';
IGN_APPLICATION = 'xml'; // xml ou json
IGN_ELEVATION = 'elevation.' + IGN_APPLICATION;
XMLNS = 'xmlns';
XML_IGN_SECTION_ELEVATIONS = 'elevations';
XML_IGN_ELEVATION_ITEM = 'elevation';
XML_IGN_LAT = 'lat';
XML_IGN_LON = 'lon';
XML_IGN_ALT = 'z';
XML_IGN_ACCURACY = 'acc';
{ TMyWebService }
procedure TMyIGNWebService.POST_AddAttributsEntreBalises(const Indent: integer; const K, V: string);
begin
FPOSTFormData.add(format('%s<%s>%s</%s>', [StringOfChar(' ', Indent), K, V, K]));
end;
procedure TMyIGNWebService.POST_AddOWSIdentifier(const Indent: integer; const Name: string);
const K = 'ows:Identifier';
begin
FPOSTFormData.add(format('%s<%s>%s</%s>', [StringOfChar(' ', Indent), K, Name, K]));
end;
procedure TMyIGNWebService.POST_BeginWPSAction(const Indent: integer; const WPSAction: string);
begin
// <wps:DataInputs>
FPOSTFormData.add(format('%s<wps:%s>', [StringOfChar(' ', Indent), WPSAction]));
end;
procedure TMyIGNWebService.POST_EndWPSAction(const Indent: integer; const WPSAction: string);
begin
FPOSTFormData.add(format('%s</wps:%s>', [StringOfChar(' ', Indent), WPSAction]));
end;
procedure TMyIGNWebService.POST_BeginWPSInput(const Indent: integer; const AttribName: string);
begin
POST_BeginWPSAction(Indent, WPS_Input); // <wps:Input>
POST_AddOWSIdentifier(Indent + 2, AttribName); // <ows:Identifier>lon</ows:Identifier>
POST_BeginWPSAction(Indent + 2, WPS_Data); // <wps:Data>
end;
procedure TMyIGNWebService.POST_EndWPSInput(const Indent: integer; const AttribName: string);
begin
POST_EndWPSAction(Indent + 2, WPS_Data); // </wps:Data>
POST_EndWPSAction(Indent, WPS_Input); // </wps:Input>
end;
procedure TMyIGNWebService.POST_BeginLiteralData(const Indent: integer);
begin
POST_BeginWPSAction(Indent, WPS_LiteralData);
end; // <wps:LiteralData>0.2367|2.1570</wps:LiteralData>
procedure TMyIGNWebService.POST_EndLiteralData(const Indent: integer);
begin
POST_EndWPSAction(Indent, WPS_LiteralData);
end;
function TMyIGNWebService.Initialiser(): boolean;
begin
AfficherMessageErreur(format('%s.Initialiser()', [classname]));
Result := false;
{ SSL initialization has to be done by hand here }
//InitSSLEAInterface(false);
FListePointsATraiter := TListePointsLatLonAltAcc.Create;
FPOSTFormData := TStringList.Create;
FResponse := TStringList.Create;
{$IFDEF USE_SYNAPSE40}
FHTTP := THTTPSend.Create;
{$else}
FHTTP := TFPHTTPClient.Create(Application);
{$endif}
try
FListePointsATraiter.ClearListe();
FPOSTFormData.Clear;
FResponse.Clear;
self.ClearAllHeaders();
self.AddAHeader('User-Agent','Mozilla/5.0 (compatible; fpweb)');
self.AddAHeader('Content-Type',Format('application/%s; charset=UTF-8', [IGN_APPLICATION]));
self.AddAHeader('Accept', 'application/%s');
{$IFDEF USE_SYNAPSE40}
pass;
{$else}
FHTTP.AllowRedirect := true;
{$endif}
Result := True;
except
end;
end;
function TMyIGNWebService.GetAltitudeByLatLon(const QLat, QLon: double; out QAltitude, QAccuracy: double): boolean;
var
MyURL, QResponse: String;
MyXML: TXMLDocument;
LastError, j: integer;
MyStringStream: TStringStream;
SectionElevations, MyElevation, QIN: TDOMNode;
LesElevations, LesAttributs: TDOMNodeList;
i, Nb, NbAttrs: LongWord;
begin
result := False;
LastError := 0;
QResponse := '';
QAltitude := -6666;
QAccuracy := 0.00;
MyXML := Nil;
MyURL := Format('https://%s/%s/%s/%s/%s?lon=%.8f&lat=%.8f&zonly=%s',
[IGN_WXS, IGN_KEY, IGN_ALTI, IGN_REST, IGN_ELEVATION,
QLon, QLat, BoolToStr(false, 'true', 'false')]);
MyXML := TXMLDocument.Create;
try // XML
try
QResponse := self.Get(MyURL);
if (trim(QResponse) = '') then
begin
QAfficherMessageErreur(format('Reponse vide: "%s"', [QResponse]));
exit(false);
end;
except
LastError := -1;
end;
if (LastError <> 0) then exit(false);
// Création du XML
QAfficherMessageErreur('-- Creation du XML');
MyStringStream := TStringStream.Create(QResponse);
Try //TStringStream
MyStringStream.Position := 0;
QAfficherMessageErreur(' -- Parse du XML');
ReadXMLFile(MyXML, MyStringStream); // Document XML complet
(*
<elevations>
<elevation>
<lon>-0.025</lon>
<lat>43.086</lat>
<z>554.11</z>
<acc>2.5</acc>
</elevation>
</elevations>
//*)
Finally
FreeAndNil(MyStringStream);
end; // TStringStream
QAfficherMessageErreur(' -- Extraction des data');
SectionElevations := MyXML.FindNode(XML_IGN_SECTION_ELEVATIONS);
QAfficherMessageErreur('---- 01: ' + SectionElevations.NodeName);
if (SectionElevations <> Nil) then
begin
LesElevations := SectionElevations.ChildNodes;
Nb := LesElevations.Count;
QAfficherMessageErreur(format('-- Section [%s] trouvee: %d items', [XML_IGN_SECTION_ELEVATIONS, Nb]));
if (Nb > 0) then
begin
for i := 0 to Nb - 1 do
begin
MyElevation := LesElevations.Item[i];
// ++ QAfficherMessageErreur(format('%d: %s', [i, MyElevation.NodeName]));
// attributs de la forme <key>value</key> = ce sont des noeuds
if (XML_IGN_ELEVATION_ITEM = MyElevation.NodeName) then
begin
LesAttributs := MyElevation.ChildNodes;
QAfficherMessageErreur(format('%d attributs', [LesAttributs.Count]));
(* +++
for j := 0 to LesAttributs.Count - 1 do
begin
AfficherMessageErreur(Format(' Attr: %d: %s = %s', [j, LesAttributs.Item[j].NodeName, LesAttributs.Item[j].TextContent]));
//4 attributs
//Attr: 0: lon = -0.025
//Attr: 1: lat = 43.086
//Attr: 2: z = 554.11
//Attr: 3: acc = 2.5
end;
// +++ *)
QAltitude := ConvertirEnNombreReel(LesAttributs.Item[2].TextContent, 0.00); // Attr: 2: z = 554.11
QAccuracy := ConvertirEnNombreReel(LesAttributs.Item[3].TextContent, 0.00); // Attr: 3: acc = 2.5
end;
end;
end;
end;
Result := true;
finally // XML
FreeAndNil(MyXML);
end;
end;
procedure TMyIGNWebService.AppendPointByLatLonAlt(const QLat, QLon: double; const QAlt: double; const QAcc: double = 0.00);
var
P: TPointLatLonAltAcc;
begin
P.setFrom(QLat, QLon, QAlt, QAcc);
FListePointsATraiter.AddElement(P);
end;
procedure TMyIGNWebService.AppendPoint(const P: TPointLatLonAltAcc);
begin
FListePointsATraiter.AddElement(P);
end;
//Documentation
// https://geoservices.ign.fr/documentation/services/api-et-services-ogc/calcul-altimetrique-rest
function TMyIGNWebService.POST_BeginListePoints(): boolean;
begin
result := false;
AfficherMessageErreur(Format('%s.POST_BeginListePoints()', [ClassName]));
FPOSTFormData.Clear;
FResponse.Clear;
//FHttpClient.AddHeader('Content-type', 'text/xml');
//nb :=
self.ClearAllRequestHeaders();
self.AddARequestHeader('User-Agent: Mozilla/5.0 (compatible; fpweb)');
self.AddARequestHeader('Content-Type:text/xml; charset=UTF-8');
self.AddARequestHeader('Accept:text/xml');
//FHttpClient.RequestHeaders.Add('Content-type:text/xml');
//FPOSTFormData.Add('Content-type:text/xml');
// <?xml version="1.0" encoding="UTF-8"?>
FPOSTFormData.Add(FormatterVersionEncodageXML('1.0', 'UTF-8'));
// <wps:Execute version="1.0.0" service="WPS"
FPOSTFormData.Add(Format('<wps:Execute version="%s" service="%s"', [IGN_VERSION, 'WPS']));
// xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
FPOSTFormData.Add(Format(' %s:xsi="%s/2001/XMLSchema-instance"', [XMLNS, HTTP_WWW_W3C_ORG]));
// xmlns="http://www.opengis.net/wps/1.0.0" xmlns:wfs="http://www.opengis.net/wfs"
FPOSTFormData.Add(Format(' %s="%s/%s/%s" xmlns:%s="%s/%s"', [XMLNS,
HTTP_WWW_OPENGIS_NET, IGN_WPS, IGN_VERSION,
IGN_WFS, HTTP_WWW_OPENGIS_NET, IGN_WFS]));
// xmlns:wps="http://www.opengis.net/wps/1.0.0"
FPOSTFormData.Add(Format(' %s:%s="%s/%s/%s"', [XMLNS,
IGN_WPS, HTTP_WWW_OPENGIS_NET, IGN_WPS, IGN_VERSION]));
//xmlns:ows="http://www.opengis.net/ows/1.1" xmlns:gml="http://www.opengis.net/gml"
FPOSTFormData.add(Format(' %s:%s="%s/%s/%s" xmlns:%s="%s/%s"', [XMLNS,
IGN_OWS, HTTP_WWW_OPENGIS_NET, IGN_OWS, '1.1',
IGN_GML, HTTP_WWW_OPENGIS_NET, IGN_GML]));
// xmlns:ogc="http://www.opengis.net/ogc"
FPOSTFormData.Add(Format(' %s:%s="%s/%s"', [XMLNS,
IGN_OGC, HTTP_WWW_OPENGIS_NET, IGN_OGC]));
// xmlns:wcs="http://www.opengis.net/wcs/1.1.1" xmlns:%s="%s/1999/xlink"
FPOSTFormData.add(Format(' %s:%s="%s/%s/%s" xmlns:%s="%s/%d/%s"', [XMLNS,
IGN_WCS , HTTP_WWW_OPENGIS_NET, IGN_WCS, '1.1.1',
IGN_XLINK, HTTP_WWW_W3C_ORG, 1999, IGN_XLINK]));
// xsi:schemaLocation="http://www.opengis.net/wps/1.0.0 http://schemas.opengis.net/wps/1.0.0/wps
FPOSTFormData.Add(Format(' xsi:schemaLocation="%s/%s/%s %s/%s/%s/wpsAll.xsd">', [
HTTP_WWW_OPENGIS_NET, IGN_WPS, IGN_VERSION,
HTTP_SCHEMAS_OPENGIS_NET, IGN_WPS, IGN_VERSION]));
// Section de données
POST_AddOWSIdentifier(2, 'gs:WPSLineElevation'); //gs:WPSElevation
result := True;
end;
function TMyIGNWebService.GetPoint(const Idx: integer): TPointLatLonAltAcc;
begin
result := FListePointsATraiter.GetElement(Idx);
end;
procedure TMyIGNWebService.QAfficherMessageErreur(const msg: string);
begin
{$ifdef IGN_DEBUG}AfficherMessageErreur('IGNService: ' + msg);{$endif}
end;
procedure TMyIGNWebService.ClearAllHeaders();
begin
{$IFDEF USE_SYNAPSE40}
FHTTP.Headers.Clear;
{$else}
FHTTP.RequestHeaders.Clear;
{$endif}
end;
procedure TMyIGNWebService.ClearAllRequestHeaders();
begin
{$IFDEF USE_SYNAPSE40}
{$else}
FHTTP.RequestHeaders.Clear;
{$endif}
end;
procedure TMyIGNWebService.AddAHeader(const H, V: string);
begin
{$IFDEF USE_SYNAPSE40}
FHTTP.Headers.Add(Format('%s:%s', [H, V]));
{$else}
FHTTP.AddHeader(H, V);
{$endif}
end;
procedure TMyIGNWebService.AddARequestHeader(const RH: string);
begin
{$IFDEF USE_SYNAPSE40}
FHTTP.Headers.Add(RH);
{$else}
FHTTP.RequestHeaders.Add(RH);
{$endif}
end;
function TMyIGNWebService.Get(const MyUrl: string): string;
var
QResults: TStringList;
begin
result := '';
{$IFDEF USE_SYNAPSE40}
QResults := TStringList.Create;
try
QResults.Clear;
if (HttpGetText(MyURL, QResults)) then
Result := QResults.Text;
finally
FreeAndNil(QResults);
end;
{$else}
result := FHTTP.Get(MyURL);
{$endif}
end;
function TMyIGNWebService.POST_Flush(): boolean;
var
Nb, i: Integer;
MyURL: String;
procedure MiouMiou(const Miou: boolean);
var
P: TPointLatLonAltAcc;
EWE, WU: String;
s: Integer;
begin
WU := IIF(Miou, 'lon', 'lat');
POST_BeginWPSInput(4, WU); // <wps:Input> \n <ows:Identifier>lon</ows:Identifier> \n <wps:Data>
POST_BeginLiteralData(8);
EWE := '';
for s := 0 to Nb-1 do
begin
P := self.GetPoint(s);
if (s > 0) then EWE += '|';
EWE += FormatterNombreWithDotDecimal(IIF(Miou, P.Longitude, P.Latitude), 8);
//++ if (Miou) then AfficherMessageErreur(Format('%d: Lat %.8f, Lon = %.8f, Alt = %.3f, Acc = %.3f', [s, P.Latitude, P.Longitude, P.Altitude, P.Accuracy]));
end;
FPOSTFormData.Add(' ' + EWE);
POST_EndLiteralData(8);
POST_EndWPSInput(4, WU); // </wps:Data> \n </wps:Input>
end;
procedure WPSInputLiteralDataWithOneParameter(const QIndent: integer; const QIdentifier, QParameter: string);
begin
POST_BeginWPSInput(QIndent, QIdentifier);
self.POST_AddAttributsEntreBalises(QIndent + 4, Format('%s:%s', [IGN_WPS, WPS_LiteralData]), QParameter);
POST_EndWPSInput(QIndent, QIdentifier);
end;
begin
result := false;
Nb := FListePointsATraiter.GetNbElements();
if (0 = Nb) then exit;
AfficherMessageErreur(Format('%s.POST_Flush(): %d points', [ClassName, Nb]));
POST_BeginWPSAction(2, WPS_DataInputs); // <wps:DataInputs>
if (Nb > 0) then
begin
MiouMiou(true);
MiouMiou(false);
end;
WPSInputLiteralDataWithOneParameter(4, 'sampling', Format('%d', [Nb]));
WPSInputLiteralDataWithOneParameter(4, 'format', 'json');
WPSInputLiteralDataWithOneParameter(4, 'indent', 'true');
POST_EndWPSAction(2, WPS_DataInputs); // </wps:DataInputs>
POST_BeginWPSAction(2, WPS_ResponseForm); // <wps:ResponseForm>
POST_BeginWPSAction(4, WPS_RawDataOutput); // <wps:RawDataOutput>
POST_AddAttributsEntreBalises(6, Format('%s.%s', [IGN_OWS, OWS_Identifier]), 'result'); //'ows:Identifier', 'result'); // <ows:Identifier>result</ows:Identifier>
POST_EndWPSAction(4, WPS_RawDataOutput); // </wps:RawDataOutput>
POST_EndWPSAction(2, WPS_ResponseForm); // </wps:ResponseForm>
POST_EndWPSAction(0, 'Execute'); // </wps:Execute>
AfficherMessageErreur('Contenu de la requete POST:');
MyURL := Format('https://%s/%s/%s/%s?%s&version=%s', [IGN_WXS, IGN_KEY, IGN_ALTI, IGN_WPS, IGN_SERVICE, IGN_VERSION]);
AfficherMessageErreur('-- Composé par GHTopo : ' + MyURL);
AfficherMessageErreur('-- Requis pour le POST : ' + 'https://wxs.ign.fr/essentiels/alti/wps?service=WPS&version=1.0.0'); // En POST
AfficherMessageErreur('Contenu request headers');
AfficherMessageErreur('===========================');
Nb := self.getNbRequestHeaders();
//++ if (Nb > 0) then for i := 0 to Nb-1 do AfficherMessageErreur(FHTTP.RequestHeaders.Strings[i]);
//++ AfficherMessageErreur('');
//++ AfficherMessageErreur('Contenu requête');
//++ AfficherMessageErreur('===========================');
//++ for i := 0 to FPOSTFormData.Count - 1 do AfficherMessageErreur(FPOSTFormData.Strings[i]);
//++ AfficherMessageErreur('===========================');
{$ifdef USE_SYNAPSE40}
{$else}
FHTTP.RequestBody := TStringStream.Create(FPOSTFormData.Text); // TRawByteStringStream
try
FHTTP.AllowRedirect := true;
FHTTP.Post(MyURL, FResponse);
AfficherMessageErreur(Format('-- > %d: "%s"', [FHTTP.ResponseStatusCode, FHTTP.ResponseStatusText]));
if (FHTTP.ResponseStatusCode <> HTTP_ERR_OK) then exit;
// Réponse du serveur
Nb := FResponse.Count;
//++ AfficherMessageErreur(Format('Code retour du serveur: %d; %d lignes retournees', [FHTTP.ResponseStatusCode, Nb]));
//++ AfficherMessageErreur('===================');
//++ for i := 0 to Nb-1 do AfficherMessageErreur(FResponse.Strings[i]);
//++ AfficherMessageErreur('===================');
//AfficherMessageErreur('Texte brut: ' + FResponse.Text);
result := True;
finally
FHTTP.RequestBody.Free;
end;
{$endif}
end;
function TMyIGNWebService.POST_TraiterReponseIGN(): boolean;
var
JsonData, jItem, jSubItem: TJSONData;
i, j, NbSubItems, NbFields: Integer;
MyPoint : TPointLatLonAltAcc;
begin
result := false;
AfficherMessageErreur(Format('%s.TraiterReponseIGN()', [ClassName]));
AfficherMessageErreur(FResponse.Text);
JsonData := GetJSON(FResponse.Text);
try
//s := JsonData.AsJSON;
// ici, on peut virer le contenu de la liste initiale
// on la recréera lors du parsing de la réponse JSON
FListePointsATraiter.ClearListe();
for i := 0 to JsonData.Count - 1 do
begin
jItem := JsonData.Items[i]; //++ AfficherMessageErreur(format('JSONItem%d: %s', [i, jItem.AsJSON]));
NbSubItems := jItem.Count; //++ AfficherMessageErreur(format('%d subitems', [NbSubItems]));
if (NbSubItems > 0) then
begin
for j := 0 to NbSubItems - 1 do
begin
jSubItem := jItem.Items[j];
NbFields := jSubItem.Count;
if (NbFields = 4) then
begin
MyPoint.Longitude := jSubItem.Items[0].AsFloat;
MyPoint.Latitude := jSubItem.Items[1].AsFloat;
MyPoint.Altitude := jSubItem.Items[2].AsFloat;
MyPoint.Accuracy := jSubItem.Items[3].AsFloat;
FListePointsATraiter.AddElement(MyPoint);
end;
end;
end; // if (NbSubItems > 0) then
end; // for i := 0 to JsonData.Count - 1 do
finally
FreeAndNil(JsonData); //It's necessary to free jData when you have finished with it. Otherwise a memory leak is created.
end;
// Test de sortie: au moins un élément
result := (FListePointsATraiter.GetNbElements() > 0);
// contrôle
AfficherMessageErreur(format('%d points valides', [FListePointsATraiter.GetNbElements()]));
(*
for i := 0 to FListePointsATraiter.GetNbElements() - 1 do
begin
MyPoint := self.GetPointTraite(i);//self.FListePointsATraiter.GetElement(i);
AfficherMessageErreur(Format('+++ %d: %s', [i, MyPoint.DebugString('')]));
end;
//*)
end;
function TMyIGNWebService.GetNbPointsExtraits(): integer;
begin
result := FListePointsATraiter.GetNbElements();
end;
function TMyIGNWebService.GetPointTraite(const Idx: integer): TPointLatLonAltAcc;
begin
result := FListePointsATraiter.GetElement(idx);
end;
function TMyIGNWebService.getNbRequestHeaders(): integer;
begin
{$ifdef USE_SYNAPSE40}
result := FHTTP.Headers.Count;
{$else}
FHTTP.RequestHeaders.Count;
{$endif}
end;
procedure TMyIGNWebService.Finaliser();
begin
try
FPOSTFormData.Clear;
FListePointsATraiter.ClearListe();
FResponse.Clear;
finally
FreeAndNil(FListePointsATraiter);
FreeAndNil(FPOSTFormData);
FreeAndNil(FResponse);
FreeAndNil(FHTTP);
end;
end;
end. |
Partager