lib/postgis_functions/common.rb in postgis_adapter-0.1.8 vs lib/postgis_functions/common.rb in postgis_adapter-0.2.1

- old
+ new

@@ -232,11 +232,11 @@ end def simplify!(tolerance=0.1) #FIXME: not good.. - self.geom = simplify + self.update_attribute(get_column_name, simplify) end # # Returns a "simplified" version of the given geometry using the Douglas-Peuker # algorithm. Will avoid creating derived geometries (polygons in particular) that @@ -422,17 +422,368 @@ def relate?(other, m = nil) # Relate is case sentitive....... m = "'#{m}'" if m postgis_calculate("Relate", [self, other], m) end + + # + # Transform the geometry into a different spatial reference system. + # + # Return geometry ST_Transform(geometry g1, integer srid); + # Returns a new geometry with its coordinates transformed to spatial reference system referenced by the SRID integer parameter. The destination SRID must exist in the SPATIAL_REF_SYS table. + # ST_Transform is often confused with ST_SetSRID(). ST_Transform actually changes the coordinates of a geometry from one spatial reference system to another, while ST_SetSRID() simply changes the SRID identifier of the geometry + # Requires PostGIS be compiled with Proj support. Use PostGIS_Full_Version to confirm you have proj support compiled in. + # + # If using more than one transformation, it is useful to have a functional index on the commonly used transformations to take advantage of index usage. + # + # Prior to 1.3.4, this function crashes if used with geometries that contain CURVES. This is fixed in 1.3.4+ + # This method implements the OpenGIS Simple Features Implementation Specification for SQL. + # This method supports Circular Strings and Curves + # + def transform(new_srid) + postgis_calculate("Transform", self, new_srid) + end + # + # LINESTRING + # + # + # + module LineStringFunctions + + # + # Returns the 2D length of the geometry if it is a linestring, multilinestring, + # ST_Curve, ST_MultiCurve. 0 is returned for areal geometries. For areal geometries + # use 'perimeter'. Measurements are in the units of the spatial reference system + # of the geometry. + # + # Returns Float + # + def length + dis = postgis_calculate(:length, self).to_f + end + + # + # Returns the 3-dimensional or 2-dimensional length of the geometry if it is + # a linestring or multi-linestring. For 2-d lines it will just return the 2-d + # length (same as 'length') + # + # Returns Float + # + def length_3d + dis = postgis_calculate(:length3d, self).to_f + end + + # + # Calculates the length of a geometry on an ellipsoid. This is useful if the + # coordinates of the geometry are in longitude/latitude and a length is + # desired without reprojection. The ellipsoid is a separate database type and + # can be constructed as follows: + # + # SPHEROID[<NAME>,<SEMI-MAJOR AXIS>,<INVERSE FLATTENING>] + # + # Example: + # SPHEROID["GRS_1980",6378137,298.257222101] + # + # Defaults to: + # + # SPHEROID["IERS_2003",6378136.6,298.25642] + # + # Returns Float length_spheroid(geometry linestring, spheroid); + # + def length_spheroid(spheroid = EARTH_SPHEROID) + dis = postgis_calculate(:length_spheroid, self, spheroid).to_f + end + + # + # Return the number of points of the geometry. + # PostGis ST_NumPoints does not work as nov/08 + # + # Returns Integer ST_NPoints(geometry g1); + # + def num_points + postgis_calculate(:npoints, self).to_i + end + + # + # Returns geometry start point. + # + def start_point + postgis_calculate(:startpoint, self) + end + + # + # Returns geometry end point. + # + def end_point + postgis_calculate(:endpoint, self) + end + + # + # Takes two geometry objects and returns TRUE if their intersection + # "spatially cross", that is, the geometries have some, but not all interior + # points in common. The intersection of the interiors of the geometries must + # not be the empty set and must have a dimensionality less than the the + # maximum dimension of the two input geometries. Additionally, the + # intersection of the two geometries must not equal either of the source + # geometries. Otherwise, it returns FALSE. + # + # + # Returns Boolean ST_Crosses(geometry g1, geometry g2); + # + def crosses? other + postgis_calculate(:crosses, [self, other]) + end + + # + # Returns a float between 0 and 1 representing the location of the closest point + # on LineString to the given Point, as a fraction of total 2d line length. + # + # You can use the returned location to extract a Point (ST_Line_Interpolate_Point) + # or a substring (ST_Line_Substring). + # + # This is useful for approximating numbers of addresses. + # + # Returns float (0 to 1) ST_Line_Locate_Point(geometry a_linestring, geometry a_point); + # + def locate_point point + postgis_calculate(:line_locate_point, [self, point]).to_f + end + + # + # Return a derived geometry collection value with elements that match the + # specified measure. Polygonal elements are not supported. + # + # Semantic is specified by: ISO/IEC CD 13249-3:200x(E) - Text for + # Continuation CD Editing Meeting + # + # Returns geometry ST_Locate_Along_Measure(geometry ageom_with_measure, float a_measure); + # + def locate_along_measure(measure) + postgis_calculate(:locate_along_measure, self, measure) + end + + # + # Return a derived geometry collection value with elements that match the + # specified range of measures inclusively. Polygonal elements are not supported. + # + # Semantic is specified by: ISO/IEC CD 13249-3:200x(E) - Text for Continuation CD Editing Meeting + # + # Returns geometry ST_Locate_Between_Measures(geometry geomA, float measure_start, float measure_end); + # + def locate_between_measures(a, b) + postgis_calculate(:locate_between_measures, self, [a,b]) + end + + # + # Returns a point interpolated along a line. First argument must be a LINESTRING. + # Second argument is a float8 between 0 and 1 representing fraction of total + # linestring length the point has to be located. + # + # See ST_Line_Locate_Point for computing the line location nearest to a Point. + # + # Returns geometry ST_Line_Interpolate_Point(geometry a_linestring, float a_fraction); + # + def interpolate_point(fraction) + postgis_calculate(:line_interpolate_point, self, fraction) + end + + # + # Return a linestring being a substring of the input one starting and ending + # at the given fractions of total 2d length. Second and third arguments are + # float8 values between 0 and 1. This only works with LINESTRINGs. To use + # with contiguous MULTILINESTRINGs use in conjunction with ST_LineMerge. + # + # If 'start' and 'end' have the same value this is equivalent to 'interpolate_point'. + # + # See 'locate_point' for computing the line location nearest to a Point. + # + # Returns geometry ST_Line_Substring(geometry a_linestring, float startfraction, float endfraction); + # + def line_substring(s,e) + postgis_calculate(:line_substring, self, [s, e]) + end + + ### + #Not implemented in postgis yet + # ST_max_distance Returns the largest distance between two line strings. + #def max_distance other + # #float ST_Max_Distance(geometry g1, geometry g2); + # postgis_calculate(:max_distance, [self, other]) + #end + end + + + #### + ### + ## + # + # POINT + # + # + module PointFunctions + + # + # Returns a float between 0 and 1 representing the location of the closest point + # on LineString to the given Point, as a fraction of total 2d line length. + # + # You can use the returned location to extract a Point (ST_Line_Interpolate_Point) + # or a substring (ST_Line_Substring). + # + # This is useful for approximating numbers of addresses. + # + # Returns float (0 to 1) ST_Line_Locate_Point(geometry a_linestring, geometry a_point); + # + def where_on_line line + postgis_calculate(:line_locate_point, [line, self]).to_f + end + + # + # Linear distance in meters between two lon/lat points. + # Uses a spherical earth and radius of 6370986 meters. + # Faster than 'distance_spheroid', but less accurate. + # + # Only implemented for points. + # + # Returns Float ST_Distance_Sphere(geometry pointlonlatA, geometry pointlonlatB); + # + def distance_sphere_to(other) + dis = postgis_calculate(:distance_sphere, [self, other]).to_f + end + + # + # Calculates the distance on an ellipsoid. This is useful if the + # coordinates of the geometry are in longitude/latitude and a length is + # desired without reprojection. The ellipsoid is a separate database type and + # can be constructed as follows: + # + # This is slower then 'distance_sphere_to', but more precise. + # + # SPHEROID[<NAME>,<SEMI-MAJOR AXIS>,<INVERSE FLATTENING>] + # + # Example: + # SPHEROID["GRS_1980",6378137,298.257222101] + # + # Defaults to: + # + # SPHEROID["IERS_2003",6378136.6,298.25642] + # + # Returns ST_Distance_Spheroid(geometry geomA, geometry geomB, spheroid); + # + def distance_spheroid_to(other, spheroid = EARTH_SPHEROID) + postgis_calculate(:distance_spheroid, [self, other], spheroid).to_f + end + + # + # The azimuth of the segment defined by the given Point geometries, + # or NULL if the two points are coincident. Return value is in radians. + # + # The Azimuth is mathematical concept defined as the angle, in this case + # measured in radian, between a reference plane and a point. + # + # Returns Float ST_Azimuth(geometry pointA, geometry pointB); + # + def azimuth other + #TODO: return if not point/point + postgis_calculate(:azimuth, [self, other]).to_f + rescue + ActiveRecord::StatementInvalid + end + + # + # True if the geometry is a point and is inside the circle. + # + # Returns Boolean ST_point_inside_circle(geometry, float, float, float) + # + def inside_circle?(x,y,r) + postgis_calculate(:point_inside_circle, self, [x,y,r]) + end + + end + + ### + ## + # + # Polygon + # + # + module PolygonFunctions + + # + # The area of the geometry if it is a polygon or multi-polygon. + # Return the area measurement of an ST_Surface or ST_MultiSurface value. + # Area is in the units of the spatial reference system. + # + # Returns Float ST_Area(geometry g1); + # + def area + postgis_calculate(:area, self).to_f + end + + # + # Returns the 2D perimeter of the geometry if it is a ST_Surface, ST_MultiSurface + # (Polygon, Multipolygon). 0 is returned for non-areal geometries. For linestrings + # use 'length'. Measurements are in the units of the spatial reference system of + # the geometry. + # + # Returns Float ST_Perimeter(geometry g1); + # + def perimeter + postgis_calculate(:perimeter, self).to_f + end + + # + # Returns the 3-dimensional perimeter of the geometry, if it is a polygon or multi-polygon. + # If the geometry is 2-dimensional, then the 2-dimensional perimeter is returned. + # + # Returns Float ST_Perimeter3D(geometry geomA); + # + def perimeter3d + postgis_calculate(:perimeter3d, self).to_f + end + + # + # True if the LineString's start and end points are coincident. + # + # This method implements the OpenGIS Simple Features Implementation + # Specification for SQL. + # + # SQL-MM defines the result of ST_IsClosed(NULL) to be 0, while PostGIS returns NULL. + # + # Returns boolean ST_IsClosed(geometry g); + # + def closed? + postgis_calculate(:isclosed, self) + end + alias_method "is_closed?", "closed?" + + # + # True if no point in Geometry B is outside Geometry A + # + # This function call will automatically include a bounding box comparison + # that will make use of any indexes that are available on the geometries. + # To avoid index use, use the function _ST_Covers. + # + # Do not call with a GEOMETRYCOLLECTION as an argument + # Do not use this function with invalid geometries. You will get unexpected results. + # + # Performed by the GEOS module. + # + # Returns Boolean ST_Covers(geometry geomA, geometry geomB); + # + def covers? other + postgis_calculate(:covers, [self, other]) + end + + end + end # NEW #ST_OrderingEquals — Returns true if the given geometries represent the same geometry and points are in the same directional order. #boolean ST_OrderingEquals(g # ST_PointOnSurface — Returns a POINT guaranteed to lie on the surface. #geometry ST_PointOnSurface(geometry g1);eometry A, geometry B); #x ST_SnapToGrid(geometry, geometry, sizeX, sizeY, sizeZ, sizeM) -# ST_X , ST_Y, SE_M, SE_Z, SE_IsMeasured has_m? \ No newline at end of file +# ST_X , ST_Y, SE_M, SE_Z, SE_IsMeasured has_m?