SAGA API  v9.2
shapes_io.cpp
Go to the documentation of this file.
1 
3 // //
4 // SAGA //
5 // //
6 // System for Automated Geoscientific Analyses //
7 // //
8 // Application Programming Interface //
9 // //
10 // Library: SAGA_API //
11 // //
12 //-------------------------------------------------------//
13 // //
14 // shapes_io.cpp //
15 // //
16 // Copyright (C) 2005 by Olaf Conrad //
17 // //
18 //-------------------------------------------------------//
19 // //
20 // This file is part of 'SAGA - System for Automated //
21 // Geoscientific Analyses'. //
22 // //
23 // This library is free software; you can redistribute //
24 // it and/or modify it under the terms of the GNU Lesser //
25 // General Public License as published by the Free //
26 // Software Foundation, either version 2.1 of the //
27 // License, or (at your option) any later version. //
28 // //
29 // This library is distributed in the hope that it will //
30 // be useful, but WITHOUT ANY WARRANTY; without even the //
31 // implied warranty of MERCHANTABILITY or FITNESS FOR A //
32 // PARTICULAR PURPOSE. See the GNU Lesser General Public //
33 // License for more details. //
34 // //
35 // You should have received a copy of the GNU Lesser //
36 // General Public License along with this program; if //
37 // not, see <http://www.gnu.org/licenses/>. //
38 // //
39 //-------------------------------------------------------//
40 // //
41 // contact: Olaf Conrad //
42 // Institute of Geography //
43 // University of Goettingen //
44 // Goldschmidtstr. 5 //
45 // 37077 Goettingen //
46 // Germany //
47 // //
48 // e-mail: oconrad@saga-gis.org //
49 // //
51 
52 //---------------------------------------------------------
53 #include "shapes.h"
54 #include "table_dbase.h"
55 #include "tool_library.h"
56 #include "data_manager.h"
57 
58 
60 // //
61 // //
62 // //
64 
65 //---------------------------------------------------------
67 {
68  return( Create(Get_File_Name(false)) );
69 }
70 
71 //---------------------------------------------------------
73 {
74  CSG_String File_Name = Get_File_Name(true);
75 
76  SG_File_Delete(File_Name);
77 
78  SG_File_Set_Extension(File_Name, "shp"); SG_File_Delete(File_Name); // shapes
79  SG_File_Set_Extension(File_Name, "shx"); SG_File_Delete(File_Name); // shape index
80  SG_File_Set_Extension(File_Name, "dbf"); SG_File_Delete(File_Name); // attributes
81  SG_File_Set_Extension(File_Name, "prj"); SG_File_Delete(File_Name); // projection
82  SG_File_Set_Extension(File_Name, "sbn"); SG_File_Delete(File_Name); // spatial index
83  SG_File_Set_Extension(File_Name, "sbx"); SG_File_Delete(File_Name); // spatial index
84  SG_File_Set_Extension(File_Name, "atx"); SG_File_Delete(File_Name); // attribute index
85  SG_File_Set_Extension(File_Name, "xml"); SG_File_Delete(File_Name); // metadata
86  SG_File_Set_Extension(File_Name, "cpg"); SG_File_Delete(File_Name); // code page
87  SG_File_Set_Extension(File_Name, "qix"); SG_File_Delete(File_Name); // quadtree spatial index
88 
89  return( true );
90 }
91 
92 
94 // //
95 // //
96 // //
98 
99 //---------------------------------------------------------
100 bool CSG_Shapes::_Load_GDAL(const CSG_String &File_Name)
101 {
102  CSG_Data_Manager Data;
103 
104  CSG_Tool *pTool = SG_Get_Tool_Library_Manager().Create_Tool("io_gdal", 3); // Import Shapes
105 
106  if( pTool )
107  {
108  if( pTool->Settings_Push(&Data) && pTool->Set_Parameter("FILES", File_Name, PARAMETER_TYPE_FilePath) )
109  {
110  SG_UI_Msg_Lock(true);
111  pTool->Execute();
112  SG_UI_Msg_Lock(false);
113  }
114 
116  }
117 
118  //-----------------------------------------------------
119  CSG_Shapes *pShapes = Data.Get_Shapes()->Count() ? Data.Get_Shapes()->Get(0)->asShapes() : NULL;
120 
121  if( !pShapes || !Create(*pShapes) )
122  {
123  return( false );
124  }
125 
126  Get_MetaData () = pShapes->Get_MetaData ();
127  Get_Projection() = pShapes->Get_Projection();
128 
129  //-----------------------------------------------------
130  if( SG_File_Cmp_Extension(File_Name, "gpkg" )
131  || SG_File_Cmp_Extension(File_Name, "GeoJSON") )
132  {
133  Set_File_Name(File_Name, true);
134  }
135  else
136  {
137  Set_File_Name(File_Name, false);
138  }
139 
140  return( true );
141 }
142 
143 //---------------------------------------------------------
144 bool CSG_Shapes::_Save_GDAL(const CSG_String &File_Name, const CSG_String &Driver)
145 {
146  bool bResult;
147 
148  SG_RUN_TOOL(bResult, "io_gdal", 4, // Export Shapes
149  SG_TOOL_PARAMETER_SET("SHAPES", this)
150  && SG_TOOL_PARAMETER_SET("FORMAT", Driver)
151  && SG_TOOL_PARAMETER_SET("FILE" , File_Name)
152  );
153 
154  return( bResult );
155 }
156 
157 
159 // //
160 // //
161 // //
163 
164 //---------------------------------------------------------
165 bool CSG_Shapes::_Load_ESRI(const CSG_String &File_Name)
166 {
167  int Type, iField, iPart, nParts, *Parts, iPoint, nPoints, iOffset;
168  double *pZ = NULL, *pM = NULL;
169  TSG_Point *pPoint;
170  CSG_Buffer File_Header(100), Record_Header(8), Content;
171  CSG_File fSHP;
172 
173  //-----------------------------------------------------
174  // Open DBase File...
175 
177 
178  if( fSHP.Open(SG_File_Make_Path("", File_Name, "cpg")) )
179  {
180  CSG_String sLine;
181 
182  if( fSHP.Read_Line(sLine) && sLine.Find("UTF-8") >= 0 )
183  {
185  }
186 
187  fSHP.Close();
188  }
189 
191 
192  if( !fDBF.Open_Read(SG_File_Make_Path("", File_Name, "dbf"), this, false) )
193  {
194  SG_UI_Msg_Add_Error(_TL("DBase file could not be opened."));
195 
196  return( false );
197  }
198 
199  fDBF.Move_First();
200 
201  if( fDBF.Get_Field_Count() < 1 )
202  {
203  SG_UI_Msg_Add_Error(_TL("DBase file does not contain any attribute fields."));
204 
205  return( false );
206  }
207 
208  //-----------------------------------------------------
209  // Open Shapes File...
210 
211  if( !fSHP.Open(SG_File_Make_Path("", File_Name, "shp"), SG_FILE_R, true) )
212  {
213  SG_UI_Msg_Add_Error(_TL("Shape file could not be opened."));
214 
215  return( false );
216  }
217 
218  //-----------------------------------------------------
219  // Read File Header (100 Bytes)...
220 
221  if( fSHP.Read(File_Header.Get_Data(), sizeof(char), 100) != 100 )
222  {
223  SG_UI_Msg_Add_Error(_TL("corrupted file header"));
224 
225  return( false );
226  }
227 
228  if( File_Header.asInt( 0, true) != 9994 ) // Byte 00 -> File Code 9994 (Integer Big)...
229  {
230  SG_UI_Msg_Add_Error(_TL("invalid file code"));
231 
232  return( false );
233  }
234 
235  if( File_Header.asInt(28, false) != 1000 ) // Byte 28 -> Version 1000 (Integer Little)...
236  {
237  SG_UI_Msg_Add_Error(_TL("unsupported file version"));
238 
239  return( false );
240  }
241 
242  switch( Type = File_Header.asInt(32) ) // Byte 32 -> Shape Type (Integer Little)...
243  {
244  case 1: m_Type = SHAPE_TYPE_Point; m_Vertex_Type = SG_VERTEX_TYPE_XY; break; // Point
245  case 8: m_Type = SHAPE_TYPE_Points; m_Vertex_Type = SG_VERTEX_TYPE_XY; break; // MultiPoint
246  case 3: m_Type = SHAPE_TYPE_Line; m_Vertex_Type = SG_VERTEX_TYPE_XY; break; // PolyLine
247  case 5: m_Type = SHAPE_TYPE_Polygon; m_Vertex_Type = SG_VERTEX_TYPE_XY; break; // Polygon
248 
249  case 11: m_Type = SHAPE_TYPE_Point; m_Vertex_Type = SG_VERTEX_TYPE_XYZM; break; // PointZ
250  case 18: m_Type = SHAPE_TYPE_Points; m_Vertex_Type = SG_VERTEX_TYPE_XYZM; break; // MultiPointZ
251  case 13: m_Type = SHAPE_TYPE_Line; m_Vertex_Type = SG_VERTEX_TYPE_XYZM; break; // PolyLineZ
252  case 15: m_Type = SHAPE_TYPE_Polygon; m_Vertex_Type = SG_VERTEX_TYPE_XYZM; break; // PolygonZ
253 
254  case 21: m_Type = SHAPE_TYPE_Point; m_Vertex_Type = SG_VERTEX_TYPE_XYZ; break; // PointM
255  case 28: m_Type = SHAPE_TYPE_Points; m_Vertex_Type = SG_VERTEX_TYPE_XYZ; break; // MultiPointM
256  case 23: m_Type = SHAPE_TYPE_Line; m_Vertex_Type = SG_VERTEX_TYPE_XYZ; break; // PolyLineM
257  case 25: m_Type = SHAPE_TYPE_Polygon; m_Vertex_Type = SG_VERTEX_TYPE_XYZ; break; // PolygonM
258 
259  default: // unsupported...
260  case 31: // unsupported: MultiPatch...
261  SG_UI_Msg_Add_Error(_TL("unsupported shape type."));
262 
263  return( false );
264  }
265 
266  //-----------------------------------------------------
267  // Load Shapes...
268 
269  for(int iShape=0; iShape<fDBF.Get_Count() && SG_UI_Process_Set_Progress(iShape, fDBF.Get_Count()); iShape++)
270  {
271  if( fSHP.Read(Record_Header.Get_Data(0), sizeof(int), 2) != 2 ) // read record header
272  {
273  SG_UI_Msg_Add_Error(_TL("corrupted record header"));
274 
275  return( false );
276  }
277 
278  if( Record_Header.asInt(0, true) != iShape + 1 ) // record number
279  {
280  SG_UI_Msg_Add_Error(CSG_String::Format("%s (%d != %d)", _TL("corrupted shapefile."), Record_Header.asInt(0, true), iShape + 1));
281 
282  return( false );
283  }
284 
285  size_t Length = 2 * Record_Header.asInt(4, true); // content length as 16-bit words !!!
286 
287  if( !Content.Set_Size(Length, false) )
288  {
289  SG_UI_Msg_Add_Error(_TL("memory allocation error."));
290 
291  return( false );
292  }
293 
294  if( fSHP.Read(Content.Get_Data(), sizeof(char), Length) != Length )
295  {
296  SG_UI_Msg_Add_Error(_TL("corrupted shapefile."));
297 
298  return( false );
299  }
300 
301  if( fDBF.isDeleted() )
302  {
303  continue; // nop
304  }
305 
306  if( Content.asInt(0) != Type )
307  {
308  if( Content.asInt(0) == 0 )
309  {
310  // null shape is allowed !!!
311  }
312  else
313  {
314  SG_UI_Msg_Add_Error(_TL("corrupted shapefile."));
315 
316  return( false );
317  }
318  }
319 
320  //-------------------------------------------------
321  else
322  {
323  CSG_Shape *pShape = Add_Shape();
324 
325  switch( m_Type )
326  {
327  default: break;
328 
329  //---------------------------------------------
330  case SHAPE_TYPE_Point:
331 
332  pPoint = (TSG_Point *)Content.Get_Data(4);
333 
334  pShape->Add_Point(pPoint->x, pPoint->y);
335 
336  switch( m_Vertex_Type ) // read Z + M
337  {
338  default: break;
339  case SG_VERTEX_TYPE_XYZM: pShape->Set_M(Content.asDouble(28), 0);
340  case SG_VERTEX_TYPE_XYZ : pShape->Set_Z(Content.asDouble(20), 0);
341  }
342 
343  break;
344 
345  //---------------------------------------------
346  case SHAPE_TYPE_Points:
347 
348  nPoints = Content.asInt(36);
349  pPoint = (TSG_Point *)Content.Get_Data(40);
350 
351  switch( m_Vertex_Type ) // read Z + M
352  {
353  default:
354  break;
355 
356  case SG_VERTEX_TYPE_XYZ:
357  pZ = 56 + nPoints * 24 <= (int)Length ? (double *)Content.Get_Data(56 + nPoints * 16) : NULL; // [40 + nPoints * 16 + 2 * 8] + [nPoints * 8]
358  break;
359 
360  case SG_VERTEX_TYPE_XYZM:
361  pZ = 56 + nPoints * 24 <= (int)Length ? (double *)Content.Get_Data(56 + nPoints * 16) : NULL; // [40 + nPoints * 16 + 2 * 8] + [nPoints * 8]
362  pM = 72 + nPoints * 32 <= (int)Length ? (double *)Content.Get_Data(72 + nPoints * 24) : NULL; // [40 + nPoints * 16 + 2 * 8] + [nPoints * 8 + 2 * 8] + [nPoints * 8]
363  break;
364  }
365 
366  //-----------------------------------------
367  for(iPoint=0; iPoint<nPoints; iPoint++, pPoint++)
368  {
369  pShape->Add_Point(pPoint->x, pPoint->y);
370 
371  if( pZ ) { pShape->Set_Z(*(pZ++), iPoint); }
372  if( pM ) { pShape->Set_M(*(pM++), iPoint); }
373  }
374 
375  break;
376 
377  //---------------------------------------------
378  case SHAPE_TYPE_Line :
379  case SHAPE_TYPE_Polygon:
380 
381  nParts = Content.asInt(36);
382  nPoints = Content.asInt(40);
383  Parts = (int *)Content.Get_Data(44);
384  pPoint = (TSG_Point *)Content.Get_Data(44 + 4 * nParts);
385 
386  switch( m_Vertex_Type ) // read Z + M
387  {
388  default:
389  break;
390 
391  case SG_VERTEX_TYPE_XYZ:
392  pZ = 60 + nParts * 4 + nPoints * 24 <= (int)Length ? (double *)Content.Get_Data(60 + nParts * 4 + nPoints * 16) : NULL; // [44 + nParts * 4 + nPoints * 16 + 2 * 8] + [nPoints * 8]
393  break;
394 
395  case SG_VERTEX_TYPE_XYZM:
396  pZ = 60 + nParts * 4 + nPoints * 24 <= (int)Length ? (double *)Content.Get_Data(60 + nParts * 4 + nPoints * 16) : NULL; // [44 + nParts * 4 + nPoints * 16 + 2 * 8] + [nPoints * 8]
397  pM = 76 + nParts * 4 + nPoints * 32 <= (int)Length ? (double *)Content.Get_Data(76 + nParts * 4 + nPoints * 24) : NULL; // [44 + nParts * 4 + nPoints * 16 + 2 * 8] + [nPoints * 8 + 2 * 8] + [nPoints * 8]
398  break;
399  }
400 
401  //-----------------------------------------
402  iOffset = 0;
403 
404  for(iPoint=0, iPart=0; iPoint<nPoints; iPoint++, pPoint++)
405  {
406  if( iPart < nParts - 1 && iPoint >= Parts[iPart + 1] )
407  {
408  iPart++;
409  iOffset = 0;
410  }
411 
412  pShape->Add_Point(pPoint->x, pPoint->y, iPart);
413 
414  if( pZ ) { pShape->Set_Z(*(pZ++), iOffset, iPart); }
415  if( pM ) { pShape->Set_M(*(pM++), iOffset, iPart); }
416 
417  iOffset++;
418  }
419 
420  break;
421  }
422 
423  //---------------------------------------------
424  for(iField=0; iField<Get_Field_Count(); iField++)
425  {
426  switch( fDBF.Get_Field_Type(iField) )
427  {
428  default:
429  pShape->Set_Value(iField, fDBF.asString(iField));
430  break;
431 
432  case DBF_FT_FLOAT:
433  case DBF_FT_NUMERIC:
434  {
435  double Value;
436 
437  if( fDBF.asDouble(iField, Value) )
438  {
439  pShape->Set_Value(iField, Value);
440  }
441  else
442  {
443  pShape->Set_NoData(iField);
444  }
445  }
446  break;
447  }
448  }
449  }
450 
451  fDBF.Move_Next();
452  }
453 
454  //-----------------------------------------------------
455  Get_Projection().Load(SG_File_Make_Path("", File_Name, "prj"), SG_PROJ_FMT_WKT);
456 
457  //-----------------------------------------------------
458  Load_MetaData(File_Name);
459 
460  CSG_MetaData *pFields = Get_MetaData_DB().Get_Child("FIELDS");
461 
462  if( pFields && pFields->Get_Children_Count() == Get_Field_Count() )
463  {
464  for(iField=0; iField<Get_Field_Count(); iField++)
465  {
466  Set_Field_Name(iField, pFields->Get_Content(iField));
467  }
468  }
469 
470  Set_File_Name(File_Name, true);
471 
472  //-----------------------------------------------------
473  return( true );
474 }
475 
476 
478 // //
479 // //
480 // //
482 
483 //---------------------------------------------------------
484 #define Set_Content_Length(n) Record_Header.Set_Value(4, (int)(n), true);\
485  fSHP.Write(Record_Header.Get_Data(), sizeof(int), 2);\
486  fSHX.Write_Int(fSHP_Size, true);\
487  fSHX.Write_Int((n) , true);\
488  fSHP_Size += 4 + (n);\
489  fSHX_Size += 4;
490 
491 //---------------------------------------------------------
492 bool CSG_Shapes::_Save_ESRI(const CSG_String &File_Name)
493 {
494  int Type, fSHP_Size, fSHX_Size, iField, iPart, iPoint, nPoints;
495  TSG_Point Point;
496  CSG_Buffer File_Header(100), Record_Header(8), Content;
497  CSG_File fSHP, fSHX;
498 
499  //-----------------------------------------------------
500  // Determine Shape Type...
501 
502  switch( m_Type )
503  {
504  case SHAPE_TYPE_Point : Type = 1; break;
505  case SHAPE_TYPE_Points : Type = 8; break;
506  case SHAPE_TYPE_Line : Type = 3; break;
507  case SHAPE_TYPE_Polygon : Type = 5; break;
508  default: return( false );
509  }
510 
512 
513  switch( Vertex_Type )
514  {
515  case SG_VERTEX_TYPE_XY : break;
516  case SG_VERTEX_TYPE_XYZ : Type += 20; break; // M
517  case SG_VERTEX_TYPE_XYZM: Type += 10; break; // Z (+M)
518  default: return( false );
519  }
520 
521  //-----------------------------------------------------
522  // DBase File Access...
523 
524  SG_File_Delete(SG_File_Make_Path("", File_Name, "cpg"));
525 
527  {
528  if( fSHP.Open(SG_File_Make_Path("", File_Name, "cpg"), SG_FILE_W, false) )
529  {
530  fSHP.Printf("UTF-8\n");
531 
532  fSHP.Close();
533  }
534  }
535 
537 
538  if( !fDBF.Open_Write(SG_File_Make_Path("", File_Name, "dbf"), this, false) )
539  {
540  return( false );
541  }
542 
543  //-----------------------------------------------------
544  // Shape File Access...
545 
546  if( !fSHX.Open(SG_File_Make_Path("", File_Name, "shx"), SG_FILE_W, true) )
547  {
548  SG_UI_Msg_Add_Error(_TL("index file could not be opened"));
549 
550  return( false );
551  }
552 
553  if( !fSHP.Open(SG_File_Make_Path("", File_Name, "shp"), SG_FILE_W, true) )
554  {
555  SG_UI_Msg_Add_Error(_TL("shape file could not be opened."));
556 
557  return( false );
558  }
559 
560  //-----------------------------------------------------
561  // Save Header...
562 
563  Make_Clean(); // polygons: first == last point, inner rings > anti-clockwise...
564 
565  Update();
566 
567  File_Header.Set_Value( 0, 9994 , true ); // Byte 0 Integer Big File Code = 9994
568  File_Header.Set_Value( 4, 0 , true ); // Byte 4 Integer Big unused
569  File_Header.Set_Value( 8, 0 , true ); // Byte 8 Integer Big unused
570  File_Header.Set_Value(12, 0 , true ); // Byte 12 Integer Big unused
571  File_Header.Set_Value(16, 0 , true ); // Byte 16 Integer Big unused
572  File_Header.Set_Value(20, 0 , true ); // Byte 20 Integer Big unused
573  File_Header.Set_Value(24, 0 , true ); // Byte 24 Integer Big File Length
574  File_Header.Set_Value(28, 1000 , false); // Byte 28 Integer Little Version = 1000
575  File_Header.Set_Value(32, Type , false); // Byte 32 Integer Little Shape Type
576  File_Header.Set_Value(36, m_Extent.Get_XMin() , false); // Byte 36 Double Little Bounding Box Xmin
577  File_Header.Set_Value(44, m_Extent.Get_YMin() , false); // Byte 44 Double Little Bounding Box Ymin
578  File_Header.Set_Value(52, m_Extent.Get_XMax() , false); // Byte 52 Double Little Bounding Box Xmax
579  File_Header.Set_Value(60, m_Extent.Get_YMax() , false); // Byte 60 Double Little Bounding Box Ymax
580  File_Header.Set_Value(68, Get_ZMin() , false); // Byte 68 Double Little Bounding Box Zmin
581  File_Header.Set_Value(76, Get_ZMax() , false); // Byte 76 Double Little Bounding Box Zmax
582  File_Header.Set_Value(84, Get_MMin() , false); // Byte 84 Double Little Bounding Box Mmin
583  File_Header.Set_Value(92, Get_MMax() , false); // Byte 92 Double Little Bounding Box Mmax
584 
585  fSHP.Write(File_Header.Get_Data(), sizeof(char), 100);
586  fSHX.Write(File_Header.Get_Data(), sizeof(char), 100);
587 
588  fSHP_Size = 50; // file size measured in 16-bit words...
589  fSHX_Size = 50; // file size measured in 16-bit words...
590 
591  //-----------------------------------------------------
592  // Save Shapes...
593 
594  for(sLong iShape=0; iShape<Get_Count() && SG_UI_Process_Set_Progress(iShape, Get_Count()); iShape++)
595  {
596  CSG_Shape *pShape = Get_Shape(iShape);
597 
598  //-------------------------------------------------
599  // geometries...
600 
601  Record_Header.Set_Value(0, (int)iShape + 1, true); // record number
602 
603  for(iPart=0, nPoints=0; iPart<pShape->Get_Part_Count(); iPart++)
604  {
605  nPoints += pShape->Get_Point_Count(iPart); // total number of points in shape
606  }
607 
608  //-------------------------------------------------
609  switch( m_Type ) // write content header
610  {
611  default: break;
612 
613  //-------------------------------------------------
614  case SHAPE_TYPE_Point:
615 
616  switch( Vertex_Type )
617  {
618  case SG_VERTEX_TYPE_XY : Set_Content_Length(10); break;
619  case SG_VERTEX_TYPE_XYZ : Set_Content_Length(14); break;
620  case SG_VERTEX_TYPE_XYZM: Set_Content_Length(18); break;
621  }
622 
623  fSHP.Write_Int (Type);
624 
625  break;
626 
627  //-------------------------------------------------
628  case SHAPE_TYPE_Points:
629 
630  switch( Vertex_Type )
631  {
632  case SG_VERTEX_TYPE_XY : Set_Content_Length(20 + 8 * nPoints); break;
633  case SG_VERTEX_TYPE_XYZ : Set_Content_Length(28 + 12 * nPoints); break;
634  case SG_VERTEX_TYPE_XYZM: Set_Content_Length(36 + 16 * nPoints); break;
635  }
636 
637  fSHP.Write_Int (Type);
638  fSHP.Write_Double (pShape->Get_Extent().Get_XMin());
639  fSHP.Write_Double (pShape->Get_Extent().Get_YMin());
640  fSHP.Write_Double (pShape->Get_Extent().Get_XMax());
641  fSHP.Write_Double (pShape->Get_Extent().Get_YMax());
642  fSHP.Write_Int (nPoints);
643 
644  break;
645 
646  //-------------------------------------------------
647  case SHAPE_TYPE_Line:
648  case SHAPE_TYPE_Polygon:
649 
650  switch( Vertex_Type )
651  {
652  case SG_VERTEX_TYPE_XY: Set_Content_Length(22 + 2 * pShape->Get_Part_Count() + 8 * nPoints); break;
653  case SG_VERTEX_TYPE_XYZ: Set_Content_Length(30 + 2 * pShape->Get_Part_Count() + 12 * nPoints); break;
654  case SG_VERTEX_TYPE_XYZM: Set_Content_Length(38 + 2 * pShape->Get_Part_Count() + 16 * nPoints); break;
655  }
656 
657  fSHP.Write_Int (Type);
658  fSHP.Write_Double (pShape->Get_Extent().Get_XMin());
659  fSHP.Write_Double (pShape->Get_Extent().Get_YMin());
660  fSHP.Write_Double (pShape->Get_Extent().Get_XMax());
661  fSHP.Write_Double (pShape->Get_Extent().Get_YMax());
662  fSHP.Write_Int (pShape->Get_Part_Count());
663  fSHP.Write_Int (nPoints);
664 
665  for(iPart=0, iPoint=0; iPart<pShape->Get_Part_Count(); iPoint+=pShape->Get_Point_Count(iPart++))
666  {
667  fSHP.Write_Int(iPoint);
668  }
669 
670  break;
671  }
672 
673  //-------------------------------------------------
674  switch( m_Type ) // write point data
675  {
676  default: break;
677 
678  //-------------------------------------------------
679  case SHAPE_TYPE_Point:
680 
681  fSHP.Write(&(Point = pShape->Get_Point()), sizeof(TSG_Point));
682 
683  //---------------------------------------------
684  if( Vertex_Type != SG_VERTEX_TYPE_XY )
685  {
686  fSHP.Write_Double(pShape->Get_Z(0));
687 
688  if( Vertex_Type == SG_VERTEX_TYPE_XYZM )
689  {
690  fSHP.Write_Double(pShape->Get_M(0));
691  }
692  }
693 
694  break;
695 
696  //-------------------------------------------------
697  case SHAPE_TYPE_Points:
698  case SHAPE_TYPE_Line:
699  case SHAPE_TYPE_Polygon:
700 
701  for(iPart=0; iPart<pShape->Get_Part_Count(); iPart++)
702  {
703  for(iPoint=0; iPoint<pShape->Get_Point_Count(iPart); iPoint++)
704  {
705  fSHP.Write(&(Point = pShape->Get_Point(iPoint, iPart)), sizeof(TSG_Point));
706  }
707  }
708 
709  //---------------------------------------------
710  if( Vertex_Type != SG_VERTEX_TYPE_XY )
711  {
712  fSHP.Write_Double(pShape->Get_ZMin());
713  fSHP.Write_Double(pShape->Get_ZMax());
714 
715  for(iPart=0; iPart<pShape->Get_Part_Count(); iPart++)
716  {
717  for(iPoint=0; iPoint<pShape->Get_Point_Count(iPart); iPoint++)
718  {
719  fSHP.Write_Double(pShape->Get_Z(iPoint, iPart));
720  }
721  }
722 
723  if( Vertex_Type == SG_VERTEX_TYPE_XYZM )
724  {
725  fSHP.Write_Double(pShape->Get_MMin());
726  fSHP.Write_Double(pShape->Get_MMax());
727 
728  for(iPart=0; iPart<pShape->Get_Part_Count(); iPart++)
729  {
730  for(iPoint=0; iPoint<pShape->Get_Point_Count(iPart); iPoint++)
731  {
732  fSHP.Write_Double(pShape->Get_M(iPoint, iPart));
733  }
734  }
735  }
736  }
737  }
738 
739  //-------------------------------------------------
740  // attributes...
741 
742  fDBF.Add_Record();
743 
744  for(iField=0; iField<Get_Field_Count(); iField++)
745  {
746  if( pShape->is_NoData(iField) )
747  {
748  fDBF.Set_NoData(iField);
749  }
750  else switch( fDBF.Get_Field_Type(iField) )
751  {
752  default:
753  fDBF.Set_Value(iField, pShape->asString(iField));
754  break;
755 
756  case DBF_FT_FLOAT:
757  case DBF_FT_NUMERIC:
758  fDBF.Set_Value(iField, pShape->asDouble(iField));
759  break;
760  }
761  }
762 
763  fDBF.Flush_Record();
764  }
765 
766  //-----------------------------------------------------
767  // File Sizes...
768 
769  fSHP.Seek(24);
770  fSHP.Write_Int(fSHP_Size, true);
771 
772  fSHX.Seek(24);
773  fSHX.Write_Int(fSHX_Size, true);
774 
775  //-----------------------------------------------------
776  Get_Projection().Save(SG_File_Make_Path("", File_Name, "prj"), SG_PROJ_FMT_WKT);
777 
778  //-----------------------------------------------------
779  CSG_MetaData *pFields = Get_MetaData_DB().Get_Child("FIELDS");
780 
781  if( !pFields )
782  {
783  pFields = Get_MetaData_DB().Add_Child("FIELDS");
784  }
785 
786  pFields->Del_Children();
787 
788  for(iField=0; iField<Get_Field_Count(); iField++)
789  {
790  pFields->Add_Child("FIELD", Get_Field_Name(iField))->Add_Property("TYPE", gSG_Data_Type_Identifier[Get_Field_Type(iField)]);
791  }
792 
793  Get_MetaData().Del_Child("GDAL_DRIVER");
794 
795  Save_MetaData(File_Name);
796 
797  //-----------------------------------------------------
798  return( true );
799 }
800 
801 
803 // //
804 // //
805 // //
807 
808 //---------------------------------------------------------
CSG_Table_Record::asDouble
double asDouble(int Field) const
Definition: table_record.cpp:527
CSG_File::Open
virtual bool Open(const CSG_String &FileName, int Mode=SG_FILE_R, bool bBinary=true, int Encoding=SG_FILE_ENCODING_ANSI)
Definition: api_file.cpp:111
DBF_FT_FLOAT
#define DBF_FT_FLOAT
Definition: table_dbase.h:77
PARAMETER_TYPE_FilePath
@ PARAMETER_TYPE_FilePath
Definition: parameters.h:136
CSG_Shapes::On_Delete
virtual bool On_Delete(void)
Definition: shapes_io.cpp:72
CSG_Data_Manager::Get_Shapes
CSG_Data_Collection * Get_Shapes(void) const
Definition: data_manager.h:172
CSG_MetaData::Del_Child
bool Del_Child(int Index)
Definition: metadata.cpp:329
_TL
#define _TL(s)
Definition: api_core.h:1480
CSG_Data_Collection::Get
CSG_Data_Object * Get(size_t i) const
Definition: data_manager.h:96
CSG_Data_Object::Set_File_Name
void Set_File_Name(const CSG_String &FileName)
Definition: dataobject.cpp:307
CSG_Shapes::Get_Shape
virtual CSG_Shape * Get_Shape(const CSG_Point &Point, double Epsilon=0.)
Definition: shapes.cpp:586
CSG_Data_Object::Update
bool Update(bool bForce=false)
Definition: dataobject.cpp:714
CSG_MetaData::Get_Children_Count
int Get_Children_Count(void) const
Definition: metadata.h:149
SG_PROJ_FMT_WKT
@ SG_PROJ_FMT_WKT
Definition: geo_tools.h:771
CSG_Data_Collection::Count
size_t Count(void) const
Definition: data_manager.h:94
CSG_MetaData::Get_Content
const CSG_String & Get_Content(void) const
Definition: metadata.h:135
CSG_Shapes::On_Reload
virtual bool On_Reload(void)
Definition: shapes_io.cpp:66
CSG_Shape::Get_Z
virtual double Get_Z(int iPoint=0, int iPart=0, bool bAscending=true) const
Definition: shapes.h:195
data_manager.h
CSG_Rect::Get_XMax
double Get_XMax(void) const
Definition: geo_tools.h:501
CSG_File::Seek
bool Seek(sLong Offset, int Origin=SG_FILE_START) const
Definition: api_file.cpp:232
SG_File_Cmp_Extension
SAGA_API_DLL_EXPORT bool SG_File_Cmp_Extension(const CSG_String &FileName, const CSG_String &Extension)
Definition: api_file.cpp:928
CSG_Shapes::Create
bool Create(const CSG_Shapes &Shapes)
Definition: shapes.cpp:204
CSG_Shapes::Get_MMax
double Get_MMax(void)
Definition: shapes.h:816
SHAPE_TYPE_Polygon
@ SHAPE_TYPE_Polygon
Definition: shapes.h:105
CSG_Data_Object::Save_MetaData
bool Save_MetaData(const CSG_String &FileName)
Definition: dataobject.cpp:618
CSG_Shapes::Get_MMin
double Get_MMin(void)
Definition: shapes.h:815
CSG_Buffer::asDouble
double asDouble(int Offset, bool bBigEndian=false) const
Definition: api_core.h:271
CSG_Tool
Definition: tool.h:149
CSG_Tool::Execute
bool Execute(bool bAddHistory=false)
Definition: tool.cpp:246
SG_RUN_TOOL
#define SG_RUN_TOOL(bRetVal, LIBRARY, TOOL, CONDITION)
Definition: tool_library.h:257
CSG_Table_DBase::Open_Write
bool Open_Write(const SG_Char *FileName, class CSG_Table *pTable, bool bRecords_Save=true)
Definition: table_dbase.cpp:307
CSG_Table::Get_Field_Count
int Get_Field_Count(void) const
Definition: table.h:355
SG_File_Delete
SAGA_API_DLL_EXPORT bool SG_File_Delete(const CSG_String &FileName)
Definition: api_file.cpp:840
CSG_Shapes::m_Type
TSG_Shape_Type m_Type
Definition: shapes.h:845
CSG_Table::m_Extent
CSG_Rect m_Extent
Definition: table.h:454
CSG_MetaData::Get_Child
CSG_MetaData * Get_Child(int Index) const
Definition: metadata.h:150
SG_FILE_R
@ SG_FILE_R
Definition: api_core.h:1101
SSG_Point
Definition: geo_tools.h:128
CSG_Buffer::Set_Size
bool Set_Size(size_t Size, bool bShrink=true)
Definition: api_memory.cpp:732
CSG_File::Read
size_t Read(void *Buffer, size_t Size, size_t Count=1) const
Definition: api_file.cpp:322
CSG_Tool_Library_Manager::Delete_Tool
bool Delete_Tool(CSG_Tool *pTool) const
Definition: tool_library.cpp:726
CSG_Shapes::Get_ZMin
double Get_ZMin(void)
Definition: shapes.h:813
CSG_Table_Record::is_NoData
bool is_NoData(int Field) const
Definition: table_record.cpp:416
CSG_File
Definition: api_core.h:1116
Set_Content_Length
#define Set_Content_Length(n)
Definition: shapes_io.cpp:484
CSG_Shape::Get_Point
virtual TSG_Point Get_Point(int iPoint=0) const =0
CSG_Shapes::Add_Shape
virtual CSG_Shape * Add_Shape(CSG_Table_Record *pCopy=NULL, TSG_ADD_Shape_Copy_Mode mCopy=SHAPE_COPY)
Definition: shapes.cpp:505
SG_VERTEX_TYPE_XY
@ SG_VERTEX_TYPE_XY
Definition: shapes.h:92
CSG_Tool::Set_Parameter
bool Set_Parameter(const CSG_String &ID, CSG_Parameter *pValue)
Definition: tool.cpp:1144
CSG_Data_Object::asShapes
class CSG_Shapes * asShapes(bool bPolymorph=false) const
Definition: dataobject.cpp:450
CSG_Projection::Load
bool Load(const CSG_String &FileName, TSG_Projection_Format Format=SG_PROJ_FMT_WKT)
Definition: projections.cpp:413
CSG_Shape::Get_Part_Count
virtual int Get_Part_Count(void) const =0
SG_TOOL_PARAMETER_SET
#define SG_TOOL_PARAMETER_SET(IDENTIFIER, VALUE)
Definition: tool_library.h:351
CSG_Table_DBase
Definition: table_dbase.h:91
CSG_Buffer
Definition: api_core.h:224
CSG_Table_Record::asString
const SG_Char * asString(int Field, int Decimals=-99) const
Definition: table_record.cpp:461
CSG_Buffer::asInt
int asInt(int Offset, bool bBigEndian=false) const
Definition: api_core.h:269
CSG_Table::Get_Field_Name
const SG_Char * Get_Field_Name(int iField) const
Definition: table.h:356
CSG_Rect::Get_YMin
double Get_YMin(void) const
Definition: geo_tools.h:502
SG_VERTEX_TYPE_XYZM
@ SG_VERTEX_TYPE_XYZM
Definition: shapes.h:94
CSG_MetaData::Del_Children
bool Del_Children(int Depth=0, const SG_Char *Name=NULL)
Definition: metadata.cpp:373
CSG_File::Read_Line
bool Read_Line(CSG_String &sLine) const
Definition: api_file.cpp:379
CSG_Tool_Library_Manager::Create_Tool
CSG_Tool * Create_Tool(const CSG_String &Library, int Index, bool bWithGUI=false) const
Definition: tool_library.cpp:697
SG_UI_Msg_Lock
int SG_UI_Msg_Lock(bool bOn)
Definition: api_callback.cpp:465
sLong
signed long long sLong
Definition: api_core.h:158
CSG_Table_DBase::Get_Field_Type
char Get_Field_Type(int iField)
Definition: table_dbase.h:109
SG_Get_Tool_Library_Manager
CSG_Tool_Library_Manager & SG_Get_Tool_Library_Manager(void)
Definition: tool_library.cpp:286
CSG_Shape::Get_ZMax
virtual double Get_ZMax(void)
Definition: shapes.h:197
CSG_Shape::Get_ZMin
virtual double Get_ZMin(void)
Definition: shapes.h:196
CSG_Table::Get_Count
sLong Get_Count(void) const
Definition: table.h:385
CSG_Data_Object::Get_File_Name
const SG_Char * Get_File_Name(bool bNative=true) const
Definition: dataobject.cpp:331
CSG_Table_DBase::Add_Record
void Add_Record(void)
Definition: table_dbase.cpp:612
CSG_Shape::Get_M
virtual double Get_M(int iPoint=0, int iPart=0, bool bAscending=true) const
Definition: shapes.h:211
CSG_File::Close
virtual bool Close(void)
Definition: api_file.cpp:154
CSG_Shape::Add_Point
virtual int Add_Point(double x, double y, int iPart=0)=0
SG_FILE_W
@ SG_FILE_W
Definition: api_core.h:1102
CSG_Shape::Get_MMin
virtual double Get_MMin(void)
Definition: shapes.h:212
CSG_Buffer::Get_Data
char * Get_Data(int Offset=0) const
Definition: api_core.h:244
CSG_Shapes::m_Vertex_Type
TSG_Vertex_Type m_Vertex_Type
Definition: shapes.h:847
CSG_Table::m_Encoding
int m_Encoding
Definition: table.h:442
CSG_String::Format
static CSG_String Format(const char *Format,...)
Definition: api_string.cpp:270
CSG_Table_DBase::Flush_Record
void Flush_Record(void)
Definition: table_dbase.cpp:630
CSG_String::Find
int Find(char Character, bool fromEnd=false) const
Definition: api_string.cpp:616
CSG_File::Write_Double
bool Write_Double(double Value, bool bBigEndian=false)
Definition: api_file.cpp:464
SHAPE_TYPE_Line
@ SHAPE_TYPE_Line
Definition: shapes.h:104
CSG_Table::Set_Field_Name
bool Set_Field_Name(int iField, const SG_Char *Name)
Definition: table.cpp:617
gSG_Data_Type_Identifier
const char gSG_Data_Type_Identifier[][32]
Definition: api_core.h:1031
table_dbase.h
SG_File_Set_Extension
SAGA_API_DLL_EXPORT bool SG_File_Set_Extension(CSG_String &FileName, const CSG_String &Extension)
Definition: api_file.cpp:934
CSG_Shape::Get_Point_Count
virtual int Get_Point_Count(void) const =0
TSG_Vertex_Type
TSG_Vertex_Type
Definition: shapes.h:91
shapes.h
CSG_Table_DBase::Set_Value
bool Set_Value(int iField, double Value)
Definition: table_dbase.cpp:771
CSG_String
Definition: api_core.h:557
SHAPE_TYPE_Point
@ SHAPE_TYPE_Point
Definition: shapes.h:102
CSG_Data_Manager
Definition: data_manager.h:164
CSG_MetaData
Definition: metadata.h:88
CSG_Data_Object::Get_MetaData_DB
CSG_MetaData & Get_MetaData_DB(void) const
Definition: dataobject.h:226
CSG_Tool::Settings_Push
bool Settings_Push(class CSG_Data_Manager *pManager=NULL)
Definition: tool.cpp:600
CSG_File::Printf
int Printf(const char *Format,...)
Definition: api_file.cpp:271
CSG_Shape::Get_MMax
virtual double Get_MMax(void)
Definition: shapes.h:213
SG_UI_Process_Set_Progress
bool SG_UI_Process_Set_Progress(int Position, int Range)
Definition: api_callback.cpp:249
CSG_File::Write_Int
bool Write_Int(int Value, bool bBigEndian=false)
Definition: api_file.cpp:438
CSG_Data_Object::Get_MetaData
CSG_MetaData & Get_MetaData(void) const
Definition: dataobject.h:225
SSG_Point::x
double x
Definition: geo_tools.h:129
CSG_Table_Record::Set_Value
bool Set_Value(int Field, const CSG_String &Value)
Definition: table_record.cpp:270
CSG_Rect::Get_YMax
double Get_YMax(void) const
Definition: geo_tools.h:503
CSG_Data_Object::Load_MetaData
bool Load_MetaData(const CSG_String &FileName)
Definition: dataobject.cpp:595
CSG_Rect::Get_XMin
double Get_XMin(void) const
Definition: geo_tools.h:500
SSG_Point::y
double y
Definition: geo_tools.h:129
CSG_Table_DBase::Set_NoData
bool Set_NoData(int iField)
Definition: table_dbase.cpp:904
CSG_Projection::Save
bool Save(const CSG_String &FileName, TSG_Projection_Format Format=SG_PROJ_FMT_WKT) const
Definition: projections.cpp:421
CSG_Shapes::Make_Clean
bool Make_Clean(void)
Definition: shapes.cpp:628
CSG_Shape::Get_Extent
virtual const CSG_Rect & Get_Extent(void)=0
CSG_Shape::Set_Z
virtual void Set_Z(double z, int iPoint=0, int iPart=0)
Definition: shapes.h:194
SG_File_Make_Path
SAGA_API_DLL_EXPORT CSG_String SG_File_Make_Path(const CSG_String &Directory, const CSG_String &Name)
Definition: api_file.cpp:903
CSG_Shapes
Definition: shapes.h:773
tool_library.h
SHAPE_TYPE_Points
@ SHAPE_TYPE_Points
Definition: shapes.h:103
CSG_File::Write
size_t Write(void *Buffer, size_t Size, size_t Count=1) const
Definition: api_file.cpp:354
CSG_MetaData::Add_Child
CSG_MetaData * Add_Child(void)
Definition: metadata.cpp:174
SG_UI_Msg_Add_Error
void SG_UI_Msg_Add_Error(const char *Message)
Definition: api_callback.cpp:544
CSG_Table::Get_Field_Type
TSG_Data_Type Get_Field_Type(int iField) const
Definition: table.h:357
DBF_FT_NUMERIC
#define DBF_FT_NUMERIC
Definition: table_dbase.h:78
SG_FILE_ENCODING_ANSI
@ SG_FILE_ENCODING_ANSI
Definition: api_core.h:544
SG_FILE_ENCODING_UTF8
@ SG_FILE_ENCODING_UTF8
Definition: api_core.h:546
CSG_Shape::Set_M
virtual void Set_M(double m, int iPoint=0, int iPart=0)
Definition: shapes.h:210
CSG_Shape
Definition: shapes.h:141
CSG_Data_Object::Get_Projection
CSG_Projection & Get_Projection(void)
Definition: dataobject.cpp:578
CSG_Table_Record::Set_NoData
bool Set_NoData(int Field)
Definition: table_record.cpp:366
SG_VERTEX_TYPE_XYZ
@ SG_VERTEX_TYPE_XYZ
Definition: shapes.h:93
CSG_Shapes::Get_ZMax
double Get_ZMax(void)
Definition: shapes.h:814