Skip to content

Instantly share code, notes, and snippets.

@ndarilek
Created July 30, 2009 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ndarilek/158862 to your computer and use it in GitHub Desktop.
Save ndarilek/158862 to your computer and use it in GitHub Desktop.
package info.hermesgps.hermes.model
import scala.collection.immutable.Map
import org.openstreetmap.osm.data.IDataSet
import org.openstreetmap.osmosis.core.domain.v0_6.{Tag, WayNode}
import org.openstreetmap.osmosis.core.domain.v0_6.{Entity, Node, Relation, Way}
/*
* Superclass for rich OSM entities.
*/
class RichEntity(val obj:Entity, val dataset:IDataSet) {
/*
* List of tags for this entity.
*/
lazy val tags = Map[String, String](obj.getTags.toArray.map { t =>
val tag = t.asInstanceOf[Tag]
(tag.getKey -> tag.getValue)
}:_*)
/*
* @return name tag for this entity
*/
def name = if(tags.contains("name"))
Some(tags("name"))
else
None
}
/*
* Added functionality for OSM nodes
*/
class RichNode(obj:Node) extends RichEntity(obj, Atlas.dataset) {
/*
* Ways in which this node is listed
*/
lazy val ways = {
val i = dataset.getWaysForNode(obj.getId)
def iterate:List[Way] = i.hasNext match {
case true => i.next :: iterate
case false => Nil
}
iterate
}
/*
* Compute a natural language name for this node. This process is fairly
* specific to a number of factors.
*
* @param way the way for calculating perspective from which the node is being viewed
* @param node node on way opposite of perspective
* @return name for this node
*/
def name(way:Way, from:Node):String = {
import Preamble._
// Does this node already have a name? If so, start with it.
var n:String = super.name.getOrElse("")
if(!n.isEmpty && ways.size > 1) n += ": "
val nw = ways.remove((w:Way) => w == way)
// The node's name varies based on how many ways include it.
ways.size match {
// If only 1, identify it by the way.
case 1 => n += ways(0).name.getOrElse("Unnamed")
// If 2, then the path changes names here.
case 2 =>
n += way.name+" becomes "+nw(0).name
// TODO: Identify node as intersection, determine crossings, direction of passage to member ways, etc.
}
n
}
}
/*
* Added functionality ofr OSM ways.
*/
class RichWay(obj:Way) extends RichEntity(obj, Atlas.dataset) {
/*
* Nodes in this way.
*/
lazy val nodes = List.fromArray(obj.getWayNodes.toArray).map { w =>
dataset.getNodeByID(w.asInstanceOf[WayNode].getNodeId)
}
}
/*
* Implicit conversions.
*/
object Preamble {
implicit def toRichNode(obj:Node) = new RichNode(obj)
implicit def toRichWay(obj:Way) = new RichWay(obj)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment