Example #1
0
def polygon_ewkt_from_coords(coords):
    '''
    Convert a string list of coordinates to SRS 4326 POLYGON EWKT.

    For more information about EWKT, see:
    http://en.wikipedia.org/wiki/Well-known_text

    NOTE: Input coordinates are expected in the order (lat, lon). The ordering
    for SRS 4326 is (lon, lat).

    NOTE 2: All coordinate values will be rounded using
    :func:`openquake.engine.utils.round_float`

    >>> polygon_ewkt_from_coords(
    ...     "38.113, -122.0, 38.113, -122.114, 38.111, -122.57")
    'SRID=4326;POLYGON((-122.0 38.113, -122.114 38.113, -122.57 38.111, \
-122.0 38.113))'
    '''
    coord_list = [round_float(x) for x in coords.split(",")]
    vertices = ['%s %s' % (coord_list[i + 1], coord_list[i]) for i in
                xrange(0, len(coord_list), 2)]

    ewkt = 'SRID=4326;POLYGON((%s, %s))'
    # The polygon needs to form a closed loop, so the first & last coord must
    # be the same:
    ewkt %= (', '.join(vertices), vertices[0])

    return ewkt
Example #2
0
    def from_coordinates(cls, coordinates):
        """
        Build a region from a list of polygon coordinates.

        :param coordinates:
            List of 2-tuples (lon, lat). Example ::

                [(-118.25, 34.07), (-118.22, 34.07), (-118.22, 34.04), \
(-118.25, 34.04)]

        :returns:
            :class:`openquake.engine.shapes.Region` instance
        """

        # Constrain the precision for the coordinates:
        coordinates = [(round_float(pt[0]), round_float(pt[1]))
                for pt in coordinates]

        return cls(geometry.Polygon(coordinates))
Example #3
0
    def _build_polygon(self):
        """
        Create the polygon underlying this grid.
        """

        # since we are always considering the center of the
        # cells, we must include half of the cell size
        # to the borders
        half_cell_size = self.cell_size / 2.0

        min_lon = self.llc.longitude - half_cell_size
        max_lon = (self.llc.longitude + (self.columns * self.cell_size)
                + half_cell_size)

        min_lat = self.llc.latitude - half_cell_size
        max_lat = (self.llc.latitude + (self.rows * self.cell_size)
                + half_cell_size)

        coords = [(min_lon, max_lat), (max_lon, max_lat),
                  (max_lon, min_lat), (min_lon, min_lat)]

        return geometry.Polygon([(round_float(pt[0]),
                round_float(pt[1])) for pt in coords])
Example #4
0
    def test_round_float_rounding(self):
        """
        By default, the :py:module:`decimal` module uses the 'round-half-even'
        algorithm for rounding numbers.

        Since the rounding method can be set in a global context for the
        :py:module:`decimal` module, we want to make sure the
        :py:function:`openquake.engine.utils.round_float` is unaffected context
        changes.
        """
        decimal.getcontext().rounding = decimal.ROUND_FLOOR

        # changing the decimal context rounding should not affect the behavior
        # of round_float
        self.assertEqual(-121.0000001, round_float(-121.00000009))

        # reset the global context so we don't potentially screw up other tests
        decimal.getcontext().rounding = decimal.ROUND_HALF_EVEN
Example #5
0
    def test_round_float(self):
        """
        This test exercises the
        :py:function:`openquake.engine.utils.round_float` function.

        Basically, the function should take any float number and round it to a
        fixed number of decimal places (for example, 7 places). The rounding
        method used is the default for the :py:module:`decimal` module, which
        is ROUND_HALF_EVEN.

        For more information on 'half-even' rounding, there is a good
        explanation here:
        http://www.diycalculator.com/popup-m-round.shtml#A5
        """
        in_values = (
            29.000000000000004, -121.00000009, -121.00000001, 121.00000005,
            121.00000006)
        out_values = (29.0, -121.0000001, -121.0, 121.0, 121.0000001)

        for i, _ in enumerate(in_values):
            self.assertEqual(out_values[i], round_float(in_values[i]))
Example #6
0
def multipoint_ewkt_from_coords(coords):
    '''
    Convert a string list of coordinates to SRS 4326 MULTIPOINT EWKT.

    For more information about EWKT, see:
    http://en.wikipedia.org/wiki/Well-known_text

    NOTE: Input coordinates are expected in the order (lat, lon). The ordering
    for SRS 4326 is (lon, lat).

    NOTE 2: All coordinate values will be rounded using
    :func:`openquake.engine.utils.round_float`

    >>> multipoint_ewkt_from_coords("38.113, -122.0, 38.113, -122.114")
    'SRID=4326;MULTIPOINT((-122.0 38.113), (-122.114 38.113))'
    '''
    coord_list = [round_float(x) for x in coords.split(",")]
    points = ['(%s %s)' % (coord_list[i + 1], coord_list[i]) for i in
              xrange(0, len(coord_list), 2)]

    ewkt = 'SRID=4326;MULTIPOINT(%s)'
    ewkt %= ', '.join(points)

    return ewkt
Example #7
0
    def __init__(self, longitude, latitude, depth=0.0):
        hazardlib_geo.Point.__init__(
            self, round_float(longitude), round_float(latitude), depth=depth)

        self.point = geometry.Point(self.longitude, self.latitude)