1 package net.systemeD.halcyon.connection {
3 import flash.events.EventDispatcher;
4 import flash.utils.Dictionary;
6 import net.systemeD.halcyon.connection.actions.*;
8 /** An Entity is an object stored in the map database, and therefore uploaded and downloaded. This includes Nodes, Ways,
9 * Relations but also Changesets etc. */
10 public class Entity extends EventDispatcher {
11 private var _id:Number;
12 private var _version:uint;
13 private var _uid:Number;
14 private var _timestamp:String;
15 private var tags:Object = {};
16 private var modified:Boolean = false;
17 private var _loaded:Boolean = true;
18 private var parents:Dictionary = new Dictionary();
19 /** Lock against purging when off-screen */
20 public var locked:Boolean = false;
21 public var deleted:Boolean = false;
22 /** Have all its parents (ie, relations that contain this object as a member, ways that contain this node) been loaded into memory */
23 public var parentsLoaded:Boolean = true;
25 public function Entity(id:Number, version:uint, tags:Object, loaded:Boolean, uid:Number, timestamp:String) {
27 this._version = version;
29 this._timestamp = timestamp;
31 this._loaded = loaded;
36 public function get id():Number {
40 /** Current version number. */
41 public function get version():uint {
45 /** User ID who last edited this entity (from OSM API). */
46 public function get uid():Number {
50 /** Is entity fully loaded, or is it just a placeholder reference (as a relation member)? */
51 public function get loaded():Boolean {
55 /** List of entities. Overridden by EntityCollection. */
56 public function get entities():Array {
60 /** Most recent modification of the entity (from OSM API). */
61 public function get timestamp():String {
65 /** Set a bunch of properties in one hit. Implicitly makes entity not deleted. */
66 public function updateEntityProperties(version:uint, tags:Object, loaded:Boolean, parentsLoaded:Boolean, uid:Number, timestamp:String):void {
67 _version=version; this.tags=tags; _loaded=loaded; this.parentsLoaded=parentsLoaded; _uid = uid; _timestamp = timestamp;
71 /** Assign a new ID and version. */
72 public function renumber(newID:Number, newVersion:uint):void {
74 this._version = newVersion;
77 // Tag-handling methods
79 /** Whether the entity has > 0 tags. */
80 public function hasTags():Boolean {
81 for (var key:String in tags)
86 /** Whether the entity has any tags other than meta-tags (attribution, created_by, source, tiger:...) */
87 public function hasInterestingTags():Boolean {
88 for (var key:String in tags) {
89 if (key != "attribution" && key != "created_by" && key != "source" && key.indexOf('tiger:') != 0) {
97 /** Rough function to detect entities untouched since TIGER import. */
98 public function isUneditedTiger():Boolean {
99 // todo: make this match the rules from the tiger edited map
100 // http://github.com/MapQuest/TIGER-Edited-map/blob/master/inc/layer-tiger.xml.inc
101 if (this is Way && (uid == 7168 || uid == 15169 || uid == 20587)) {//todo fixme etc
107 /** Retrieve a tag by key. */
108 public function getTag(key:String):String {
112 /** @return true if there exists key=value */
113 public function tagIs(key:String,value:String):Boolean {
114 if (!tags[key]) { return false; }
115 return tags[key]==value;
118 /** Set key=value, with optional undoability.
119 * @param key Name of key to set
120 * @param value Value to set tag to
121 * @param performAction Single-argument function to pass a SetTagAction to.
122 * @example setTag("highway", "residential", MainUndoStack.getGlobalStack().addAction);
124 public function setTag(key:String, value:String, performAction:Function):void {
125 performAction(new SetTagAction(this, key, value));
128 /** Change oldKey=[value] to newKey=[value], with optional undoability.
129 * @param oldKey Name of key to rename
130 * @parame newKey New name of key
131 * @param performAction Single-argument function to pass a SetTagKeyAction to.
132 * @example renameTag("building", "amenity", MainUndoStack.getGlobalStack().addAction);
134 public function renameTag(oldKey:String, newKey:String, performAction:Function):void {
135 performAction(new SetTagKeyAction(this, oldKey, newKey));
138 public function getTagList():TagList {
139 return new TagList(tags);
142 /** Returns an object that duplicates the tags on this entity. */
143 public function getTagsCopy():Object {
144 var copy:Object = {};
145 for (var key:String in tags )
146 copy[key] = tags[key];
150 public function getTagsHash():Object {
151 // hm, not sure we should be doing this, but for read-only purposes
152 // it's faster than using getTagsCopy
156 /** Returns an array that duplicates the tags on this entity. */
157 public function getTagArray():Array {
159 for (var key:String in tags )
160 copy.push(new Tag(this, key, tags[key]));
164 // Clean/dirty methods
166 /** Check if entity is modified since last markClean(). */
167 public function get isDirty():Boolean {
171 /** Reset modified flag. You should not be calling this directly, instead you should be calling markClean from your UndoableEntityAction */
172 public function markClean():void {
176 /** Set entity as modified. You should not be calling this directly, instead you should be calling markDirty from your UndoableEntityAction */
177 internal function markDirty():void {
182 /** Delete entity - must be overridden. */
183 public function remove(performAction:Function):void {
187 /** Whether entity is marked deleted. */
188 public function isDeleted():Boolean {
192 /** Mark entity as deleted. */
193 public function setDeletedState(isDeleted:Boolean):void {
196 var n:Node = Node(this);
198 Connection.getConnection().removeDupe(n);
200 Connection.getConnection().addDupe(n);
205 /** Whether entity is "empty" - to be overridden by subclass. */
206 internal function isEmpty():Boolean {
210 /** Free up memory by converting entity to a dummy entity, for entities that we no longer need
211 * but which are part of a still-in-memory relation */
212 public function nullify():void {
216 /** Implement nullifybehaviour: delete tags, etc. */
217 protected function nullifyEntity():void {
218 // this is the common nullify behaviour for all entity types (we'd call this by super() if ActionScript let us)
225 public function within(left:Number,right:Number,top:Number,bottom:Number):Boolean {
226 return true; // to be overridden
229 public function removeFromParents(performAction:Function):void {
230 for (var o:Object in parents) {
231 if (o is Relation) { Relation(o).removeMember(this, performAction); }
232 else if (o is Way) { Way(o).removeNode(Node(this), performAction); }
233 if (o.isEmpty()) { o.remove(performAction); }
239 /** Create parent link from this entity to another. */
240 public function addParent(parent:Entity):void {
241 parents[parent]=true;
243 if ( parent is Relation )
244 dispatchEvent(new RelationMemberEvent(Connection.ADDED_TO_RELATION, this, parent as Relation, -1));
247 /** Remove parent link. */
248 public function removeParent(parent:Entity):void {
249 delete parents[parent];
251 if ( parent is Relation )
252 dispatchEvent(new RelationMemberEvent(Connection.REMOVED_FROM_RELATION, this, parent as Relation, -1));
255 /** Get array of all Ways of which this object (presumably a node) is a child. */
256 public function get parentWays():Array {
258 for (var o:Object in parents) {
259 if (o is Way) { a.push(o); }
264 /** Whether this entity has any parents. */
265 public function get hasParents():Boolean {
266 for (var o:Object in parents) { return true; }
270 /** Whether this entity has any parents that are Ways. */
271 public function get hasParentWays():Boolean {
272 for (var o:Object in parents) {
273 if (o is Way) { return true; }
278 /** How many parents are Ways? */
279 public function get numParentWays():uint {
281 for (var o:Object in parents) {
282 if (o is Way) { i++; }
287 /** All parents that are Relations */
288 public function get parentRelations():Array {
290 for (var o:Object in parents) {
291 if (o is Relation) { a.push(o); }
296 /** Returns parents that are relations, and of the specified type, and of which this entity is the correct role (if provided).
298 * @example entity.findParentRelationsOfType('multipolygon','inner');
300 public function findParentRelationsOfType(type:String, role:String=null):Array {
302 for (var o:Object in parents) {
303 if (o is Relation && Relation(o).tagIs('type',type) && (role==null || Relation(o).hasMemberInRole(this,role))) {
310 public function getRelationMemberships():Array {
311 var memberships:Array = [];
312 for each( var rel:Relation in parentRelations ) {
313 for each( var memberIndex:int in rel.findEntityMemberIndexes(this)) {
318 role: rel.getMember(memberIndex).role,
319 description: rel.getDescription(),
320 id_idx: rel.id + "/"+memberIndex });
326 /** How many parents does this entity have that satisfy the "within" constraint? */
327 public function countParentObjects(within:Object):uint {
329 for (var o:Object in parents) {
330 if (o.getType()==within.entity && o.getTag(within.k)) {
331 if (within.v && within.v!=o.getTag(within.k)) { break; }
332 if (within.role && !Relation(o).hasMemberInRole(this,within.role)) { break; }
339 /** All parents of this entity. */
340 public function get parentObjects():Array {
342 for (var o:Object in parents) { a.push(o); }
346 /** Whether 'entity' is a parent of this Entity. */
347 public function hasParent(entity:Entity):Boolean {
348 return parents[entity] == true;
351 /** Returns all relations that this Entity is part of, as array of {relation, position, role}, sorted by position. */
352 public function get memberships():Array {
354 for (var o:Object in parents) {
356 for (var i:uint=0; i<o.length; i++) {
357 if (o.getMember(i).entity==this) {
358 list.push( { relation:o, position:i, role: o.getMember(i).role } );
363 // it's useful to return in a sorted order, even if the relations are interleaved
364 // e.g. [{r0 p1},{r1 p1},{r0 p4}]
365 return list.sortOn("position");
370 /** Temporarily prevent redrawing of the object. */
371 public function suspend():void {
372 dispatchEvent(new EntityEvent(Connection.SUSPEND_REDRAW, this));
374 /** Resume redrawing of the object */
375 public function resume():void {
376 dispatchEvent(new EntityEvent(Connection.RESUME_REDRAW, this));
380 /** Basic description of Entity - should be overriden by subclass. */
381 public function getDescription():String {
382 var basic:String=this.getType()+" "+_id;
383 if (tags['ref'] && tags['name']) { return tags['ref']+' '+tags['name']+' ('+basic+')'; }
384 if (tags['ref']) { return tags['ref']+' ('+basic+')'; }
385 if (tags['name']) { return tags['name']+' ('+basic+')'; }
389 /** The type of Entity (node, way etc). By default, returns ''. */
390 public function getType():String {
394 /** Compare type against supplied string */
395 public function isType(str:String):Boolean {
396 return getType()==str;
399 /** Copy tags from another entity into this one, creating "key=value1; value2" pairs if necessary.
400 * * @return Array of keys that require manual merging, in order to warn the user. */
401 public function mergeTags(source: Entity, performAction:Function):Array {
402 var sourcetags:Object = source.getTagsHash();
403 var problem_keys:Array=new Array();
404 for (var k:String in sourcetags) {
405 var v1:String = tags[k];
406 var v2:String = sourcetags[k];
407 if ( v1 && v1 != v2) {
408 // This can create broken tags (does anything support "highway=residential; tertiary"?).
409 // Probably better to do something like:
410 // highway=residential
411 // highway:tomerge=tertiary
413 setTag(k, v1+"; "+v2, performAction);
414 problem_keys.push(k);
416 setTag(k, v2, performAction);
419 if (problem_keys.length > 0)