[Mapbender-commits] r6000 - trunk/mapbender/http/plugins
svn_mapbender at osgeo.org
svn_mapbender at osgeo.org
Thu Apr 22 06:34:50 EDT 2010
Author: christoph
Date: 2010-04-22 06:34:49 -0400 (Thu, 22 Apr 2010)
New Revision: 6000
Modified:
trunk/mapbender/http/plugins/mb_digitize_geometry.js
Log:
Modified: trunk/mapbender/http/plugins/mb_digitize_geometry.js
===================================================================
--- trunk/mapbender/http/plugins/mb_digitize_geometry.js 2010-04-22 09:54:46 UTC (rev 5999)
+++ trunk/mapbender/http/plugins/mb_digitize_geometry.js 2010-04-22 10:34:49 UTC (rev 6000)
@@ -1,83 +1,266 @@
+/**
+ * Package: mb_digitize_geometry
+ *
+ * Description:
+ * A button that allows to draw a geometry on maps, for example polygons.
+ *
+ * For other geometries, change the element variable "geometryType", and
+ * of course select another button image.
+ *
+ * Files:
+ * - http/plugins/mb_digitize_geometry.js
+ *
+ * SQL:
+ * > INSERT INTO gui_element(fkey_gui_id, e_id, e_pos, e_public, e_comment,
+ * > e_title, e_element, e_src, e_attributes, e_left, e_top, e_width,
+ * > e_height, e_z_index, e_more_styles, e_content, e_closetag, e_js_file,
+ * > e_mb_mod, e_target, e_requires, e_url) VALUES ('<appId>',
+ * > 'mb_digitize_polygon',1,1,'Digitize a polygon geometry',
+ * > 'Digitize a polygon geometry','img',
+ * > '../img/button_digitize/polygon_off.png','',NULL ,NULL ,24,24,NULL ,'',
+ * > '','','../plugins/mb_digitize_geometry.js','','mapframe1','','');
+ * >
+ * > INSERT INTO gui_element_vars(fkey_gui_id, fkey_e_id, var_name,
+ * > var_value, context, var_type) VALUES('<appId>', 'mb_digitize_polygon',
+ * > 'geometryType', 'polygon', '' ,'var');
+ *
+ * Help:
+ * http://www.mapbender.org/<wiki site name>
+ *
+ * Maintainer:
+ * http://www.mapbender.org/User:Christoph_Baudson
+ *
+ * Parameters:
+ * geometryType - (String) "polygon", "line", "point" or "rectangle"
+ *
+ * License:
+ * Copyright (c) 2009, Open Source Geospatial Foundation
+ * This program is dual licensed under the GNU General Public License
+ * and Simplified BSD license.
+ * http://svn.osgeo.org/mapbender/trunk/mapbender/license/license.txt
+ */
+
var $digitize = $(this);
-var DigitizeApi = function () {
+var DigitizeApi = function (o) {
var that = this;
var featureCollection = new GeometryArray();
var defaults = {
geometryType: "point"
};
- var settings = $.extend(defaults, options);
+ var settings = $.extend(defaults, o);
this.events = {
+ /**
+ * Property: events.finished
+ *
+ * Description:
+ * Triggered after the geometry has been completed.
+ * obj.featureCollection contains the GeoJSON
+ */
finished: new Mapbender.Event(),
+ /**
+ * Property: events.aborted
+ *
+ * Description:
+ * Triggered if the geometry could not be created.
+ */
aborted: new Mapbender.Event(),
+ /**
+ * Property: events.added
+ *
+ * Description:
+ * Triggered after a point has been added to the geometry.
+ * obj.featureCollection contains the feature collection as GeoJSON
+ * obj.point contains the last point as Mapbender.Point
+ */
added: new Mapbender.Event(),
+ /**
+ * Property: events.mousemove
+ *
+ * Description:
+ * Triggered whenever a new point can be added to the geometry.
+ * obj.featureCollection contains the feature collection as GeoJSON
+ * obj.point contains the current mouse position as Mapbender.Point
+ */
mousemove: new Mapbender.Event()
};
var mousemove = function (e) {
- that.events.mousemove.trigger(e);
+ var map = $(this).mapbender();
+ var pt = map.getPos(e);
+ var coord = map.convertPixelToReal(pt);
+
+ // compute feature collection to be delegated
+ var collection = new GeometryArray();
+ collection.importGeoJSON(featureCollection.toString(), false);
+
+ if (settings.geometryType === "point") {
+ // if a point is digitized, we discard it, as we will
+ // display the current position anyway
+ collection = new GeometryArray();
+ }
+ else if (settings.geometryType === "rectangle") {
+ var lastPoint = collection.getPoint(-1, -1, -1);
+ if (lastPoint) {
+ // if the second point of the rectangle is set, add
+ // other points to complete the polygon
+ var points = calculateRectanglePoints(lastPoint, coord);
+ collection.getGeometry(-1, -1).addPoint(points[1]);
+ collection.getGeometry(-1, -1).addPoint(points[2]);
+ collection.getGeometry(-1, -1).addPoint(points[3]);
+ }
+ }
+ else if (
+ settings.geometryType === "polygon"
+ || settings.geometryType === "line"
+ ) {
+ var numCollections = collection.count();
+ if (numCollections > 0) {
+ var numFeatures = collection.get(-1).count();
+ if (numFeatures > 0) {
+ var feature = collection.getGeometry(-1, -1);
+ var ps = feature.count();
+ // if geometry is a polygon, reopen it
+ if (ps > 1 &&
+ feature.geomType === Mapbender.geometryType.polygon
+ ) {
+ collection.delPoint(-1, -1, -1);
+ }
+ // add current point to geometry
+ feature.addPoint(coord);
+ }
+ }
+ }
+ // display current mouse position as point
+ collection.addMember(Mapbender.geometryType.point);
+ collection.get(-1).addGeometry();
+ collection.getGeometry(-1, -1).addPoint(coord);
+
+ that.events.mousemove.trigger({
+ featureCollection: collection.toString()
+ });
};
+ var calculateRectanglePoints = function (p1, p3) {
+ return [
+ p1,
+ new Mapbender.Point(p3.x, p1.y),
+ p3,
+ new Mapbender.Point(p1.x, p3.y)
+ ];
+ };
+
var setPoint = function (e) {
var map = $(this).mapbender();
var pt = map.getPos(e);
var coord = map.convertPixelToReal(pt);
+
+ // do not add same point twice
+ var lastPoint = featureCollection.getPoint(-1,-1,-1);
+ if (lastPoint && coord.equals(featureCollection.getPoint(-1,-1,-1))) {
+ // abort if rectangle
+ if (settings.geometryType === "rectangle") {
+ featureCollection = new GeometryArray();
+ button.stop();
+ }
+ return false;
+ }
+
+ // add point(s)
+ if (settings.geometryType === "rectangle" && lastPoint) {
+ // if the second point of the rectangle is set, add
+ // other points to complete the polygon
+ var points = calculateRectanglePoints(lastPoint, coord);
+ featureCollection.getGeometry(-1, -1).addPoint(points[1]);
+ featureCollection.getGeometry(-1, -1).addPoint(points[2]);
+ featureCollection.getGeometry(-1, -1).addPoint(points[3]);
+ }
+ else {
+ featureCollection.getGeometry(-1, -1).addPoint(coord);
+ }
+
+ // set SRS
featureCollection.getGeometry(-1, -1).setEpsg(map.getSrs());
- // TODO: add snapping!
- featureCollection.getGeometry(-1, -1).addPoint(coord);
-
that.events.added.trigger({
- point: coord
+ point: coord,
+ featureCollection: featureCollection.toString()
});
if (settings.geometryType === Mapbender.geometryType.point) {
return button.stop();
}
};
+
+ var finishFeature = function () {
+ if (featureCollection.count() > 0 &&
+ featureCollection.getGeometry(-1, -1).close()
+ ) {
+ button.stop();
+ }
+ };
+
+ var correctGeometryType = function (str) {
+ if (str === "rectangle") {
+ return Mapbender.geometryType.polygon;
+ }
+ return str;
+ };
var button = new Mapbender.Button({
domElement: $digitize.get(0),
- over: options.src.replace(/_off/, "_over"),
- on: options.src.replace(/_off/, "_on"),
- off: options.src,
- name: options.id,
+ over: o.src.replace(/_off/, "_over"),
+ on: o.src.replace(/_off/, "_on"),
+ off: o.src,
+ name: o.id,
go: function () {
- featureCollection.addMember(settings.geometryType);
+ featureCollection = new GeometryArray();
+ featureCollection.addMember(
+ correctGeometryType(settings.geometryType)
+ );
featureCollection.get(-1).addGeometry();
- options.$target.bind("click", setPoint);
- options.$target.bind("mousemove", mousemove);
+ if (settings.geometryType === "rectangle") {
+ o.$target.bind("mousedown", setPoint);
+ o.$target.bind("mouseup", setPoint);
+ o.$target.bind("mouseup", finishFeature);
+ }
+ else {
+ o.$target.bind("click", setPoint);
+ o.$target.bind("dblclick", finishFeature);
+ }
+ o.$target.bind("mousemove", mousemove);
},
stop: function () {
if (featureCollection.count() > 0) {
- if (!featureCollection.getGeometry(-1, -1).close()) {
- new Mapbender.Exception("Geometry could not be created.");
+ if (!featureCollection.getGeometry(-1, -1).isValid()) {
+ new Mapbender.Exception("Geometry could not be created.");
that.events.aborted.trigger();
}
else {
- var json = featureCollection.toString();
that.events.finished.trigger({
- featureCollection: json
+ featureCollection: featureCollection.toString()
});
}
featureCollection.empty();
}
- options.$target.unbind("click", setPoint);
- options.$target.unbind("mousemove", mousemove);
+ else {
+ new Mapbender.Exception("Geometry could not be created.");
+ that.events.aborted.trigger();
+ }
+ if (settings.geometryType === "rectangle") {
+ o.$target.unbind("mousedown", setPoint);
+ o.$target.unbind("mouseup", setPoint);
+ o.$target.unbind("mouseup", finishFeature);
+ }
+ else {
+ o.$target.unbind("click", setPoint);
+ o.$target.unbind("dblclick", finishFeature);
+ }
+ o.$target.unbind("mousemove", mousemove);
}
});
};
-$digitize.mapbender(new DigitizeApi());
-
-$digitize.mapbender().events.finished.register(function (obj) {
- console.log(obj);
-});
-$digitize.mapbender().events.mousemove.register(function (obj) {
-// console.log(obj);
-});
-$digitize.mapbender().events.added.register(function (obj) {
-// console.log(obj);
-});
+$digitize.mapbender(new DigitizeApi(options));
More information about the Mapbender_commits
mailing list