1 
2 ///////////////////////////////////////////////////////////
3 //                                                       //
4 //                         SAGA                          //
5 //                                                       //
6 //      System for Automated Geoscientific Analyses      //
7 //                                                       //
8 //                     Tool Library                      //
9 //                                                       //
10 //                       io_gdal                         //
11 //                                                       //
12 //-------------------------------------------------------//
13 //                                                       //
14 //                    ogr_import.cpp                     //
15 //                                                       //
16 //            Copyright (C) 2008 O. Conrad               //
17 //                                                       //
18 //-------------------------------------------------------//
19 //                                                       //
20 // This file is part of 'SAGA - System for Automated     //
21 // Geoscientific Analyses'. SAGA is free software; you   //
22 // can redistribute it and/or modify it under the terms  //
23 // of the GNU General Public License as published by the //
24 // Free Software Foundation, either version 2 of the     //
25 // License, or (at your option) any later version.       //
26 //                                                       //
27 // SAGA is distributed in the hope that it will be       //
28 // useful, but WITHOUT ANY WARRANTY; without even the    //
29 // implied warranty of MERCHANTABILITY or FITNESS FOR A  //
30 // PARTICULAR PURPOSE. See the GNU General Public        //
31 // License for more details.                             //
32 //                                                       //
33 // You should have received a copy of the GNU General    //
34 // Public License along with this program; if not, see   //
35 // <http://www.gnu.org/licenses/>.                       //
36 //                                                       //
37 //-------------------------------------------------------//
38 //                                                       //
39 //    e-mail:     oconrad@saga-gis.de                    //
40 //                                                       //
41 //    contact:    Olaf Conrad                            //
42 //                Bundesstr. 55                          //
43 //                D-20146 Hamburg                        //
44 //                Germany                                //
45 //                                                       //
46 ///////////////////////////////////////////////////////////
47 
48 //---------------------------------------------------------
49 #include "ogr_import.h"
50 
51 
52 ///////////////////////////////////////////////////////////
53 //														 //
54 //														 //
55 //														 //
56 ///////////////////////////////////////////////////////////
57 
58 //---------------------------------------------------------
COGR_Import(void)59 COGR_Import::COGR_Import(void)
60 {
61 	Set_Name	(_TL("Import Shapes"));
62 
63 	Set_Author	("O.Conrad (c) 2008");
64 
65 	Add_Reference("GDAL/OGR contributors", "2019",
66 		"GDAL/OGR Geospatial Data Abstraction software Library",
67 		"A translator library for raster and vector geospatial data formats. Open Source Geospatial Foundation.",
68 		SG_T("https://gdal.org"), SG_T("Link")
69 	);
70 
71 	CSG_String	Description, Filter, Filter_All;
72 
73 	Description	= _TW(
74 		"The \"OGR Vector Data Import\" tool imports vector data from various file/database formats using the "
75 		"\"Geospatial Data Abstraction Library\" (GDAL) by Frank Warmerdam. "
76 	);
77 
78 	Description	+= CSG_String::Format("\nGDAL %s:%s\n\n", _TL("Version"), SG_Get_OGR_Drivers().Get_Version().c_str());
79 
80 	Description	+= _TL("Following raster formats are currently supported:");
81 
82 	Description	+= CSG_String::Format("\n<table border=\"1\"><tr><th>%s</th><th>%s</th><th>%s</th></tr>",
83 		_TL("ID"), _TL("Name"), _TL("Extension")
84 	);
85 
86 	for(int i=0; i<SG_Get_OGR_Drivers().Get_Count(); i++)
87     {
88 		if( SG_Get_OGR_Drivers().is_Vector(i) && SG_Get_OGR_Drivers().Can_Read(i) )
89 		{
90 			CSG_String	ID		= SG_Get_OGR_Drivers().Get_Description(i).c_str();
91 			CSG_String	Name	= SG_Get_OGR_Drivers().Get_Name       (i).c_str();
92 			CSG_String	Ext		= SG_Get_OGR_Drivers().Get_Extension  (i).c_str();
93 
94 			Description	+= "<tr><td>" + ID + "</td><td>" + Name + "</td><td>" + Ext + "</td></tr>";
95 
96 			if( !Ext.is_Empty() )
97 			{
98 				Ext.Replace("/", ";");
99 
100 				Filter		+= Name + "|*." + Ext + "|";
101 				Filter_All	+= (Filter_All.is_Empty() ? "*." : ";*.") + Ext;
102 			}
103 		}
104     }
105 
106 	Description	+= "</table>";
107 
108 	Set_Description(Description);
109 
110 	Filter.Prepend(CSG_String::Format("%s|%s|", _TL("All Recognized Files"), Filter_All.c_str()));
111 	Filter.Append (CSG_String::Format("%s|*.*", _TL("All Files")));
112 
113 	//-----------------------------------------------------
114 	Parameters.Add_Shapes_List("",
115 		"SHAPES"	, _TL("Shapes"),
116 		_TL(""),
117 		PARAMETER_OUTPUT
118 	);
119 
120 	Parameters.Add_FilePath("",
121 		"FILES"		, _TL("Files"),
122 		_TL(""),
123 		Filter, NULL, false, false, true
124 	);
125 
126 	Parameters.Add_Choice("",
127 		"GEOM_TYPE"	, _TL("Geometry Type"),
128 		_TL("Some OGR drivers are unable to determine the geometry type automatically, please choose the appropriate one in this case"),
129 		SG_Get_OGR_WKB_Type_Choices(), 0
130 	);
131 }
132 
133 
134 ///////////////////////////////////////////////////////////
135 //														 //
136 ///////////////////////////////////////////////////////////
137 
138 //---------------------------------------------------------
On_Execute(void)139 bool COGR_Import::On_Execute(void)
140 {
141 	//-----------------------------------------------------
142 	CSG_Strings		Files;
143 
144 	if( !Parameters("FILES")->asFilePath()->Get_FilePaths(Files) )
145 	{
146 		return( false );
147 	}
148 
149 	//-----------------------------------------------------
150 	Parameters("SHAPES")->asShapesList()->Del_Items();
151 
152 	for(int iFile=0; iFile<Files.Get_Count(); iFile++)
153 	{
154 		Message_Fmt("\n%s: %s", _TL("loading"), Files[iFile].c_str());
155 
156 		CSG_OGR_DataSet	DataSource;
157 
158 		if( !DataSource.Create(Files[iFile]) )
159 		{
160 			Message_Add(_TL("could not open data source"));
161 		}
162 		else if( DataSource.Get_Count() <= 0 )
163 		{
164 			Message_Add(_TL("no layers in data source"));
165 		}
166 		else for(int iLayer=0; iLayer<DataSource.Get_Count(); iLayer++)
167 		{
168 			CSG_Shapes	*pShapes	= DataSource.Read(iLayer, Parameters("GEOM_TYPE")->asInt());
169 
170 			if( pShapes )
171 			{
172 				Parameters("SHAPES")->asShapesList()->Add_Item(pShapes);
173 
174 				CSG_String	Name	= pShapes->Get_Name();
175 
176 				pShapes->Get_MetaData().Add_Child("GDAL_DRIVER", DataSource.Get_DriverID());
177 				pShapes->Set_File_Name(Files[iFile]);
178 				pShapes->Set_Description(DataSource.Get_Description(iLayer));
179 
180 				if( Name.is_Empty() )
181 				{
182 					pShapes->Set_Name(SG_File_Get_Name(Files[iFile], false) + (DataSource.Get_Count() == 1 ? CSG_String("") : CSG_String::Format(" [%d]", 1 + iLayer)));
183 				}
184 				else
185 				{
186 					pShapes->Set_Name(Name);
187 				}
188 			}
189 		}
190 	}
191 
192 	//-----------------------------------------------------
193 	return( Parameters("SHAPES")->asShapesList()->Get_Item_Count() > 0 );
194 }
195 
196 
197 ///////////////////////////////////////////////////////////
198 //														 //
199 //														 //
200 //														 //
201 ///////////////////////////////////////////////////////////
202 
203 //---------------------------------------------------------
SG_OGR_Import(const CSG_String & File_Name)204 bool	SG_OGR_Import	(const CSG_String &File_Name)
205 {
206 	COGR_Import	Import;
207 
208 	if(	!Import.Get_Parameters()->Set_Parameter("FILES", File_Name, PARAMETER_TYPE_FilePath) )
209 	{
210 		return( false );
211 	}
212 
213 	if(	!Import.Execute() )
214 	{
215 		return( false );
216 	}
217 
218 	CSG_Parameter_Shapes_List	*pShapes	= Import.Get_Parameters()->Get_Parameter("SHAPES")->asShapesList();
219 
220 	for(int i=0; i<pShapes->Get_Item_Count(); i++)
221 	{
222 		SG_UI_DataObject_Add(pShapes->Get_Shapes(i), SG_UI_DATAOBJECT_UPDATE_ONLY);
223 	}
224 
225 	return( true );
226 }
227 
228 
229 ///////////////////////////////////////////////////////////
230 //														 //
231 //														 //
232 //														 //
233 ///////////////////////////////////////////////////////////
234 
235 //---------------------------------------------------------
236