Skip to content

Instantly share code, notes, and snippets.

@jankeesvw
Created June 30, 2010 07:30
Show Gist options
  • Save jankeesvw/458361 to your computer and use it in GitHub Desktop.
Save jankeesvw/458361 to your computer and use it in GitHub Desktop.
package nl.gemeentedeurne.model.utils {
import inlet42.util.Random;
import com.google.maps.LatLng;
/**
* @author Jankees van Woezik / Base42.nl
*/
public class PointParser {
private static const SHAPE_DELIMITER : String = "||";
private static const TYPE_DELIMITER : String = "::";
private static const POINT_DELIMITER : String = ":";
private static const POINTS_DELIMITER : String = ",";
/**
* @param inShapeString {51.48031493051618:5.734144921246838},{51.48130381759007:5.739380593243908}
* @return [LatLng(),LatLng()]
*/
private static function parseShape(inShapeString : String) : Array {
var split : Array = inShapeString.split(POINTS_DELIMITER);
var result : Array = new Array();
var leni : uint = split.length;
for (var i : uint = 0;i < leni;i++) {
var part : String = split[i] as String;
var lat : Number = Number(part.substr(1, part.lastIndexOf(POINT_DELIMITER) - 1));
var lng : Number = Number(part.substr(part.lastIndexOf(POINT_DELIMITER) + 1, part.length - part.lastIndexOf(POINT_DELIMITER) - 2));
var latLng : LatLng = new LatLng(lat, lng);
result.push(latLng);
}
return result;
}
/**
* @param inString example: {fill}::{51.48031493051618:5.734144921246838},{51.48130381759007:5.739380593243908},{51.47907210959733:5.739917035046886}&{51.47907210959733:5.739917035046886}
* @return fill
*/
public static function getType(inDataString : String) : String {
return String(inDataString.split(TYPE_DELIMITER)[0]).substr(1, String(inDataString.split(TYPE_DELIMITER)[0]).length - 2);
}
/**
* @param inString {fill}::{51.48031493051618:5.734144921246838},{51.48130381759007:5.739380593243908},{51.47907210959733:5.739917035046886}&{51.47907210959733:5.739917035046886}
* @return [LatLng(),LatLng(),LatLng()],[LatLng()]
*/
public static function getShapes(inDataString : String) : Array {
var result : Array = [];
if(inDataString.split(TYPE_DELIMITER).length < 2) return result;
var allShapesString : String = String(inDataString.split(TYPE_DELIMITER)[1]);
var shapes : Array = allShapesString.split(SHAPE_DELIMITER);
var leni : uint = shapes.length;
for (var i : uint = 0;i < leni;i++) {
result.push(parseShape(shapes[i]));
}
return result;
}
/**
* @param inType Type of the tree @see TreeTypes
* @param inShapes Array of Arrays with @see LatLng
*/
public static function coordinatesToString(inType : String, inShapes : Array) : String {
var result : String = "{" + inType + "}" + TYPE_DELIMITER;
var point : LatLng;
for each (var shape : Array in inShapes) {
var leni : uint = shape.length;
for (var i : uint = 0;i < leni;i++) {
point = shape[i];
result += "{" + point.lat() + POINT_DELIMITER + point.lng() + "},";
}
result = result.substr(0, result.length - 1);
result += SHAPE_DELIMITER;
}
result = result.substr(0, result.length - SHAPE_DELIMITER.length);
return result;
}
/**
* For debugging only
* @return Random Latlng
*/
public static function getRandomPoint() : LatLng {
//
return new LatLng(Random.between(51.48010001366223, 51.389780068199364, true), Random.between(5.758209228515625, 5.9470367431640625, true));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment