-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
153 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
core/src/main/scala/com/acervera/osm4scala/utilities/StringTableUtils.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2020 Ángel Cervera Claudio | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
*/ | ||
|
||
package com.acervera.osm4scala.utilities | ||
|
||
import org.openstreetmap.osmosis.osmbinary.osmformat.StringTable | ||
|
||
/** | ||
* Utilities to extract data from StringTable Objects. | ||
*/ | ||
private[osm4scala] trait StringTableUtils { | ||
|
||
private val CHARSET = "UTF-8" | ||
|
||
implicit class StringTableEnricher(stringTable: StringTable) { | ||
|
||
/** | ||
* From a sequence of keys and values indexes, it creates a Map of tags. | ||
* | ||
* @param keys Sequence of indexes pointing to strings used as keys | ||
* @param values Sequence of indexes pointing to strings used as values | ||
* @return Map with tags. | ||
*/ | ||
def extractTags(keys: Seq[Int], values: Seq[Int]): Map[String, String] = | ||
(keys, values).zipped.map { (key, value) => | ||
stringTable.s(key).toString(CHARSET) -> stringTable.s(value).toString(CHARSET) | ||
}.toMap | ||
|
||
/** | ||
* From a sequence of indexes following the sequence pattern `(key,value)*`, it creates a Map of tags. | ||
* | ||
* @param keyValueSequence key,value sequence. | ||
* @return Map with tags. | ||
*/ | ||
def extractTags(keyValueSequence: Iterator[Int]): Map[String, String] = | ||
keyValueSequence | ||
.grouped(2) | ||
.map(tag => stringTable.s(tag.head).toString(CHARSET) -> stringTable.s(tag.last).toString(CHARSET)) | ||
.toMap | ||
|
||
/** | ||
* Extract String from the String table. | ||
* | ||
* @param idx String index. | ||
* @return Proper Scala String. | ||
*/ | ||
def getString(idx: Int): String = stringTable.s(idx).toString(CHARSET) | ||
|
||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
core/src/test/scala/com/acervera/osm4scala/utilities/StringTableUtilsSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2017 Ángel Cervera Claudio | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
*/ | ||
|
||
package com.acervera.osm4scala.utilities | ||
|
||
import com.google.protobuf.ByteString | ||
import org.openstreetmap.osmosis.osmbinary.osmformat.StringTable | ||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.wordspec.AnyWordSpecLike | ||
|
||
class StringTableUtilsSpec extends AnyWordSpecLike with Matchers with StringTableUtils { | ||
|
||
val strTable = StringTable( | ||
(0 to 10).map(idx => ByteString.copyFromUtf8(s"value$idx")) | ||
) | ||
|
||
"StringTableEnricher" should { | ||
"extract tags from one sequences of keys and other of values " in { | ||
strTable.extractTags(Seq(0,5), Seq(2,1)) shouldBe Map( | ||
"value0" -> "value2", | ||
"value5" -> "value1" | ||
) | ||
} | ||
"extract tags from key,value sequence" in { | ||
strTable.extractTags(Seq(0,2,5,1).toIterator) shouldBe Map( | ||
"value0" -> "value2", | ||
"value5" -> "value1" | ||
) | ||
} | ||
"extract the String" in { | ||
strTable.getString(0) shouldBe "value0" | ||
} | ||
} | ||
|
||
} |