GithubHelp home page GithubHelp logo

ghettovoice / ol-rotate-feature Goto Github PK

View Code? Open in Web Editor NEW
28.0 2.0 11.0 713 KB

Rotate feature interaction for OpenLayers

Home Page: https://ghettovoice.github.io/ol-rotate-feature/

License: MIT License

JavaScript 90.84% HTML 9.16%
openlayers ol ol3 openlayers3 openlayers4 openlayers-plugins ol-plugins ol-rotate-feature hacktoberfest

ol-rotate-feature's Introduction

Build Status Tests Coverage Status GitHub tag view on npm License

Rotate feature interaction for OpenLayers

Plugin adds interaction that allows rotating vector features around some anchor.

Installation

Install it with NPM (recommended):

# ES6 version for bundling with Webpack, Rollup or etc.
npm install ol ol-rotate-feature

Or add from CDN:

<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.0.0/build/ol.js"></script>
<script src="https://unpkg.com/ol-rotate-feature@latest/dist/ol-rotate-feature.umd.js"></script>
<script>
  // plugin exports global variable RotateFeatureInteraction
  // in addition it also exported to `ol.interaction.RotateFeature` field (for backward compatibility).
</script>

Options

Option Type Description
features ol.Collection<ol.Feature> The features the interaction works on. Required.
style ol.style.Style | Array<ol.style.Style> | ol.style.StyleFunction | undefined Style of the overlay with interaction helper features.
angle number | undefined Initial angle in radians (positive is counter-clockwise), applied for features already added to collection. Default is 0.
anchor number[] | ol.Coordinate | undefined Initial anchor coordinate. Default is center of features extent.
condition module:ol/events/condition~Condition A function that takes an module:ol/MapBrowserEvent~MapBrowserEvent and returns a boolean to indicate whether that event should be handled. Default is module:ol/events/condition~always

Methods

// Set current angle of interaction features.
RotateFeatureInteraction.prototype.setAngle(angle : number)
// Returns current angle of interaction features.
RotateFeatureInteraction.prototype.getAngle() : number
// Set current anchor position.
RotateFeatureInteraction.prototype.setAnchor(anchor? : number[] | ol.Coordinate)
// Returns current anchor position.
RotateFeatureInteraction.prototype.getAnchor() : number[] | ol.Coordinate | undefined 

Events

All events triggered by the interaction are instances of RotateFeatureEvent.

Members
  • features ol.Collection The features being rotated.
  • angle number Current angle in radians.
  • anchor ol.Coordinate Current anchor position.
Event Arguments Description
rotatestart RotateFeatureEvent Triggered upon feature rotate start.
rotating RotateFeatureEvent Triggered upon feature rotating.
rotateend RotateFeatureEvent Triggered upon feature rotation end.

Example usage:

import { Map, View, Feature } from 'ol'
import { Tile as TileLayer, Vector as VectorLayer } from 'ol/layer'
import { OSM as OSMSource, Vector as VectorSource } from 'ol/source'
import { Point, LineString, Polygon } from 'ol/geom'
import { Select as SelectInteraction } from 'ol/interaction'
import RotateFeatureInteraction from 'ol-rotate-feature'

const point = new Feature({
  name: 'point',
  geometry: new Point([ 2384267.0573564973, 7557371.884852641 ])
})
const line = new Feature({
  name: 'line',
  geometry: new LineString([ [ -603697.2100018249, -239432.60826165066 ], [ 4190433.20404443, 2930563.8287811787 ] ])
})
const polygon = new Feature({
  name: 'polygon',
  geometry: new Polygon([ [
    [ -14482348.171434438, 6661491.741627443 ],
    [ -9541458.663080638, 6221214.458704827 ],
    [ -11473786.738129886, 3300708.4819848104 ],
    [ -14482348.171434438, 6661491.741627443 ]
  ] ])
})

const map = new Map({
  view: new View({
    center: [ 0, 0 ],
    zoom: 2
  }),
  layers: [
    new TileLayer({
      source: new OSMSource()
    }),
    new VectorLayer({
      source: new VectorSource({
        projection: 'EPSG:33857',
        features: [ point, line, polygon ]
      })
    })
  ],
  target: 'map',
  projection: 'EPSG:3857'
})

