[OpenLayers-Users] ModifyFeature: Rotate Center Point and
Control Point
carls
carlshe at 163.com
Fri Nov 7 21:31:03 EST 2008
Maybe the name of 'GetCenter' should be changed. The result of this function
is not really the cetriod of the geometry. It is just the average of all its
vertices.
I add this function just for getting a fixed rotate center while
Rotating/Dragging a feature. because the average of all vertices will keep
unchanged while the geometry is rotated or scaled.
Maybe somebody can help improving above code and integrate it to Library.
Thanks.
Eric Lemoine wrote:
>
> Hi. Thanks for sharing your code.
>
> Adding a getCentroid function to the Geometry class would make sense
> to me. Does your getCenter func effectively calculate the centroid?
> Also, the centroid should be cached in a property of the geom to avoid
> calculating it multiple times (the cache should obviously be cleared
> when the coords are changed). That'be great if you could open a ticket
> for that, with your patch.
>
> And yes, I think it'd be good that the modify feature control could be
> configured so that the rotate and drag handles are rendered at the
> centroid of the feature. But I think this should be optional, for the
> cases when it costs too much to calculate the centroid.
> This patch would go in a ticket of its own.
>
> Thanks,
>
> Eric
>
> 2008/11/6, carls <carlshe at 163.com>:
>>
>> I just try to do some modification to ModifyFeature.js and Geometry.js.
>>
>> one function 'GetCenter()' is added to OpenLayers.Geometry, and a little
>> modification is applied to 'collectDragHandle' and 'collectRadiusHandle'
>> of
>> ModifyFeature.js.
>>
>> for Geometry.js:
>>
>>
>> GetCenter: function() {
>> var vertices = new Array();
>> var center = new OpenLayers.Geometry.Point(0, 0);
>> var xx = 0, yy = 0;
>> /**
>> * Method: Collect All Vertices
>> */
>> function collectAllVertices(geometry, vertices) {
>> var i, vertex, component, len;
>> if (geometry.CLASS_NAME == "OpenLayers.Geometry.Point") {
>> vertex = new OpenLayers.Feature.Vector(this);
>> vertices.push(vertex);
>> } else {
>> var numVert = geometry.components.length;
>> if (geometry.CLASS_NAME ==
>> "OpenLayers.Geometry.LinearRing")
>> {
>> numVert -= 1;
>> }
>> for (i = 0; i < numVert; ++i) {
>> component = geometry.components[i];
>> if (component.CLASS_NAME ==
>> "OpenLayers.Geometry.Point")
>> {
>> vertex = new
>> OpenLayers.Feature.Vector(component);
>> vertices.push(vertex);
>> } else {
>> collectAllVertices(component, vertices);
>> }
>> }
>> }
>> }
>>
>> collectAllVertices(this,vertices);
>> if (vertices.length > 0) {
>> for (ii in vertices) {
>> xx += vertices[ii].geometry.x;
>> yy += vertices[ii].geometry.y;
>> }
>> center = new OpenLayers.Geometry.Point(xx / vertices.length,
>> yy
>> / vertices.length);
>> }
>> return center;
>> },
>>
>>
>> for ModifyFeature.js:
>>
>> /**
>> * Method: collectDragHandle
>> * Collect the drag handle for the selected geometry.
>> */
>> collectDragHandle: function() {
>> var geometry = this.feature.geometry;
>> ///var center = geometry.getBounds().getCenterLonLat();
>> //var originGeometry = new OpenLayers.Geometry.Point(
>> // center.lon, center.lat
>> //);
>> var originGeometry = geometry.GetCenter();
>> var origin = new OpenLayers.Feature.Vector(originGeometry);
>> originGeometry.move = function(x, y) {
>> OpenLayers.Geometry.Point.prototype.move.call(this, x, y);
>> geometry.move(x, y);
>> };
>> this.dragHandle = origin;
>> this.layer.addFeatures([this.dragHandle], { silent: true });
>> },
>> /**
>> * Method: collectRadiusHandle
>> * Collect the radius handle for the selected geometry.
>> */
>> collectRadiusHandle: function() {
>> var geometry = this.feature.geometry;
>> var bounds = geometry.getBounds();
>> //var center = bounds.getCenterLonLat();
>> //var originGeometry = new OpenLayers.Geometry.Point(
>> // center.lon, center.lat
>> //);
>> var originGeometry = geometry.GetCenter();
>> var radiusGeometry = new OpenLayers.Geometry.Point(
>> (bounds.right - originGeometry.x) * 1.3 + originGeometry.x,
>> (bounds.bottom - originGeometry.y) * 1.3 + originGeometry.y
>> );
>>
>> var radius = new OpenLayers.Feature.Vector(radiusGeometry);
>> var resize = (this.mode &
>> OpenLayers.Control.ModifyFeature.RESIZE);
>> var rotate = (this.mode &
>> OpenLayers.Control.ModifyFeature.ROTATE);
>> radiusGeometry.move = function(x, y) {
>> OpenLayers.Geometry.Point.prototype.move.call(this, x, y);
>> var dx1 = this.x - originGeometry.x;
>> var dy1 = this.y - originGeometry.y;
>> var dx0 = dx1 - x;
>> var dy0 = dy1 - y;
>> if (rotate) {
>> var a0 = Math.atan2(dy0, dx0);
>> var a1 = Math.atan2(dy1, dx1);
>> var angle = a1 - a0;
>> angle *= 180 / Math.PI;
>> geometry.rotate(angle, originGeometry);
>> }
>> if (resize) {
>> var l0 = Math.sqrt((dx0 * dx0) + (dy0 * dy0));
>> var l1 = Math.sqrt((dx1 * dx1) + (dy1 * dy1));
>> geometry.resize(l1 / l0, originGeometry);
>> }
>> };
>> this.radiusHandle = radius;
>> this.layer.addFeatures([this.radiusHandle], { silent: true });
>> },
>>
>>
>>
>> I tested it and it seems can run well.
>>
>>
>>
>>
>> carls wrote:
>>>
>>> while using ModifyFeature control on a regular polygon, it can be found
>>> the rotate center is not right the real center of a regular polygon?
>>> (try
>>> it with a regular triangle)
>>>
>>> additionally, while rotating and modifying a rectangle parallel to the
>>> axis, the control point for rotating might be at the same position of
>>> the
>>> right-bottom corner of the rectangle, which confuses the action of
>>> rotation and modifition, i.e. what i really need is to rotate the
>>> rectangle, but really I moved the right-bottom corner of it. This can
>>> be
>>> resolved if the control point becomes a little more far to the center
>>> point than any vertex of the geometry.
>>>
>>> It is not a big problem. But it will be better if imporved.
>>>
>>> Thanks!
>>>
>>
>>
>> -----
>> Regards, Carl SHE
>> --
>> View this message in context:
>> http://n2.nabble.com/ModifyFeature%3A-Rotate-Center-Point-and-Control-Point-tp1394002p1464935.html
>> Sent from the OpenLayers Users mailing list archive at Nabble.com.
>>
>> _______________________________________________
>> Users mailing list
>> Users at openlayers.org
>> http://openlayers.org/mailman/listinfo/users
>>
> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users
>
>
-----
Regards, Carl SHE
--
View this message in context: http://n2.nabble.com/ModifyFeature%3A-Rotate-Center-Point-and-Control-Point-tp1394002p1472478.html
Sent from the OpenLayers Users mailing list archive at Nabble.com.
More information about the Users
mailing list