Skip to content

Commit

Permalink
FeatureImage: add simple point rasterization (with demo)
Browse files Browse the repository at this point in the history
  • Loading branch information
gwaldron committed Oct 10, 2024
1 parent 8b35685 commit 17756d5
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 24 deletions.
23 changes: 23 additions & 0 deletions src/osgEarth/FeatureRasterizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,7 @@ FeatureRasterizer::render_blend2d(
_inverted = true;

// find the symbology:
const PointSymbol* masterPoint = style.get<PointSymbol>();
const LineSymbol* masterLine = style.getSymbol<LineSymbol>();
const PolygonSymbol* masterPoly = style.getSymbol<PolygonSymbol>();
const CoverageSymbol* masterCov = style.getSymbol<CoverageSymbol>();
Expand Down Expand Up @@ -865,6 +866,28 @@ FeatureRasterizer::render_blend2d(
}
}

if (masterPoint)
{
float width = masterPoint->size().value();

ctx.setFillStyle(BLRgba(
masterPoint->fill()->color().r(), masterPoint->fill()->color().g(), masterPoint->fill()->color().b(), masterPoint->fill()->color().a()));

for (const auto& feature : features)
{
feature->getGeometry()->forEachPart([&](const Geometry* part)
{
for (auto& p : *part)
{
double x = frame.xf * (p.x() - frame.xmin);
double y = frame.yf * (p.y() - frame.ymin);
y = ctx.targetHeight() - y;
ctx.fillCircle(x, y, width / 2.0);
}
});
}
}

if (masterText || masterSkin)
{
// Sort the features based their location. We do this to ensure that features collected in a metatiling
Expand Down
43 changes: 33 additions & 10 deletions src/osgEarth/Geometry
Original file line number Diff line number Diff line change
Expand Up @@ -256,17 +256,18 @@ namespace osgEarth
//! Whether a closed geometry contains the 2D point
virtual bool contains2D(double x, double y) const { return false; }

//! Iterate over all the parts of a geometry
void forEachPart(const std::function<void(Geometry*)>& func) { forEachPart(true, func); }

//! Iterate over all the parts of a geometry
void forEachPart(bool includePolygonHoles, const std::function<void(Geometry*)>& func);
//! Iterate over all the parts of a geometry (signature = void(Geometry* part))
template<typename CALLABLE>
inline void forEachPart(bool includePolygonHoles, CALLABLE&& func);
template<typename CALLABLE>
inline void forEachPart(CALLABLE&& func) { forEachPart(true, func); };

//! Iterate over all the parts of a geometry
void forEachPart(const std::function<void(const Geometry*)>& func) const { forEachPart(true, func); }

//! Iterate over all the parts of a geometry
void forEachPart(bool includePolygonHoles, const std::function<void(const Geometry*)>& func) const;
//! Iterate over all the parts of a CONST geometry (signature = void(const Geometry* part))
template<typename CALLABLE>
inline void forEachPart(bool includePolygonHoles, CALLABLE&& func) const;
template<typename CALLABLE>
inline void forEachPart(CALLABLE&& func) const { forEachPart(true, func); };

public:
inline Type getType() const { return _type; }
Expand Down Expand Up @@ -560,7 +561,9 @@ namespace osgEarth
const Geometry* next();

//! Visits each part and calls a user-defined functor
inline void forEach(const std::function<void(const Geometry* part)>& func) {
//! Signature = void(const Geometry* part)
template<typename CALLABLE>
inline void forEach(CALLABLE&& func) { //const std::function<void(const Geometry* part)>& func) {
while (hasMore()) func(next());
}

Expand Down Expand Up @@ -620,6 +623,26 @@ namespace osgEarth
bool _closeLoop;
Segment _current;
};



//! Iterate over all the parts of a geometry
template<typename CALLABLE>
inline void Geometry::forEachPart(bool includePolygonHoles, CALLABLE&& func)
{
GeometryIterator i(this, includePolygonHoles);
i.forEach(func);
}

//! Iterate over all the parts of a geometry
template<typename CALLABLE>
inline void Geometry::forEachPart(bool includePolygonHoles, CALLABLE&& func) const
{
ConstGeometryIterator i(this, includePolygonHoles);
i.forEach(func);
}


} // namespace osgEarth


Expand Down
26 changes: 13 additions & 13 deletions src/osgEarth/Geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -696,19 +696,19 @@ Geometry::close()
push_back( front() );
}

void
Geometry::forEachPart(bool includePolygonHoles, const std::function<void(Geometry*)>& func)
{
GeometryIterator i(this, includePolygonHoles);
i.forEach(func);
}

void
Geometry::forEachPart(bool includePolygonHoles, const std::function<void(const Geometry*)>& func) const
{
ConstGeometryIterator i(this, includePolygonHoles);
i.forEach(func);
}
//void
//Geometry::forEachPart(bool includePolygonHoles, const std::function<void(Geometry*)>& func)
//{
// GeometryIterator i(this, includePolygonHoles);
// i.forEach(func);
//}
//
//void
//Geometry::forEachPart(bool includePolygonHoles, const std::function<void(const Geometry*)>& func) const
//{
// ConstGeometryIterator i(this, includePolygonHoles);
// i.forEach(func);
//}

//----------------------------------------------------------------------------

Expand Down
16 changes: 15 additions & 1 deletion tests/feature_rasterize.earth
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ FeatureImage - rasterizes feature data into an image layer.
<url>../data/world.shp</url>
</OGRFeatures>

<FeatureImage name="World Boundaries">
<FeatureImage name="Rasterized polygons + lines">
<features>world-data</features>
<styles>
<style type="text/css">
Expand All @@ -23,4 +23,18 @@ FeatureImage - rasterizes feature data into an image layer.
</styles>
</FeatureImage>

<FeatureImage name="Rasterized points" min_level="5">
<ogrfeatures url="../data/cities_mercator.shp"/>
<styles>
<script language="javascript">
<![CDATA[
function get_style() {
var rank = feature.properties.scalerank;
return "{ point-fill:#7f7fff;point-size:" + (rank*2.5) + "; }";
}
]]>
</script>
<selector style_expr="get_style();"/>
</styles>
</FeatureImage>
</Map>

0 comments on commit 17756d5

Please sign in to comment.