const select = new SelectInteraction()
select.getFeatures().extend([ point, line, polygon ])

const rotate = new RotateFeatureInteraction({
  features: select.getFeatures(),
  anchor: [ 0, 0 ],
  angle: -90 * Math.PI / 180
})

rotate.on('rotatestart', evt => console.log('rotate start', evt))
rotate.on('rotating', evt => console.log('rotating', evt))
rotate.on('rotateend', evt => console.log('rotate end', evt))

map.addInteraction(select)
map.addInteraction(rotate)

Example of usage in Browser environment in test/umd.html.

Getting total angle or last anchor coordinates after rotation:

rotate.on('rotateend', evt => {
    // get total angle in degrees
    console.log(evt.angle + ' is '+ (-1 * evt.angle * 180 / Math.PI ) + '°')
    // get last anchor coordinates
    console.log(evt.anchor)
})

License

MIT (c) 2016-2024, Vladimir Vershinin

ol-rotate-feature's People

Contributors

dependabot[bot] avatar ghettovoice avatar rpfilipe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

ol-rotate-feature's Issues

Prevent anchor from moving?

I'm looking at the demo and am curious if you're able to disallow movement of the anchor. Ie. I want the anchor to begin at the 0,0 of the feature and not be movable.

Thanks.

P.S. this ol Interaction is fantastic. Thank you for authoring it!

Upgrade to ol 8

Hello,
please make changes to support openlayers 8.

I created a pull request (#24) that should do the trick.

Thanks, Vojtech Osvald

Rename package

Rename repo and package name to "ol-tilecache" to avoid confusion with a supported version of OpenLayers

Upgrading to 3.1.0

getting this error when trying to upgrade to 3.1.0

npm ERR! code EINVALIDTAGNAME
npm ERR! Invalid tag name "^6.0.0|^7.0.0" of package "ol@^6.0.0|^7.0.0": Tags may not have any characters that encodeURIComponent encodes.

npm logs:

x@  14 user  staff    448 Jun 23 17:19 _npx
-rw-r--r--@   1 user  staff      0 Jul 25 15:42 _update-notifier-last-checked
 199   │ 198 timing idealTree Completed in 62619ms
 200   │ 199 timing command:i Completed in 62624ms
 201   │ 200 verbose stack Error: Invalid tag name "^6.0.0|^7.0.0" of package "ol@^6.0.0|^7.0.0": Tags may not have any characters that encodeURIComponent e
       │ ncodes.
 202   │ 200 verbose stack     at invalidTagName (/Users/user/.nvm/versions/node/v18.17.0/lib/node_modules/npm/node_modules/npm-package-arg/lib/npa
       │ .js:118:15)
 203   │ 200 verbose stack     at fromRegistry (/Users/user/.nvm/versions/node/v18.17.0/lib/node_modules/npm/node_modules/npm-package-arg/lib/npa.j
       │ s:426:13)
 204   │ 200 verbose stack     at Function.resolve (/Users/user/.nvm/versions/node/v18.17.0/lib/node_modules/npm/node_modules/npm-package-arg/lib/n
       │ pa.js:87:12)
 205   │ 200 verbose stack     at #nodeFromEdge (/Users/user/.nvm/versions/node/v18.17.0/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arb
       │ orist/build-ideal-tree.js:1058:37)
 206   │ 200 verbose stack     at async #loadPeerSet (/Users/user/.nvm/versions/node/v18.17.0/lib/node_modules/npm/node_modules/@npmcli/arborist/li
       │ b/arborist/build-ideal-tree.js:1321:23)
 207   │ 200 verbose stack     at async #buildDepStep (/Users/user/.nvm/versions/node/v18.17.0/lib/node_modules/npm/node_modules/@npmcli/arborist/l
       │ ib/arborist/build-ideal-tree.js:917:11)
 208   │ 200 verbose stack     at async Arborist.buildIdealTree (/Users/user/.nvm/versions/node/v18.17.0/lib/node_modules/npm/node_modules/@npmcli/
       │ arborist/lib/arborist/build-ideal-tree.js:196:7)
 209   │ 200 verbose stack     at async Promise.all (index 1)
 210   │ 200 verbose stack     at async Arborist.reify (/Users/user/.nvm/versions/node/v18.17.0/lib/node_modules/npm/node_modules/@npmcli/arborist/
       │ lib/arborist/reify.js:159:5)
 211   │ 200 verbose stack     at async Install.exec (/Users/user/.nvm/versions/node/v18.17.0/lib/node_modules/npm/lib/commands/install.js:147:5)
 212   │ 201 verbose cwd /Users/user/Documents/Projects/eTopocart/geo360-front
 213   │ 202 verbose Darwin 22.6.0
 214   │ 203 verbose node v18.17.0
 215   │ 204 verbose npm  v9.6.7
 216   │ 205 error code EINVALIDTAGNAME
 217   │ 206 error Invalid tag name "^6.0.0|^7.0.0" of package "ol@^6.0.0|^7.0.0": Tags may not have any characters that encodeURIComponent encodes.
 218   │ 207 verbose exit 1
 219   │ 208 timing npm Completed in 62754ms
 220   │ 209 verbose unfinished npm timer reify 1690310974766
 221   │ 210 verbose unfinished npm timer reify:loadTrees 1690310974768
 222   │ 211 verbose unfinished npm timer idealTree:buildDeps 1690310974778
 223   │ 212 verbose unfinished npm timer idealTree:#root 1690310974778
 224   │ 213 verbose code 1
 225   │ 214 error A complete log of this run can be found in: /Users/user/.npm/_logs/2023-07-25T18_49_34_643Z-debug-0.log

