/usr/include/osgEarthDrivers/scriptengine_javascript/JSGeometry is in libosgearth-dev 2.7.0+dfsg-2+b3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
* Copyright 2015 Pelican Mapping
* http://osgearth.org
*
* osgEarth is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#ifndef OSGEARTHDRIVERS_DUKTAPE_JS_GEOMETRY_H
#define OSGEARTHDRIVERS_DUKTAPE_JS_GEOMETRY_H
#include <osgEarthSymbology/Geometry>
#include <osgEarthFeatures/GeometryUtils>
#include "duktape.h"
#define LC "[duktape] "
namespace osgEarth { namespace Drivers { namespace Duktape
{
using namespace osgEarth::Features;
using namespace osgEarth::Symbology;
struct GeometryAPI
{
static void install(duk_context* ctx)
{
duk_push_c_function(ctx, GeometryAPI::buffer, 2/*numargs*/); // [global, function]
duk_put_prop_string(ctx, -2, "oe_geometry_buffer"); // [global]
duk_push_c_function(ctx, GeometryAPI::getBounds, 1);
duk_put_prop_string(ctx, -2, "oe_geometry_getBounds");
duk_push_c_function(ctx, GeometryAPI::cloneAs, 2);
duk_put_prop_string(ctx, -2, "oe_geometry_cloneAs");
duk_eval_string_noresult(ctx,
"oe_duk_bind_geometry_api = function(geometry) {"
" geometry.getBounds = function() {"
" return oe_geometry_getBounds(this);"
" };"
" geometry.buffer = function(distance) {"
" var result = oe_geometry_buffer(this, distance);"
" return oe_duk_bind_geometry_api(result);"
" };"
" geometry.cloneAs = function(typeName) {"
" var result = oe_geometry_cloneAs(this, typeName);"
" return oe_duk_bind_geometry_api(result);"
" };"
" return geometry;"
"};"
);
}
static void bindToFeature(duk_context* ctx)
{
duk_eval_string_noresult(ctx,
"oe_duk_bind_geometry_api(feature.geometry);" );
}
/**
* buffer operation
* input: 1) geometry GeoJSON, 2) distance
* output: geometry GeoJSON object, or undefined on fail
*/
static duk_ret_t buffer(duk_context* ctx)
{
// arg#0: geometry object
if ( !duk_is_object(ctx, 0) && !duk_is_number(ctx, 1) )
{
OE_WARN << LC << "geometry.buffer(): illegal arguments" << std::endl;
return DUK_RET_TYPE_ERROR;
}
// arg#0 : geometry
std::string geomJSON = duk_json_encode(ctx, 0);
osg::ref_ptr<Geometry> input = GeometryUtils::geometryFromGeoJSON(geomJSON);
if ( !input.valid() )
return DUK_RET_TYPE_ERROR;
// arg#1 : distance
double distance = duk_get_number(ctx, 1);
// run the buffer op:
osg::ref_ptr<Geometry> output;
BufferParameters p;
p._cornerSegs = 0; // speed it up
p._joinStyle = p.JOIN_ROUND;
p._capStyle = p.CAP_ROUND;
if ( input->buffer(distance, output, p) )
{
duk_push_string(ctx, GeometryUtils::geometryToGeoJSON(output.get()).c_str());
duk_json_decode(ctx, -1);
}
else
{
duk_push_undefined(ctx);
}
return 1;
}
// input: GeoJSON geometry
// output: bounds object, e.g. { xmin: num, ymin: num, xmax: num, ymax: num }
static duk_ret_t getBounds(duk_context* ctx)
{
if ( !duk_is_object(ctx, 0) )
{
OE_WARN << LC << "geometry.getBounds(): illegal arguments" << std::endl;
return DUK_RET_TYPE_ERROR;
}
// arg#0 : geometry
std::string geomJSON = duk_json_encode(ctx, 0);
osg::ref_ptr<Geometry> input = GeometryUtils::geometryFromGeoJSON(geomJSON);
if ( !input.valid() )
return DUK_RET_TYPE_ERROR;
Bounds b = input->getBounds();
duk_push_object(ctx);
duk_push_number(ctx, b.xMin());
duk_put_prop_string(ctx, -2, "xmin");
duk_push_number(ctx, b.yMin());
duk_put_prop_string(ctx, -2, "ymin");
duk_push_number(ctx, b.xMax());
duk_put_prop_string(ctx, -2, "xmax");
duk_push_number(ctx, b.yMax());
duk_put_prop_string(ctx, -2, "ymax");
return 1;
}
// input: type name
// output: new geometry
static duk_ret_t cloneAs(duk_context* ctx)
{
// arg#0 : geometry
std::string geomJSON = duk_json_encode(ctx, 0);
osg::ref_ptr<Geometry> input = GeometryUtils::geometryFromGeoJSON(geomJSON);
if ( !input.valid() )
return DUK_RET_TYPE_ERROR;
// arg#1 : type
std::string typeName = osgEarth::toLower( duk_get_string(ctx, 1) );
Geometry::Type type;
if ( typeName == "point" || typeName == "multipoint" )
type = Geometry::TYPE_POINTSET;
else if (typeName == "linestring" || typeName == "multilinestring")
type = Geometry::TYPE_LINESTRING;
else //if (typeName == "polygon")
type = Geometry::TYPE_POLYGON;
// run the buffer op:
osg::ref_ptr<Geometry> output = input->cloneAs(type);
if ( output.valid() )
{
duk_push_string(ctx, GeometryUtils::geometryToGeoJSON(output.get()).c_str());
duk_json_decode(ctx, -1);
}
else
{
duk_push_undefined(ctx);
}
return 1;
}
};
} } } // namespace osgEarth::Drivers::Duktape
#endif // OSGEARTHDRIVERS_DUKTAPE_JS_GEOMETRY_H
|