<commit_before># -*- encoding: utf-8 -*-

__author__ = 'kotaimen'
__date__ = '6/3/14'

"""
    georest.geo.metadata
    ~~~~~~~~~~~~~~~~~~~~
    Metadata of a feature
"""

import collections
import time

import geohash
import shapely.geometry.base

from .spatialref import SpatialReference


class Metadata(collections.namedtuple('Metadata',
                                      '''created modified bbox geohash
                                      cells''')):
    """ Metadata of a geometry

    - `created` `modified` float timestamp, time since epoch returned
       by `time.time()`
    - `geohash` geohash of the geometry used to do quick adhoc spatial search.
      For point geometry, max precision is 12 chars, for other geometry, defined
      by its bounding box.  Note geohash only works on geometry with lonlat
      based coordinate reference systems.
    - `bbox` bounding box of the geometry in (minx, miny, maxx, maxy)
    - `cells` a list of integers describes S2CellID of the geometry, currently
      not implemented.
    """

    GEOHASH_PRECISION = 12

    @staticmethod
    def make_metadata(created=None, modified=None,
                      geometry=None):

        if created is None:
            created = time.time()

        if modified is None:
            modified = time.time()

        bbox = calc_bbox(geometry)

        geohash = calc_geohash(geometry, Metadata.GEOHASH_PRECISION)

        return Metadata(created, modified, bbox, geohash, [])

    def respawn(self, geometry=None):
        modified = time.time()
        bbox = calc_bbox(geometry)

        geohash = calc_geohash(geometry, Metadata.GEOHASH_PRECISION)

        return self._replace(modified=modified,
                             bbox=bbox,
                             geohash=geohash)


def calc_bbox(geom):
    """Calculate bounding box of the geometry"""
    assert isinstance(geom, shapely.geometry.base.BaseGeometry)
    return geom.bounds


def calc_geohash(geom, precision=7, ignore_crs=False):
    """Calculate geohash of th geometry, mimics behaviour of postgis st_geohash

    `geom` must be a geometry with lonlat coordinates and `precision` is<commit_msg>Bbox returns a list instead of a tuple<commit_after># -*- encoding: utf-8 -*-

__author__ = 'kotaimen'
__date__ = '6/3/14'

"""
    georest.geo.metadata
    ~~~~~~~~~~~~~~~~~~~~
    Metadata of a feature
"""

import collections
import time

import geohash
import shapely.geometry.base

from .spatialref import SpatialReference


class Metadata(collections.namedtuple('Metadata',
                                      '''created modified bbox geohash
                                      cells''')):
    """ Metadata of a geometry

    - `created` `modified` float timestamp, time since epoch returned
       by `time.time()`
    - `geohash` geohash of the geometry used to do quick adhoc spatial search.
      For point geometry, max precision is 12 chars, for other geometry, defined
      by its bounding box.  Note geohash only works on geometry with lonlat
      based coordinate reference systems.
    - `bbox` bounding box of the geometry as a list [minx, miny, maxx, maxy]
    - `cells` a list of integers describes S2CellID of the geometry, currently
      not implemented.
    """

    GEOHASH_PRECISION = 12

    @staticmethod
    def make_metadata(created=None, modified=None,
                      geometry=None):

        if created is None:
            created = time.time()

        if modified is None:
            modified = time.time()

        bbox = calc_bbox(geometry)

        geohash = calc_geohash(geometry, Metadata.GEOHASH_PRECISION)

        return Metadata(created, modified, bbox, geohash, [])

    def respawn(self, geometry=None):
        modified = time.time()
        bbox = calc_bbox(geometry)

        geohash = calc_geohash(geometry, Metadata.GEOHASH_PRECISION)

        return self._replace(modified=modified,
                             bbox=bbox,
                             geohash=geohash)


def calc_bbox(geom):
    """Calculate bounding box of the geometry"""
    assert isinstance(geom, shapely.geometry.base.BaseGeometry)
    return list(geom.bounds)


def calc_geohash(geom, precision=7, ignore_crs=False):
    """Calculate geohash of th geometry, mimics behaviour of postgis st_geohash

    `geom` must be a geometry with lonlat coordinates and `precision` is
{'fraction_non_alphanumeric': 0.09041095890410959, 'fraction_numerical': 0.00502283105022831, 'mean_word_length': 2.493620414673046, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1323038', 'n_tokens_mistral': 1308, 'n_tokens_neox': 1214, 'n_words': 388}