Open Layers 5 support?

With the arrival of the new version (5.0.0), i want to know if its going to be updated or if works normally? Thanks

Problem with OpenLayers 5.3 with Angular 7.0.3

When using the last update (5.3) of OpenLayers with Angular 7.0.3 this warning is shown when serving the app:

WARNING in ./node_modules/ol-rotate-feature/dist/bundle.es.js 760:9-20
"export 'handleEvent' was not found in 'ol/interaction/Pointer'
i 「wdm」: Compiled with warnings.

That causes this error when trying to rotate a feature in the application:

ERROR TypeError: Cannot read property 'call' of undefined
    at RotateFeatureInteraction.handleEvent$1 [as handleEvent]
...

Anchor does not reset after feature is changed

I've noticed that the interaction does not re-calculate the anchor property on a feature if it is already present when creating the new interaction. This was a problem because I am working on an app that involves manipulating a feature with different interactions, and the anchor wouldn't update if a feature had been moved. This led me to have to write feature.U.anchor = [] after the other events were finished to correctly update the anchor. If you like, I can create a PR to fix this once I look through this code!

Example to overrule styles

Hi,
happy new year and thank you providing us this ol-interaction :)
Could y give us an example how we can overrule css styles to hide the anchor icon for example or setting font color to the caption showing current angle on map?

Thx and best regards,
Nic

how to rotate a Icon?

const point = new Feature({
name: 'point',
geometry: new Point([2384267.0573564973, 7557371.884852641])
})
point.setStyle([
new Style({
image: new Icon({
size: [112, 30], // 尺寸
src: zhuchi // 图片url
// scale: 0.1 // 缩放比例
})
})
])

Openlayers 7 support?

We have been using this with OL 7.4.0 and it seems to work fine

However yarn install complains:
warning " > [email protected]" has incorrect peer dependency "ol@^6.0.0".

Could the peer dependencies be updated?

Removing rotate interaction

Hello, i have this method of adding the rotate interaction, but removing it is not possible. How is the way to achieve this?

rotateStatus = false;
  onRotateMode() {
    this.rotateStatus = !this.rotateStatus;
    let rotate;
    if (this.selectedFeatures) {
      rotate = new RotateFeatureInteraction({
        features: this.selectedFeatures
      });
    }
    if (this.rotateStatus) {
      this.map.addInteraction(rotate);
    } else {
     
      rotate = new RotateFeatureInteraction({
        features: null
      });
      this.map.removeInteraction(rotate);
    }
   
  }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.