Class: MapWKT::Geometry

Inherits:
Object
  • Object
show all
Defined in:
lib/mapwkt.rb,
lib/mapwkt/wkt/geometry.rb

Direct Known Subclasses

LineString, Point, Polygon

Defined Under Namespace

Classes: LineString, Point, Polygon

Class Method Summary collapse

Class Method Details

.parse_linestrings(linestrings_string) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/mapwkt/wkt/geometry.rb', line 54

def self.parse_linestrings (linestrings_string)
  # Create LineStrings from each "(x y, …)" in the linestrings string.
  linestrings_string.scan(/\((.*?)\)/).flatten.map do |points_string|
    points = self.parse_points(points_string)
    MapWKT::Geometry::LineString.new(*points)
  end
end

.parse_points(points_string) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/mapwkt/wkt/geometry.rb', line 62

def self.parse_points (points_string)
  # Create Points from each "x y" in the points string.
  points_string.split(?,).map do |x_y_string|
    x, y = self.parse_x_y(x_y_string)
    MapWKT::Geometry::Point.new(y,x)
  end
end

.parse_polygons(polygons_string) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/mapwkt/wkt/geometry.rb', line 46

def self.parse_polygons (polygons_string)
  # Create Polygons from each "((x y, …), (x y, …))" in the polygons string.
  polygons_string.scan(/\((\(.*?\))\)/).flatten.map do |linestrings_string|
    perimeter, lacunae = self.parse_linestrings(linestrings_string)
    MapWKT::Geometry::Polygon.new(perimeter, *lacunae)
  end
end

.parse_wkt(wkt_string) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mapwkt/wkt/geometry.rb', line 4

def self.parse_wkt (wkt_string)
  # Remove extraneous whitespace.
  s = wkt_string.to_s.upcase.strip.gsub(/\s*([),(])\s*/,'\1').gsub(/\s+/,' ')
  
  case s
    when /^POINT\(([\d\s.-]*?)\)$/
      x, y = self.parse_x_y $1
      [MapWKT::Geometry::Point.new(y,x)]
    
    when /^LINESTRING\(([\d\s,.-]*?)\)$/
      points = self.parse_points $1
      [MapWKT::Geometry::LineString.new(*points)]
    
    when /^POLYGON\((\([\d\s,.-]*?\)(?:,\([\d\s,.-]*?\))*)\)$/
      perimeter, lacunae = self.parse_linestrings $1
      [MapWKT::Geometry::Polygon.new(perimeter, *lacunae)]
    
    when /^MULTIPOINT\(([\d\s,.-]*?)\)$/
      self.parse_points $1
    
    when /^MULTIPOINT\((\([\d\s,.-]*?\)(?:,\([\d\s,.-]*?\))*)\)$/
      points_string = $1.gsub(/[()]/,'')
      self.parse_points(points_string)
    
    when /^MULTILINESTRING\((\([\d\s,.-]*?\)(?:,\([\d\s,.-]*?\))*)\)$/
      self.parse_linestrings $1
    
    when /^MULTIPOLYGON\((\(\([\d\s,.-]*?\)(?:,\([\d\s,.-]*?\))*\)(?:,\(\([\d\s,.-]*?\)(?:,\([\d\s,.-]*?\))*\))*)\)$/
      self.parse_polygons $1
    
    when /^GEOMETRYCOLLECTION\((.*?)\)$/
      wkt_strings = $1.scan(/([A-Z]+\(.*?\))(?=,[A-Z]|$)/).flatten
      wkt_strings.map {|wkt_string| self.parse_wkt(wkt_string) }.flatten
    
  else
    raise SyntaxError
  end
  
rescue SyntaxError
  raise SyntaxError, "not a valid WKT string: #{wkt_string.inspect}"
end

.parse_x_y(x_y_string) ⇒ Object

Raises:

  • (SyntaxError)


70
71
72
73
74
75
76
# File 'lib/mapwkt/wkt/geometry.rb', line 70

def self.parse_x_y (x_y_string)
  # Check the string.
  raise SyntaxError unless x_y_string =~ /^(-?\d+(?:\.\d+)?)\s(-?\d+(?:\.\d+)?)$/
  
  # Return the x and y values.
  [$1, $2].map(&:to_f)
end