Debian Codegraph @

ogr/ogrsf_frmts/dwg/ogrdwgblockslayer.cpp

raw
1 /******************************************************************************
2 *
3 * Project: DWG Translator
4 * Purpose: Implements OGRDWGBlocksLayer class.
5 * Author: Frank Warmerdam, [email protected]
6 *
7 ******************************************************************************
8 * Copyright (c) 2010, Frank Warmerdam <[email protected]>
9 *
10 * SPDX-License-Identifier: MIT
11 ****************************************************************************/
12
13 #include "ogr_dwg.h"
14 #include "cpl_conv.h"
15
16 /************************************************************************/
17 /* OGRDWGBlocksLayer() */
18 /************************************************************************/
19
20 OGRDWGBlocksLayer::OGRDWGBlocksLayer(OGRDWGDataSource *poDSIn)
21 : poDS(poDSIn), poFeatureDefn(new OGRFeatureDefn("blocks"))
22 {
23 OGRDWGBlocksLayer::ResetReading();
24
25 poFeatureDefn->Reference();
26
27 poDS->AddStandardFields(poFeatureDefn);
28 }
29
30 /************************************************************************/
31 /* ~OGRDWGBlocksLayer() */
32 /************************************************************************/
33
34 OGRDWGBlocksLayer::~OGRDWGBlocksLayer()
35
36 {
37 if (m_nFeaturesRead > 0 && poFeatureDefn != nullptr)
38 {
39 CPLDebug("DWG", "%d features read on layer '%s'.", (int)m_nFeaturesRead,
40 poFeatureDefn->GetName());
41 }
42
43 if (poFeatureDefn)
44 poFeatureDefn->Release();
45 }
46
47 /************************************************************************/
48 /* ResetReading() */
49 /************************************************************************/
50
51 void OGRDWGBlocksLayer::ResetReading()
52
53 {
54 iNextFID = 0;
55 iNextSubFeature = 0;
56 oIt = poDS->GetBlockMap().begin();
57 }
58
59 /************************************************************************/
60 /* GetNextUnfilteredFeature() */
61 /************************************************************************/
62
63 OGRFeature *OGRDWGBlocksLayer::GetNextUnfilteredFeature()
64
65 {
66 OGRFeature *poFeature = nullptr;
67
68 /* -------------------------------------------------------------------- */
69 /* Are we out of features? */
70 /* -------------------------------------------------------------------- */
71 if (oIt == poDS->GetBlockMap().end())
72 return nullptr;
73
74 /* -------------------------------------------------------------------- */
75 /* Are we done reading the current blocks features? */
76 /* -------------------------------------------------------------------- */
77 DWGBlockDefinition *psBlock = &(oIt->second);
78 unsigned int nSubFeatureCount =
79 static_cast<unsigned int>(psBlock->apoFeatures.size());
80
81 if (psBlock->poGeometry != nullptr)
82 nSubFeatureCount++;
83
84 if (iNextSubFeature >= nSubFeatureCount)
85 {
86 ++oIt;
87
88 iNextSubFeature = 0;
89
90 if (oIt == poDS->GetBlockMap().end())
91 return nullptr;
92
93 psBlock = &(oIt->second);
94 }
95
96 /* -------------------------------------------------------------------- */
97 /* Is this a geometry based block? */
98 /* -------------------------------------------------------------------- */
99 if (psBlock->poGeometry != nullptr &&
100 iNextSubFeature == psBlock->apoFeatures.size())
101 {
102 poFeature = new OGRFeature(poFeatureDefn);
103 poFeature->SetGeometry(psBlock->poGeometry);
104 iNextSubFeature++;
105 }
106
107 /* -------------------------------------------------------------------- */
108 /* Otherwise duplicate the next sub-feature. */
109 /* -------------------------------------------------------------------- */
110 else
111 {
112 poFeature = new OGRFeature(poFeatureDefn);
113 poFeature->SetFrom(psBlock->apoFeatures[iNextSubFeature]);
114 iNextSubFeature++;
115 }
116
117 /* -------------------------------------------------------------------- */
118 /* Set FID and block name. */
119 /* -------------------------------------------------------------------- */
120 poFeature->SetFID(iNextFID++);
121
122 poFeature->SetField("BlockName", oIt->first.c_str());
123
124 m_nFeaturesRead++;
125
126 return poFeature;
127 }
128
129 /************************************************************************/
130 /* GetNextFeature() */
131 /************************************************************************/
132
133 OGRFeature *OGRDWGBlocksLayer::GetNextFeature()
134
135 {
136 while (true)
137 {
138 OGRFeature *poFeature = GetNextUnfilteredFeature();
139
140 if (poFeature == nullptr)
141 return nullptr;
142
143 if ((m_poFilterGeom == nullptr ||
144 FilterGeometry(poFeature->GetGeometryRef())) &&
145 (m_poAttrQuery == nullptr || m_poAttrQuery->Evaluate(poFeature)))
146 {
147 return poFeature;
148 }
149
150 delete poFeature;
151 }
152 }
153
154 /************************************************************************/
155 /* TestCapability() */
156 /************************************************************************/
157
158 int OGRDWGBlocksLayer::TestCapability(const char *pszCap) const
159
160 {
161 if (EQUAL(pszCap, OLCStringsAsUTF8))
162 return TRUE;
163 else
164 return FALSE;
165 }
166
167 /************************************************************************/
168 /* GetDataset() */
169 /************************************************************************/
170
171 GDALDataset *OGRDWGBlocksLayer::GetDataset()
172 {
173 return poDS;
174 }