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
|
/******************************************************************************
*
* Project: FlatGeobuf driver
* Purpose: Implements GeometryWriter class.
* Author: Björn Harrtell <bjorn at wololo dot org>
*
******************************************************************************
* Copyright (c) 2018-2019, Björn Harrtell <bjorn at wololo dot org>
*
* SPDX-License-Identifier: MIT
****************************************************************************/
#include "geometrywriter.h"
using namespace flatbuffers;
using namespace FlatGeobuf;
using namespace ogr_flatgeobuf;
GeometryType
GeometryWriter::translateOGRwkbGeometryType(const OGRwkbGeometryType eGType)
{
const auto flatType = wkbFlatten(eGType);
GeometryType geometryType = GeometryType::Unknown;
if (flatType <= 17)
geometryType = (GeometryType)flatType;
return geometryType;
}
void GeometryWriter::writePoint(const OGRPoint *p)
{
m_xy.push_back(p->getX());
m_xy.push_back(p->getY());
if (m_hasZ)
m_z.push_back(p->getZ());
if (m_hasM)
m_m.push_back(p->getM());
}
void GeometryWriter::writeMultiPoint(const OGRMultiPoint *mp)
{
for (const auto part : *mp)
if (!part->IsEmpty())
writePoint(part);
}
uint32_t GeometryWriter::writeSimpleCurve(const OGRSimpleCurve *sc)
{
uint32_t numPoints = sc->getNumPoints();
const auto xyLength = m_xy.size();
m_xy.resize(xyLength + (numPoints * 2));
const auto zLength = m_z.size();
double *padfZOut = nullptr;
if (m_hasZ)
{
m_z.resize(zLength + numPoints);
padfZOut = m_z.data() + zLength;
}
const auto mLength = m_m.size();
double *padfMOut = nullptr;
if (m_hasM)
{
m_m.resize(mLength + numPoints);
padfMOut = m_m.data() + mLength;
}
sc->getPoints(reinterpret_cast<double *>(
reinterpret_cast<OGRRawPoint *>(m_xy.data() + xyLength)),
sizeof(OGRRawPoint),
reinterpret_cast<double *>(
reinterpret_cast<OGRRawPoint *>(m_xy.data() + xyLength)) +
1,
sizeof(OGRRawPoint), padfZOut, sizeof(double), padfMOut,
sizeof(double));
return numPoints;
}
void GeometryWriter::writeMultiLineString(const OGRMultiLineString *mls)
{
uint32_t e = 0;
for (const auto part : *mls)
if (!part->IsEmpty())
m_ends.push_back(e += writeSimpleCurve(part));
}
void GeometryWriter::writePolygon(const OGRPolygon *p)
{
const auto exteriorRing = p->getExteriorRing();
const auto numInteriorRings = p->getNumInteriorRings();
uint32_t e = writeSimpleCurve(exteriorRing);
// NOTE: should not write ends if only exterior ring
if (numInteriorRings > 0)
{
m_ends.push_back(e);
for (int i = 0; i < numInteriorRings; i++)
m_ends.push_back(e += writeSimpleCurve(p->getInteriorRing(i)));
}
}
const Offset<Geometry>
GeometryWriter::writeMultiPolygon(const OGRMultiPolygon *mp, int depth)
{
std::vector<Offset<Geometry>> parts;
for (const auto part : *mp)
if (!part->IsEmpty())
parts.push_back(writePart(part, GeometryType::Polygon, depth + 1));
return CreateGeometryDirect(m_fbb, nullptr, nullptr, nullptr, nullptr,
nullptr, nullptr, m_geometryType, &parts);
}
const Offset<Geometry>
GeometryWriter::writeGeometryCollection(const OGRGeometryCollection *gc,
int depth)
{
std::vector<Offset<Geometry>> parts;
for (const auto part : *gc)
if (!part->IsEmpty())
parts.push_back(writePart(part, depth + 1));
return CreateGeometryDirect(m_fbb, nullptr, nullptr, nullptr, nullptr,
nullptr, nullptr, m_geometryType, &parts);
}
const Offset<Geometry>
GeometryWriter::writeCompoundCurve(const OGRCompoundCurve *cc, int depth)
{
std::vector<Offset<Geometry>> parts;
for (const auto curve : *cc)
parts.push_back(writePart(curve, depth + 1));
return CreateGeometryDirect(m_fbb, nullptr, nullptr, nullptr, nullptr,
nullptr, nullptr, m_geometryType, &parts);
}
const Offset<Geometry>
GeometryWriter::writeCurvePolygon(const OGRCurvePolygon *cp, int depth)
{
std::vector<Offset<Geometry>> parts;
for (const auto curve : *cp)
parts.push_back(writePart(curve, depth + 1));
return CreateGeometryDirect(m_fbb, nullptr, nullptr, nullptr, nullptr,
nullptr, nullptr, m_geometryType, &parts);
}
const Offset<Geometry>
GeometryWriter::writePolyhedralSurface(const OGRPolyhedralSurface *p, int depth)
{
std::vector<Offset<Geometry>> parts;
for (const auto part : *p)
parts.push_back(writePart(part, depth + 1));
return CreateGeometryDirect(m_fbb, nullptr, nullptr, nullptr, nullptr,
nullptr, nullptr, m_geometryType, &parts);
}
void GeometryWriter::writeTIN(const OGRTriangulatedSurface *ts)
{
const auto numGeometries = ts->getNumGeometries();
if (numGeometries == 1)
return (void)writeSimpleCurve(ts->getGeometryRef(0)->getExteriorRing());
uint32_t e = 0;
for (const auto part : *ts)
m_ends.push_back(e += writeSimpleCurve(part->getExteriorRing()));
}
const Offset<Geometry> GeometryWriter::write(int depth)
{
bool unknownGeometryType = false;
if (depth == 0 && m_geometryType == GeometryType::Unknown)
{
m_geometryType =
translateOGRwkbGeometryType(m_ogrGeometry->getGeometryType());
unknownGeometryType = true;
}
switch (m_geometryType)
{
case GeometryType::Point:
writePoint(m_ogrGeometry->toPoint());
break;
case GeometryType::MultiPoint:
writeMultiPoint(m_ogrGeometry->toMultiPoint());
break;
case GeometryType::LineString:
writeSimpleCurve(m_ogrGeometry->toLineString());
break;
case GeometryType::MultiLineString:
writeMultiLineString(m_ogrGeometry->toMultiLineString());
break;
case GeometryType::Polygon:
writePolygon(m_ogrGeometry->toPolygon());
break;
case GeometryType::MultiPolygon:
return writeMultiPolygon(m_ogrGeometry->toMultiPolygon(), depth);
case GeometryType::GeometryCollection:
return writeGeometryCollection(
m_ogrGeometry->toGeometryCollection(), depth);
case GeometryType::CircularString:
writeSimpleCurve(m_ogrGeometry->toCircularString());
break;
case GeometryType::CompoundCurve:
return writeCompoundCurve(m_ogrGeometry->toCompoundCurve(), depth);
case GeometryType::CurvePolygon:
return writeCurvePolygon(m_ogrGeometry->toCurvePolygon(), depth);
case GeometryType::MultiCurve:
return writeGeometryCollection(m_ogrGeometry->toMultiCurve(),
depth);
case GeometryType::MultiSurface:
return writeGeometryCollection(m_ogrGeometry->toMultiSurface(),
depth);
case GeometryType::PolyhedralSurface:
return writePolyhedralSurface(m_ogrGeometry->toPolyhedralSurface(),
depth);
case GeometryType::Triangle:
writePolygon(m_ogrGeometry->toTriangle());
break;
case GeometryType::TIN:
writeTIN(m_ogrGeometry->toTriangulatedSurface());
break;
default:
CPLError(CE_Failure, CPLE_AppDefined,
"GeometryWriter::write: Unknown type %d",
(int)m_geometryType);
return 0;
}
const auto pEnds = m_ends.empty() ? nullptr : &m_ends;
const auto pXy = m_xy.empty() ? nullptr : &m_xy;
const auto pZ = m_z.empty() ? nullptr : &m_z;
const auto pM = m_m.empty() ? nullptr : &m_m;
const auto geometryType = depth > 0 || unknownGeometryType
? m_geometryType
: GeometryType::Unknown;
return FlatGeobuf::CreateGeometryDirect(m_fbb, pEnds, pXy, pZ, pM, nullptr,
nullptr, geometryType);
}
|