1 : /******************************************************************************
2 : * $Id: ograrcgenlayer.cpp 23148 2011-10-01 11:55:08Z rouault $
3 : *
4 : * Project: Arc/Info Generate Translator
5 : * Purpose: Implements OGRARCGENLayer class.
6 : * Author: Even Rouault, <even dot rouault at mines dash paris dot org>
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2011, Even Rouault <even dot rouault at mines dash paris dot org>
10 : *
11 : * Permission is hereby granted, free of charge, to any person obtaining a
12 : * copy of this software and associated documentation files (the "Software"),
13 : * to deal in the Software without restriction, including without limitation
14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 : * and/or sell copies of the Software, and to permit persons to whom the
16 : * Software is furnished to do so, subject to the following conditions:
17 : *
18 : * The above copyright notice and this permission notice shall be included
19 : * in all copies or substantial portions of the Software.
20 : *
21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMARCGENS OR OTHER
25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 : * DEALINGS IN THE SOFTWARE.
28 : ****************************************************************************/
29 :
30 : #include "ogr_arcgen.h"
31 : #include "cpl_conv.h"
32 : #include "cpl_string.h"
33 : #include "ogr_p.h"
34 : #include "ogr_srs_api.h"
35 :
36 : CPL_CVSID("$Id: ograrcgenlayer.cpp 23148 2011-10-01 11:55:08Z rouault $");
37 :
38 : /************************************************************************/
39 : /* OGRARCGENLayer() */
40 : /************************************************************************/
41 :
42 6 : OGRARCGENLayer::OGRARCGENLayer( const char* pszFilename,
43 6 : VSILFILE* fp, OGRwkbGeometryType eType )
44 :
45 : {
46 6 : this->fp = fp;
47 6 : nNextFID = 0;
48 6 : bEOF = FALSE;
49 :
50 6 : poFeatureDefn = new OGRFeatureDefn( CPLGetBasename(pszFilename) );
51 6 : poFeatureDefn->Reference();
52 6 : poFeatureDefn->SetGeomType( eType );
53 :
54 6 : OGRFieldDefn oField1( "ID", OFTInteger);
55 6 : poFeatureDefn->AddFieldDefn( &oField1 );
56 6 : }
57 :
58 : /************************************************************************/
59 : /* ~OGRARCGENLayer() */
60 : /************************************************************************/
61 :
62 6 : OGRARCGENLayer::~OGRARCGENLayer()
63 :
64 : {
65 6 : poFeatureDefn->Release();
66 :
67 6 : VSIFCloseL( fp );
68 6 : }
69 :
70 :
71 : /************************************************************************/
72 : /* ResetReading() */
73 : /************************************************************************/
74 :
75 0 : void OGRARCGENLayer::ResetReading()
76 :
77 : {
78 0 : nNextFID = 0;
79 0 : bEOF = FALSE;
80 0 : VSIFSeekL( fp, 0, SEEK_SET );
81 0 : }
82 :
83 :
84 : /************************************************************************/
85 : /* GetNextFeature() */
86 : /************************************************************************/
87 :
88 6 : OGRFeature *OGRARCGENLayer::GetNextFeature()
89 : {
90 : OGRFeature *poFeature;
91 :
92 0 : while(TRUE)
93 : {
94 6 : poFeature = GetNextRawFeature();
95 6 : if (poFeature == NULL)
96 0 : return NULL;
97 :
98 6 : if((m_poFilterGeom == NULL
99 : || FilterGeometry( poFeature->GetGeometryRef() ) )
100 : && (m_poAttrQuery == NULL
101 : || m_poAttrQuery->Evaluate( poFeature )) )
102 : {
103 6 : return poFeature;
104 : }
105 : else
106 0 : delete poFeature;
107 : }
108 : }
109 :
110 : /************************************************************************/
111 : /* GetNextRawFeature() */
112 : /************************************************************************/
113 :
114 6 : OGRFeature *OGRARCGENLayer::GetNextRawFeature()
115 : {
116 6 : if (bEOF)
117 0 : return NULL;
118 :
119 : const char* pszLine;
120 6 : OGRwkbGeometryType eType = poFeatureDefn->GetGeomType();
121 :
122 6 : if (wkbFlatten(eType) == wkbPoint)
123 : {
124 0 : while(TRUE)
125 : {
126 2 : pszLine = CPLReadLine2L(fp,256,NULL);
127 2 : if (pszLine == NULL || EQUAL(pszLine, "END"))
128 : {
129 0 : bEOF = TRUE;
130 0 : return NULL;
131 : }
132 2 : char** papszTokens = CSLTokenizeString2( pszLine, " ,", 0 );
133 2 : int nTokens = CSLCount(papszTokens);
134 2 : if (nTokens == 3 || nTokens == 4)
135 : {
136 2 : OGRFeature* poFeature = new OGRFeature(poFeatureDefn);
137 2 : poFeature->SetFID(nNextFID ++);
138 2 : poFeature->SetField(0, papszTokens[0]);
139 2 : if (nTokens == 3)
140 : poFeature->SetGeometryDirectly(
141 1 : new OGRPoint(atof(papszTokens[1]),
142 2 : atof(papszTokens[2])));
143 : else
144 : poFeature->SetGeometryDirectly(
145 1 : new OGRPoint(atof(papszTokens[1]),
146 1 : atof(papszTokens[2]),
147 3 : atof(papszTokens[3])));
148 2 : CSLDestroy(papszTokens);
149 2 : return poFeature;
150 : }
151 : else
152 0 : CSLDestroy(papszTokens);
153 : }
154 : }
155 :
156 4 : CPLString osID;
157 : OGRLinearRing* poLR =
158 4 : (wkbFlatten(eType) == wkbPolygon) ? new OGRLinearRing() : NULL;
159 : OGRLineString* poLS =
160 4 : (wkbFlatten(eType) == wkbLineString) ? new OGRLineString() : poLR;
161 18 : while(TRUE)
162 : {
163 22 : pszLine = CPLReadLine2L(fp,256,NULL);
164 22 : if (pszLine == NULL)
165 0 : break;
166 :
167 22 : if (EQUAL(pszLine, "END"))
168 : {
169 4 : if (osID.size() == 0)
170 0 : break;
171 :
172 4 : OGRFeature* poFeature = new OGRFeature(poFeatureDefn);
173 4 : poFeature->SetFID(nNextFID ++);
174 4 : poFeature->SetField(0, osID.c_str());
175 4 : if (wkbFlatten(eType) == wkbPolygon)
176 : {
177 2 : OGRPolygon* poPoly = new OGRPolygon();
178 2 : poPoly->addRingDirectly(poLR);
179 2 : poFeature->SetGeometryDirectly(poPoly);
180 : }
181 : else
182 2 : poFeature->SetGeometryDirectly(poLS);
183 4 : return poFeature;
184 : }
185 :
186 18 : char** papszTokens = CSLTokenizeString2( pszLine, " ,", 0 );
187 18 : int nTokens = CSLCount(papszTokens);
188 18 : if (osID.size() == 0)
189 : {
190 4 : if (nTokens >= 1)
191 4 : osID = papszTokens[0];
192 : else
193 : {
194 0 : CSLDestroy(papszTokens);
195 0 : break;
196 : }
197 : }
198 : else
199 : {
200 14 : if (nTokens == 2)
201 : {
202 7 : poLS->addPoint(atof(papszTokens[0]),
203 14 : atof(papszTokens[1]));
204 : }
205 7 : else if (nTokens == 3)
206 : {
207 7 : poLS->addPoint(atof(papszTokens[0]),
208 7 : atof(papszTokens[1]),
209 21 : atof(papszTokens[2]));
210 : }
211 : else
212 : {
213 0 : CSLDestroy(papszTokens);
214 0 : break;
215 : }
216 : }
217 18 : CSLDestroy(papszTokens);
218 : }
219 :
220 0 : bEOF = TRUE;
221 0 : delete poLS;
222 0 : return NULL;
223 : }
224 : /************************************************************************/
225 : /* TestCapability() */
226 : /************************************************************************/
227 :
228 0 : int OGRARCGENLayer::TestCapability( const char * pszCap )
229 :
230 : {
231 0 : return FALSE;
232 : }
233 :
|