ppe-postgis-adapter 0.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,886 @@
1
+ # -*- coding: utf-8 -*-
2
+ # #
3
+ #
4
+ # COMMON GEOMETRICAL FUNCTIONS
5
+ #
6
+ # The methods here can be used by all geoms.
7
+ #
8
+
9
+ module PostgisFunctions
10
+
11
+ #
12
+ # True if the given geometries represent the same geometry.
13
+ # Directionality is ignored.
14
+ #
15
+ # Returns TRUE if the given Geometries are "spatially equal".
16
+ # Use this for a 'better' answer than '='. Note by spatially equal we
17
+ # mean ST_Within(A,B) = true and ST_Within(B,A) = true and also mean ordering
18
+ # of points can be different but represent the same geometry structure.
19
+ # To verify the order of points is consistent, use ST_OrderingEquals
20
+ # (it must be noted ST_OrderingEquals is a little more stringent than
21
+ # simply verifying order of points are the same).
22
+ #
23
+ # This function will return false if either geometry is invalid even
24
+ # if they are binary equal.
25
+ #
26
+ # Returns Boolean ST_Equals(geometry A, geometry B);
27
+ #
28
+ def spatially_equal?(other)
29
+ postgis_calculate(:equals, [self, other])
30
+ end
31
+
32
+ #
33
+ # Returns the minimum bounding box for the supplied geometry, as a geometry.
34
+ # The polygon is defined by the corner points of the bounding box
35
+ # ((MINX, MINY), (MINX, MAXY), (MAXX, MAXY), (MAXX, MINY), (MINX, MINY)).
36
+ # PostGIS will add a ZMIN/ZMAX coordinate as well/
37
+ #
38
+ # Degenerate cases (vertical lines, points) will return a geometry of
39
+ # lower dimension than POLYGON, ie. POINT or LINESTRING.
40
+ #
41
+ # In PostGIS, the bounding box of a geometry is represented internally using
42
+ # float4s instead of float8s that are used to store geometries. The bounding
43
+ # box coordinates are floored, guarenteeing that the geometry is contained
44
+ # entirely within its bounds. This has the advantage that a geometry's
45
+ # bounding box is half the size as the minimum bounding rectangle,
46
+ # which means significantly faster indexes and general performance.
47
+ # But it also means that the bounding box is NOT the same as the minimum
48
+ # bounding rectangle that bounds the geome.
49
+ #
50
+ # Returns GeometryCollection ST_Envelope(geometry g1);
51
+ #
52
+ def envelope
53
+ postgis_calculate(:envelope, self)
54
+ end
55
+
56
+ #
57
+ # Computes the geometric center of a geometry, or equivalently,
58
+ # the center of mass of the geometry as a POINT. For [MULTI]POINTs, this is
59
+ # computed as the arithmetric mean of the input coordinates.
60
+ # For [MULTI]LINESTRINGs, this is computed as the weighted length of each
61
+ # line segment. For [MULTI]POLYGONs, "weight" is thought in terms of area.
62
+ # If an empty geometry is supplied, an empty GEOMETRYCOLLECTION is returned.
63
+ # If NULL is supplied, NULL is returned.
64
+ #
65
+ # The centroid is equal to the centroid of the set of component Geometries of
66
+ # highest dimension (since the lower-dimension geometries contribute zero
67
+ # "weight" to the centroid).
68
+ #
69
+ # Computation will be more accurate if performed by the GEOS module (enabled at compile time).
70
+ #
71
+ # http://postgis.refractions.net/documentation/manual-svn/ST_Centroid.html
72
+ #
73
+ # Returns Geometry ST_Centroid(geometry g1);
74
+ #
75
+ def centroid
76
+ postgis_calculate(:centroid, self)
77
+ end
78
+
79
+ #
80
+ # Returns the closure of the combinatorial boundary of this Geometry.
81
+ # The combinatorial boundary is defined as described in section 3.12.3.2 of the
82
+ # OGC SPEC. Because the result of this function is a closure, and hence topologically
83
+ # closed, the resulting boundary can be represented using representational
84
+ # geometry primitives as discussed in the OGC SPEC, section 3.12.2.
85
+ #
86
+ # Do not call with a GEOMETRYCOLLECTION as an argument.
87
+ #
88
+ # Performed by the GEOS module.
89
+ #
90
+ # Returns Geometry ST_Boundary(geometry geomA);
91
+ #
92
+ def boundary
93
+ postgis_calculate(:boundary, self)
94
+ end
95
+
96
+ #
97
+ # 2D minimum cartesian distance between two geometries in projected units.
98
+ #
99
+ # Returns Float ST_Distance(geometry g1, geometry g2);
100
+ #
101
+ def distance_to(other)
102
+ postgis_calculate(:distance, [self, other]).to_f
103
+ end
104
+
105
+ #
106
+ # True if geometry A is completely inside geometry B.
107
+ #
108
+ # For this function to make sense, the source geometries must both be of the same
109
+ # coordinate projection, having the same SRID. It is a given that
110
+ # if ST_Within(A,B) is true and ST_Within(B,A) is true, then the
111
+ # two geometries are considered spatially equal.
112
+ #
113
+ # This function call will automatically include a bounding box comparison that will
114
+ # make use of any indexes that are available on the geometries. To avoid index use,
115
+ # use the function _ST_Within.
116
+ #
117
+ # Do not call with a GEOMETRYCOLLECTION as an argument
118
+ # Do not use this function with invalid geometries. You will get unexpected results.
119
+ #
120
+ # Performed by the GEOS module.
121
+ #
122
+ # Returns Boolean ST_Within(geometry A, geometry B);
123
+ #
124
+ def within? other
125
+ postgis_calculate(:within, [self, other])
126
+ end
127
+
128
+ #
129
+ # True if the geometries are within the specified distance of one another.
130
+ # The distance is specified in units defined by the spatial reference system
131
+ # of the geometries. For this function to make sense, the source geometries
132
+ # must both be of the same coorindate projection, having the same SRID.
133
+ #
134
+ # Returns boolean ST_DWithin(geometry g1, geometry g2, double precision distance);
135
+ #
136
+ def d_within?(other, margin=0.1)
137
+ postgis_calculate(:dwithin, [self, other], margin)
138
+ end
139
+ alias_method "in_bounds?", "d_within?"
140
+
141
+ #
142
+ # True if geometry B is completely inside geometry A.
143
+ #
144
+ # For this function to make sense, the source geometries must both be of the same
145
+ # coordinate projection, having the same SRID. 'contains?' is the inverse of 'within?'.
146
+ #
147
+ # So a.contains?(b) is like b.within?(a) except in the case of invalid
148
+ # geometries where the result is always false regardless or not defined.
149
+ #
150
+ # Do not call with a GEOMETRYCOLLECTION as an argument
151
+ # Do not use this function with invalid geometries. You will get unexpected results.
152
+ #
153
+ # Performed by the GEOS module
154
+ #
155
+ # Returns Boolean ST_Contains(geometry geomA, geometry geomB);
156
+ #
157
+ def contains? other
158
+ postgis_calculate(:contains, [self, other])
159
+ end
160
+
161
+ #
162
+ # True if no point in Geometry A is outside Geometry B
163
+ #
164
+ # This function call will automatically include a bounding box comparison that
165
+ # will make use of any indexes that are available on the geometries. To avoid
166
+ # index use, use the function _ST_CoveredBy.
167
+ #
168
+ # Do not call with a GEOMETRYCOLLECTION as an argument.
169
+ # Do not use this function with invalid geometries. You will get unexpected results.
170
+ #
171
+ # Performed by the GEOS module.
172
+ #
173
+ # Aliased as 'inside?'
174
+ #
175
+ # Returns Boolean ST_CoveredBy(geometry geomA, geometry geomB);
176
+ #
177
+ def covered_by? other
178
+ postgis_calculate(:coveredby, [self, other])
179
+ end
180
+ alias_method "inside?", "covered_by?"
181
+
182
+ #
183
+ # Eye-candy. See 'covered_by?'.
184
+ #
185
+ # Returns !(Boolean ST_CoveredBy(geometry geomA, geometry geomB);)
186
+ #
187
+ def outside? other
188
+ !covered_by? other
189
+ end
190
+
191
+ #
192
+ # True if the Geometries do not "spatially intersect" - if they
193
+ # do not share any space together.
194
+ #
195
+ # Overlaps, Touches, Within all imply geometries are not spatially disjoint.
196
+ # If any of the aforementioned returns true, then the geometries are not
197
+ # spatially disjoint. Disjoint implies false for spatial intersection.
198
+ #
199
+ # Do not call with a GEOMETRYCOLLECTION as an argument.
200
+ #
201
+ # Returns boolean ST_Disjoint( geometry A , geometry B );
202
+ #
203
+ def disjoint? other
204
+ postgis_calculate(:disjoint, [self, other])
205
+ end
206
+
207
+ #
208
+ # How many dimensions the geom is made of (2, 3 or 4)
209
+ #
210
+ # Returns Integer ST_Dimension(geom g1)
211
+ #
212
+ def dimension
213
+ postgis_calculate(:dimension, self).to_i
214
+ end
215
+
216
+ #
217
+ # Returns a "simplified" version of the given geometry using the Douglas-Peuker
218
+ # algorithm. Will actually do something only with (multi)lines and (multi)polygons
219
+ # but you can safely call it with any kind of geometry. Since simplification
220
+ # occurs on a object-by-object basis you can also feed a GeometryCollection to this
221
+ # function.
222
+ #
223
+ # Note that returned geometry might loose its simplicity (see 'is_simple?').
224
+ # Topology may not be preserved and may result in invalid geometries.
225
+ # Use 'simplify_preserve_topology' to preserve topology.
226
+ #
227
+ # Performed by the GEOS Module.
228
+ #
229
+ # Returns Geometry ST_Simplify(geometry geomA, float tolerance);
230
+ #
231
+ def simplify(tolerance=0.1)
232
+ postgis_calculate(:simplify, self, tolerance)
233
+ end
234
+
235
+
236
+ def simplify!(tolerance=0.1)
237
+ #FIXME: not good..
238
+ self.update_attribute(geo_columns.first, simplify)
239
+ end
240
+
241
+
242
+ def buffer(width=0.1)
243
+ postgis_calculate(:buffer,self,width)
244
+ end
245
+
246
+ #
247
+ # Returns a "simplified" version of the given geometry using the Douglas-Peuker
248
+ # algorithm. Will avoid creating derived geometries (polygons in particular) that
249
+ # are invalid. Will actually do something only with (multi)lines and (multi)polygons
250
+ # but you can safely call it with any kind of geometry. Since simplification occurs
251
+ # on a object-by-object basis you can also feed a GeometryCollection to this function.
252
+ #
253
+ # Performed by the GEOS module. Requires GEOS 3.0.0+
254
+ #
255
+ # Returns Geometry ST_SimplifyPreserveTopology(geometry geomA, float tolerance);0.005
256
+ #
257
+ def simplify_preserve_topology(tolerance=0.1)
258
+ postgis_calculate(:simplifypreservetopology, self, tolerance)
259
+ end
260
+
261
+ #
262
+ # True if Geometries "spatially intersect", share any portion of space.
263
+ # False if they don't (they are Disjoint).
264
+ #
265
+ # 'overlaps?', 'touches?', 'within?' all imply spatial intersection.
266
+ # If any of the aforementioned returns true, then the geometries also
267
+ # spatially intersect. 'disjoint?' implies false for spatial intersection.
268
+ #
269
+ # Returns Boolean ST_Intersects(geometry geomA, geometry geomB);
270
+ #
271
+ def intersects? other
272
+ postgis_calculate(:intersects, [self, other])
273
+ end
274
+
275
+ #
276
+ # True if a Geometry`s Envelope "spatially intersect", share any portion of space.
277
+ #
278
+ # It`s 'intersects?', for envelopes.
279
+ #
280
+ # Returns Boolean SE_EnvelopesIntersect(geometry geomA, geometry geomB);
281
+ #
282
+ def envelopes_intersect? other
283
+ postgis_calculate(:se_envelopesintersect, [self, other])
284
+ end
285
+
286
+ #
287
+ # Geometry that represents the point set intersection of the Geometries.
288
+ # In other words - that portion of geometry A and geometry B that is shared between
289
+ # the two geometries. If the geometries do not share any space (are disjoint),
290
+ # then an empty geometry collection is returned.
291
+ #
292
+ # 'intersection' in conjunction with intersects? is very useful for clipping
293
+ # geometries such as in bounding box, buffer, region queries where you only want
294
+ # to return that portion of a geometry that sits in a country or region of interest.
295
+ #
296
+ # Do not call with a GEOMETRYCOLLECTION as an argument.
297
+ # Performed by the GEOS module.
298
+ #
299
+ # Returns Geometry ST_Intersection(geometry geomA, geometry geomB);
300
+ #
301
+ def intersection other
302
+ postgis_calculate(:intersection, [self, other])
303
+ end
304
+
305
+ #
306
+ # True if the Geometries share space, are of the same dimension, but are
307
+ # not completely contained by each other. They intersect, but one does not
308
+ # completely contain another.
309
+ #
310
+ # Do not call with a GeometryCollection as an argument
311
+ # This function call will automatically include a bounding box comparison that
312
+ # will make use of any indexes that are available on the geometries. To avoid
313
+ # index use, use the function _ST_Overlaps.
314
+ #
315
+ # Performed by the GEOS module.
316
+ #
317
+ # Returns Boolean ST_Overlaps(geometry A, geometry B);
318
+ #
319
+ def overlaps? other
320
+ postgis_calculate(:overlaps, [self, other])
321
+ end
322
+
323
+ # True if the geometries have at least one point in common,
324
+ # but their interiors do not intersect.
325
+ #
326
+ # If the only points in common between g1 and g2 lie in the union of the
327
+ # boundaries of g1 and g2. The 'touches?' relation applies to all Area/Area,
328
+ # Line/Line, Line/Area, Point/Area and Point/Line pairs of relationships,
329
+ # but not to the Point/Point pair.
330
+ #
331
+ # Returns Boolean ST_Touches(geometry g1, geometry g2);
332
+ #
333
+ def touches? other
334
+ postgis_calculate(:touches, [self, other])
335
+ end
336
+
337
+ #
338
+ # The convex hull of a geometry represents the minimum closed geometry that
339
+ # encloses all geometries within the set.
340
+ #
341
+ # It is usually used with MULTI and Geometry Collections. Although it is not
342
+ # an aggregate - you can use it in conjunction with ST_Collect to get the convex
343
+ # hull of a set of points. ST_ConvexHull(ST_Collect(somepointfield)).
344
+ # It is often used to determine an affected area based on a set of point observations.
345
+ #
346
+ # Performed by the GEOS module.
347
+ #
348
+ # Returns Geometry ST_ConvexHull(geometry geomA);
349
+ #
350
+ def convex_hull
351
+ postgis_calculate(:convexhull, self)
352
+ end
353
+
354
+ #
355
+ # Creates an areal geometry formed by the constituent linework of given geometry.
356
+ # The return type can be a Polygon or MultiPolygon, depending on input.
357
+ # If the input lineworks do not form polygons NULL is returned. The inputs can
358
+ # be LINESTRINGS, MULTILINESTRINGS, POLYGONS, MULTIPOLYGONS, and GeometryCollections.
359
+ #
360
+ # Returns Boolean ST_BuildArea(geometry A);
361
+ #
362
+ def build_area
363
+ postgis_calculate(:buildarea, self)
364
+ end
365
+
366
+ #
367
+ # Returns true if this Geometry has no anomalous geometric points, such as
368
+ # self intersection or self tangency.
369
+ #
370
+ # Returns boolean ST_IsSimple(geometry geomA);
371
+ #
372
+ def is_simple?
373
+ postgis_calculate(:issimple, self)
374
+ end
375
+ alias_method "simple?", "is_simple?"
376
+
377
+ #
378
+ # Aggregate. Creates a GeometryCollection containing possible polygons formed
379
+ # from the constituent linework of a set of geometries.
380
+ #
381
+ # Geometry Collections are often difficult to deal with with third party tools,
382
+ # so use ST_Polygonize in conjunction with ST_Dump to dump the polygons out into
383
+ # individual polygons.
384
+ #
385
+ # Returns Geometry ST_Polygonize(geometry set geomfield);
386
+ #
387
+ def polygonize
388
+ postgis_calculate(:polygonize, self)
389
+ end
390
+
391
+ #
392
+ # Returns true if this Geometry is spatially related to anotherGeometry,
393
+ # by testing for intersections between the Interior, Boundary and Exterior
394
+ # of the two geometries as specified by the values in the
395
+ # intersectionPatternMatrix. If no intersectionPatternMatrix is passed in,
396
+ # then returns the maximum intersectionPatternMatrix that relates the 2 geometries.
397
+ #
398
+ #
399
+ # Version 1: Takes geomA, geomB, intersectionMatrix and Returns 1 (TRUE) if
400
+ # this Geometry is spatially related to anotherGeometry, by testing for
401
+ # intersections between the Interior, Boundary and Exterior of the two
402
+ # geometries as specified by the values in the intersectionPatternMatrix.
403
+ #
404
+ # This is especially useful for testing compound checks of intersection,
405
+ # crosses, etc in one step.
406
+ #
407
+ # Do not call with a GeometryCollection as an argument
408
+ #
409
+ # This is the "allowable" version that returns a boolean, not an integer.
410
+ # This is defined in OGC spec.
411
+ # This DOES NOT automagically include an index call. The reason for that
412
+ # is some relationships are anti e.g. Disjoint. If you are using a relationship
413
+ # pattern that requires intersection, then include the && index call.
414
+ #
415
+ # Version 2: Takes geomA and geomB and returns the DE-9IM
416
+ # (dimensionally extended nine-intersection matrix)
417
+ #
418
+ # Do not call with a GeometryCollection as an argument
419
+ # Not in OGC spec, but implied. see s2.1.13.2
420
+ #
421
+ # Both Performed by the GEOS module
422
+ #
423
+ # Returns:
424
+ #
425
+ # String ST_Relate(geometry geomA, geometry geomB);
426
+ # Boolean ST_Relate(geometry geomA, geometry geomB, text intersectionPatternMatrix);
427
+ #
428
+ def relate?(other, m = nil)
429
+ # Relate is case sentitive.......
430
+ m = "'#{m}'" if m
431
+ postgis_calculate("Relate", [self, other], m)
432
+ end
433
+
434
+ #
435
+ # Transform the geometry into a different spatial reference system.
436
+ # The destination SRID must exist in the SPATIAL_REF_SYS table.
437
+ #
438
+ # This method implements the OpenGIS Simple Features Implementation Specification for SQL.
439
+ # This method supports Circular Strings and Curves (PostGIS 1.3.4+)
440
+ #
441
+ # Requires PostGIS be compiled with Proj support.
442
+ #
443
+ # Return Geometry ST_Transform(geometry g1, integer srid);
444
+ #
445
+ def transform!(new_srid)
446
+ self[postgis_geoms.keys[0]] = postgis_calculate("Transform", self.new_record? ? self.geom : self, new_srid)
447
+ end
448
+
449
+ def transform(new_srid)
450
+ dup.transform!(new_srid)
451
+ end
452
+
453
+ #
454
+ # Returns a modified geometry having no segment longer than the given distance.
455
+ # Distance computation is performed in 2d only.
456
+ #
457
+ # This will only increase segments. It will not lengthen segments shorter than max length
458
+ #
459
+ # Return Geometry ST_Segmentize(geometry geomA, float max_length);
460
+ #
461
+ def segmentize(max_length=1.0)
462
+ postgis_calculate("segmentize", self, max_length)
463
+ end
464
+
465
+ #
466
+ # Returns the instance`s geom srid
467
+ #
468
+ def srid
469
+ self[postgis_geoms.keys.first].srid
470
+ end
471
+
472
+ #
473
+ # Return UTM Zone for a geom
474
+ #
475
+ # Return Integer
476
+ def utm_zone
477
+ if srid == 4326
478
+ geom = centroid
479
+ else
480
+ geomdup = transform(4326)
481
+ mezzo = geomdup.length / 2
482
+ geom = case geomdup
483
+ when Point then geomdup
484
+ when LineString then geomdup[mezzo]
485
+ else
486
+ geomgeog[mezzo][geomgeo[mezzo]/2]
487
+ end
488
+
489
+ end
490
+
491
+ pref = geom.y > 0 ? 32700 : 32600
492
+ zone = ((geom.x + 180) / 6 + 1).to_i
493
+ zone + pref
494
+ end
495
+
496
+ #
497
+ # Returns the Geometry in its UTM Zone
498
+ #
499
+ # Return Geometry
500
+ def to_utm!(utm=nil)
501
+ utm ||= utm_zone
502
+ self[postgis_geoms.keys.first] = transform(utm)
503
+ end
504
+
505
+ def to_utm
506
+ dup.to_utm!
507
+ end
508
+
509
+ #
510
+ # Returns Geometry as GeoJSON
511
+ #
512
+ # http://geojson.org/
513
+ #
514
+ def as_geo_json(precision=15, bbox = 0)
515
+ postgis_calculate(:AsGeoJSON, self, [precision, bbox])
516
+ end
517
+
518
+ #ST_PointOnSurface — Returns a POINT guaranteed to lie on the surface.
519
+ #geometry ST_PointOnSurface(geometry g1);eometry A, geometry B);
520
+ def point_on_surface
521
+ postgis_calculate(:pointonsurface, self)
522
+ end
523
+
524
+
525
+ #
526
+ #
527
+ # LINESTRING
528
+ #
529
+ #
530
+ #
531
+ module LineStringFunctions
532
+
533
+ #
534
+ # Returns the 2D length of the geometry if it is a linestring, multilinestring,
535
+ # ST_Curve, ST_MultiCurve. 0 is returned for areal geometries. For areal geometries
536
+ # use 'perimeter'. Measurements are in the units of the spatial reference system
537
+ # of the geometry.
538
+ #
539
+ # Returns Float
540
+ #
541
+ def length
542
+ postgis_calculate(:length, self).to_f
543
+ end
544
+
545
+ #
546
+ # Returns the 3-dimensional or 2-dimensional length of the geometry if it is
547
+ # a linestring or multi-linestring. For 2-d lines it will just return the 2-d
548
+ # length (same as 'length')
549
+ #
550
+ # Returns Float
551
+ #
552
+ def length_3d
553
+ postgis_calculate(:length3d, self).to_f
554
+ end
555
+
556
+ #
557
+ # Calculates the length of a geometry on an ellipsoid. This is useful if the
558
+ # coordinates of the geometry are in longitude/latitude and a length is
559
+ # desired without reprojection. The ellipsoid is a separate database type and
560
+ # can be constructed as follows:
561
+ #
562
+ # SPHEROID[<NAME>,<SEMI-MAJOR AXIS>,<INVERSE FLATTENING>]
563
+ #
564
+ # Example:
565
+ # SPHEROID["GRS_1980",6378137,298.257222101]
566
+ #
567
+ # Defaults to:
568
+ #
569
+ # SPHEROID["IERS_2003",6378136.6,298.25642]
570
+ #
571
+ # Returns Float length_spheroid(geometry linestring, spheroid);
572
+ #
573
+ def length_spheroid(spheroid = EARTH_SPHEROID)
574
+ postgis_calculate(:length_spheroid, self, spheroid).to_f
575
+ end
576
+
577
+ #
578
+ # Return the number of points of the geometry.
579
+ # PostGis ST_NumPoints does not work as nov/08
580
+ #
581
+ # Returns Integer ST_NPoints(geometry g1);
582
+ #
583
+ def num_points
584
+ postgis_calculate(:npoints, self).to_i
585
+ end
586
+
587
+ #
588
+ # Returns geometry start point.
589
+ #
590
+ def start_point
591
+ postgis_calculate(:startpoint, self)
592
+ end
593
+
594
+ #
595
+ # Returns geometry end point.
596
+ #
597
+ def end_point
598
+ postgis_calculate(:endpoint, self)
599
+ end
600
+
601
+ #
602
+ # Takes two geometry objects and returns TRUE if their intersection
603
+ # "spatially cross", that is, the geometries have some, but not all interior
604
+ # points in common. The intersection of the interiors of the geometries must
605
+ # not be the empty set and must have a dimensionality less than the the
606
+ # maximum dimension of the two input geometries. Additionally, the
607
+ # intersection of the two geometries must not equal either of the source
608
+ # geometries. Otherwise, it returns FALSE.
609
+ #
610
+ #
611
+ # Returns Boolean ST_Crosses(geometry g1, geometry g2);
612
+ #
613
+ def crosses? other
614
+ postgis_calculate(:crosses, [self, other])
615
+ end
616
+
617
+ #
618
+ # Warning: PostGIS 1.4+
619
+ #
620
+ # Return crossing direction
621
+ def line_crossing_direction(other)
622
+ postgis_calculate(:lineCrossingDirection, [self, other])
623
+ end
624
+
625
+ #
626
+ # Returns a float between 0 and 1 representing the location of the closest point
627
+ # on LineString to the given Point, as a fraction of total 2d line length.
628
+ #
629
+ # You can use the returned location to extract a Point (ST_Line_Interpolate_Point)
630
+ # or a substring (ST_Line_Substring).
631
+ #
632
+ # This is useful for approximating numbers of addresses.
633
+ #
634
+ # Returns float (0 to 1) ST_Line_Locate_Point(geometry a_linestring, geometry a_point);
635
+ #
636
+ def locate_point point
637
+ postgis_calculate(:line_locate_point, [self, point]).to_f
638
+ end
639
+
640
+ #
641
+ # Return a derived geometry collection value with elements that match the
642
+ # specified measure. Polygonal elements are not supported.
643
+ #
644
+ # Semantic is specified by: ISO/IEC CD 13249-3:200x(E) - Text for
645
+ # Continuation CD Editing Meeting
646
+ #
647
+ # Returns geometry ST_Locate_Along_Measure(geometry ageom_with_measure, float a_measure);
648
+ #
649
+ def locate_along_measure(measure)
650
+ postgis_calculate(:locate_along_measure, self, measure)
651
+ end
652
+
653
+ #
654
+ # Return a derived geometry collection value with elements that match the
655
+ # specified range of measures inclusively. Polygonal elements are not supported.
656
+ #
657
+ # Semantic is specified by: ISO/IEC CD 13249-3:200x(E) - Text for Continuation CD Editing Meeting
658
+ #
659
+ # Returns geometry ST_Locate_Between_Measures(geometry geomA, float measure_start, float measure_end);
660
+ #
661
+ def locate_between_measures(a, b)
662
+ postgis_calculate(:locate_between_measures, self, [a,b])
663
+ end
664
+
665
+ #
666
+ # Returns a point interpolated along a line. First argument must be a LINESTRING.
667
+ # Second argument is a float8 between 0 and 1 representing fraction of total
668
+ # linestring length the point has to be located.
669
+ #
670
+ # See ST_Line_Locate_Point for computing the line location nearest to a Point.
671
+ #
672
+ # Returns geometry ST_Line_Interpolate_Point(geometry a_linestring, float a_fraction);
673
+ #
674
+ def interpolate_point(fraction)
675
+ postgis_calculate(:line_interpolate_point, self, fraction)
676
+ end
677
+
678
+ #
679
+ # Return a linestring being a substring of the input one starting and ending
680
+ # at the given fractions of total 2d length. Second and third arguments are
681
+ # float8 values between 0 and 1. This only works with LINESTRINGs. To use
682
+ # with contiguous MULTILINESTRINGs use in conjunction with ST_LineMerge.
683
+ #
684
+ # If 'start' and 'end' have the same value this is equivalent to 'interpolate_point'.
685
+ #
686
+ # See 'locate_point' for computing the line location nearest to a Point.
687
+ #
688
+ # Returns geometry ST_Line_Substring(geometry a_linestring, float startfraction, float endfraction);
689
+ #
690
+ def line_substring(s,e)
691
+ postgis_calculate(:line_substring, self, [s, e])
692
+ end
693
+
694
+ ###
695
+ #Not implemented in postgis yet
696
+ # ST_max_distance Returns the largest distance between two line strings.
697
+ #def max_distance other
698
+ # #float ST_Max_Distance(geometry g1, geometry g2);
699
+ # postgis_calculate(:max_distance, [self, other])
700
+ #end
701
+ end
702
+
703
+
704
+ #
705
+ #
706
+ #
707
+ #
708
+ # POINT
709
+ #
710
+ #
711
+ #
712
+ #
713
+ module PointFunctions
714
+
715
+ #
716
+ # Returns a float between 0 and 1 representing the location of the closest point
717
+ # on LineString to the given Point, as a fraction of total 2d line length.
718
+ #
719
+ # You can use the returned location to extract a Point (ST_Line_Interpolate_Point)
720
+ # or a substring (ST_Line_Substring).
721
+ #
722
+ # This is useful for approximating numbers of addresses.
723
+ #
724
+ # Returns float (0 to 1) ST_Line_Locate_Point(geometry a_linestring, geometry a_point);
725
+ #
726
+ def where_on_line line
727
+ postgis_calculate(:line_locate_point, [line, self]).to_f
728
+ end
729
+
730
+ #
731
+ # Linear distance in meters between two lon/lat points.
732
+ # Uses a spherical earth and radius of 6370986 meters.
733
+ # Faster than 'distance_spheroid', but less accurate.
734
+ #
735
+ # Only implemented for points.
736
+ #
737
+ # Returns Float ST_Distance_Sphere(geometry pointlonlatA, geometry pointlonlatB);
738
+ #
739
+ def distance_sphere_to(other)
740
+ postgis_calculate(:distance_sphere, [self, other]).to_f
741
+ end
742
+
743
+ #
744
+ # Calculates the distance on an ellipsoid. This is useful if the
745
+ # coordinates of the geometry are in longitude/latitude and a length is
746
+ # desired without reprojection. The ellipsoid is a separate database type and
747
+ # can be constructed as follows:
748
+ #
749
+ # This is slower then 'distance_sphere_to', but more precise.
750
+ #
751
+ # SPHEROID[<NAME>,<SEMI-MAJOR AXIS>,<INVERSE FLATTENING>]
752
+ #
753
+ # Example:
754
+ # SPHEROID["GRS_1980",6378137,298.257222101]
755
+ #
756
+ # Defaults to:
757
+ #
758
+ # SPHEROID["IERS_2003",6378136.6,298.25642]
759
+ #
760
+ # Returns ST_Distance_Spheroid(geometry geomA, geometry geomB, spheroid);
761
+ #
762
+ def distance_spheroid_to(other, spheroid = EARTH_SPHEROID)
763
+ postgis_calculate(:distance_spheroid, [self, other], spheroid).to_f
764
+ end
765
+
766
+ #
767
+ # The azimuth of the segment defined by the given Point geometries,
768
+ # or NULL if the two points are coincident. Return value is in radians.
769
+ #
770
+ # The Azimuth is mathematical concept defined as the angle, in this case
771
+ # measured in radian, between a reference plane and a point.
772
+ #
773
+ # Returns Float ST_Azimuth(geometry pointA, geometry pointB);
774
+ #
775
+ def azimuth other
776
+ #TODO: return if not point/point
777
+ postgis_calculate(:azimuth, [self, other]).to_f
778
+ rescue
779
+ ActiveRecord::StatementInvalid
780
+ end
781
+
782
+ #
783
+ # True if the geometry is a point and is inside the circle.
784
+ #
785
+ # Returns Boolean ST_point_inside_circle(geometry, float, float, float)
786
+ #
787
+ def inside_circle?(x,y,r)
788
+ postgis_calculate(:point_inside_circle, self, [x,y,r])
789
+ end
790
+
791
+ end
792
+
793
+ #
794
+ #
795
+ #
796
+ #
797
+ # Polygon
798
+ #
799
+ #
800
+ #
801
+ #
802
+ module PolygonFunctions
803
+
804
+ #
805
+ # The area of the geometry if it is a polygon or multi-polygon.
806
+ # Return the area measurement of an ST_Surface or ST_MultiSurface value.
807
+ # Area is in the units of the spatial reference system.
808
+ #
809
+ # Accepts optional parameter, the srid to transform to.
810
+ #
811
+ # Returns Float ST_Area(geometry g1);
812
+ #
813
+ def area transform=nil
814
+ postgis_calculate(:area, self, { :transform => transform }).to_f
815
+ end
816
+
817
+ #
818
+ # Returns the 2D perimeter of the geometry if it is a ST_Surface, ST_MultiSurface
819
+ # (Polygon, Multipolygon). 0 is returned for non-areal geometries. For linestrings
820
+ # use 'length'. Measurements are in the units of the spatial reference system of
821
+ # the geometry.
822
+ #
823
+ # Accepts optional parameter, the sridto transform to.
824
+ #
825
+ # Returns Float ST_Perimeter(geometry g1);
826
+ #
827
+ def perimeter transform=nil
828
+ postgis_calculate(:perimeter, self, { :transform => transform }).to_f
829
+ end
830
+
831
+ #
832
+ # Returns the 3-dimensional perimeter of the geometry, if it is a polygon or multi-polygon.
833
+ # If the geometry is 2-dimensional, then the 2-dimensional perimeter is returned.
834
+ #
835
+ # Returns Float ST_Perimeter3D(geometry geomA);
836
+ #
837
+ def perimeter3d
838
+ postgis_calculate(:perimeter3d, self).to_f
839
+ end
840
+
841
+ #
842
+ # True if the LineString's start and end points are coincident.
843
+ #
844
+ # This method implements the OpenGIS Simple Features Implementation
845
+ # Specification for SQL.
846
+ #
847
+ # SQL-MM defines the result of ST_IsClosed(NULL) to be 0, while PostGIS returns NULL.
848
+ #
849
+ # Returns boolean ST_IsClosed(geometry g);
850
+ #
851
+ def closed?
852
+ postgis_calculate(:isclosed, self)
853
+ end
854
+ alias_method "is_closed?", "closed?"
855
+
856
+ #
857
+ # True if no point in Geometry B is outside Geometry A
858
+ #
859
+ # This function call will automatically include a bounding box comparison
860
+ # that will make use of any indexes that are available on the geometries.
861
+ # To avoid index use, use the function _ST_Covers.
862
+ #
863
+ # Do not call with a GEOMETRYCOLLECTION as an argument
864
+ # Do not use this function with invalid geometries. You will get unexpected results.
865
+ #
866
+ # Performed by the GEOS module.
867
+ #
868
+ # Returns Boolean ST_Covers(geometry geomA, geometry geomB);
869
+ #
870
+ def covers? other
871
+ postgis_calculate(:covers, [self, other])
872
+ end
873
+
874
+ end
875
+
876
+ end
877
+
878
+ # NEW
879
+ #ST_OrderingEquals — Returns true if the given geometries represent the same geometry and points are in the same directional order.
880
+ #boolean ST_OrderingEquals(g
881
+ # ST_PointOnSurface — Returns a POINT guaranteed to lie on the surface.
882
+ #geometry ST_PointOnSurface(geometry g1);eometry A, geometry B);
883
+
884
+
885
+ #x ST_SnapToGrid(geometry, geometry, sizeX, sizeY, sizeZ, sizeM)
886
+ # ST_X , ST_Y, SE_M, SE_Z, SE_IsMeasured has_m?