def search_by_ip(self, ipaddr, radius=None, query=None, category=None):
        """
        Search for places near an IP address, within a radius (in
        kilometers).

        The server uses guesses the latitude and longitude from the
        ipaddr and then does the same thing as search(), using that
        guessed latitude and longitude.
        """
        precondition(is_valid_ip(ipaddr), ipaddr)
        precondition(radius is None or is_numeric(radius), radius)
        precondition(query is None or isinstance(query, basestring), query)
        precondition(category is None or isinstance(category, basestring), category)

        if isinstance(query, unicode):
            query = query.encode('utf-8')
        if isinstance(category, unicode):
            category = category.encode('utf-8')

        kwargs = { }
        if radius:
            kwargs['radius'] = radius
        if query:
            kwargs['q'] = query
        if category:
            kwargs['category'] = category
        quargs = urllib.urlencode(kwargs)
        if quargs:
            quargs = '?'+quargs
        endpoint = self._endpoint('search_by_ip', ipaddr=ipaddr, quargs=quargs)

        result = self._request(endpoint, 'GET')[1]

        fc = json_decode(result)
        return [Feature.from_dict(f) for f in fc['features']]
    def search_by_my_ip(self, radius=None, query=None, category=None):
        """
        Search for places near your IP address, within a radius (in
        kilometers).

        The server gets the IP address from the HTTP connection (this
        may be the IP address of your device or of a firewall, NAT, or
        HTTP proxy device between you and the server), and then does
        the same thing as search_by_ip(), using that IP address.
        """
        precondition(radius is None or is_numeric(radius), radius)
        precondition(query is None or isinstance(query, basestring), query)
        precondition(category is None or isinstance(category, basestring), category)

        if isinstance(query, unicode):
            query = query.encode('utf-8')
        if isinstance(category, unicode):
            category = category.encode('utf-8')

        kwargs = { }
        if radius:
            kwargs['radius'] = radius
        if query:
            kwargs['q'] = query
        if category:
            kwargs['category'] = category
        quargs = urllib.urlencode(kwargs)
        if quargs:
            quargs = '?'+quargs
        endpoint = self._endpoint('search_by_my_ip', quargs=quargs)

        result = self._request(endpoint, 'GET')[1]

        fc = json_decode(result)
        return [Feature.from_dict(f) for f in fc['features']]
    def search(self, lat, lon, radius=None, query=None, category=None):
        """Search for places near a lat/lon, within a radius (in kilometers)."""
        precondition(is_valid_lat(lat), lat)
        precondition(is_valid_lon(lon), lon)
        precondition(radius is None or is_numeric(radius), radius)
        precondition(query is None or isinstance(query, basestring), query)
        precondition(category is None or isinstance(category, basestring), category)

        if isinstance(query, unicode):
            query = query.encode('utf-8')
        if isinstance(category, unicode):
            category = category.encode('utf-8')

        kwargs = { }
        if radius:
            kwargs['radius'] = radius
        if query:
            kwargs['q'] = query
        if category:
            kwargs['category'] = category
        quargs = urllib.urlencode(kwargs)
        if quargs:
            quargs = '?'+quargs
        endpoint = self._endpoint('search', lat=lat, lon=lon, quargs=quargs)

        result = self._request(endpoint, 'GET')[1]

        fc = json_decode(result)
        return [Feature.from_dict(f) for f in fc['features']]
    def test_record_from_dict(self):
        record_dict = {
            "geometry": {"type": "Point", "coordinates": [D("10.0"), D("11.0")]},
            "type": "Feature",
            "properties": {"record_id": "my_id", "key": "value", "type": "object"},
        }

        record = Feature.from_dict(record_dict)
        self.assertEquals(record.coordinates[0], D("11.0"))
        self.assertEquals(record.coordinates[1], D("10.0"))
        self.assertEquals(record.properties.get("record_id"), "my_id")
        self.assertEquals(record.properties["key"], "value")
        self.assertEquals(record.properties["type"], "object")

        record_dict = {
            "geometry": {"type": "Point", "coordinates": [D("10.0"), D("11.0")]},
            "id": "SG_abcdefghijklmnopqrstuv",
            "type": "Feature",
            "properties": {"key": "value", "type": "object"},
        }

        record = Feature.from_dict(record_dict)
        self.assertEquals(record.properties.get("record_id"), None)
        self.assertEquals(record.id, "SG_abcdefghijklmnopqrstuv")

        record_dict = {
            "geometry": {"type": "Point", "coordinates": [D("10.0"), D("11.0")]},
            "id": "SG_abcdefghijklmnopqrstuv",
            "type": "Feature",
            "properties": {"record_id": "my_id", "key": "value", "type": "object"},
        }

        record = Feature.from_dict(record_dict)
        self.assertEquals(record.properties.get("record_id"), "my_id")
        self.assertEquals(record.id, "SG_abcdefghijklmnopqrstuv")

        record_dict = {
            "geometry": {"type": "Point", "coordinates": [10.0, 11.0]},
            "type": "Feature",
            "properties": {"record_id": "my_id", "key": "value", "type": "object"},
        }

        record = Feature.from_dict(record_dict)
        self.assertEquals(record.coordinates[0], 11.0)
        self.assertEquals(record.coordinates[1], 10.0)
    def test_record_constructor(self):
        self.failUnlessRaises(AssertionError, Feature, D("11.0"), D("10.0"), properties={"record_id": "my_id"})

        # lat exceeds bound
        self.failUnlessRaises(AssertionError, Feature, (D("91.0"), D("10.1")), properties={"record_id": "my_id"})

        # lon exceeds bound
        self.failUnlessRaises(AssertionError, Feature, (D("10.1"), D("180.1")), properties={"record_id": "my_id"})

        record = Feature(coordinates=(D("11.0"), D("10.0")), properties={"record_id": "my_id"})
        self.failUnlessEqual(record.properties.get("record_id"), "my_id")
        self.failUnlessEqual(record.id, None)
        self.failUnlessEqual(record.geomtype, "Point")
        self.failUnlessEqual(record.coordinates[0], D("11.0"))
        self.failUnlessEqual(record.coordinates[1], D("10.0"))

        record = Feature(coordinates=(D("11.0"), D("10.0")), simplegeohandle="SG_abcdefghijklmnopqrstuv")
        self.failUnlessEqual(record.properties.get("record_id"), None)
        self.failUnlessEqual(record.id, "SG_abcdefghijklmnopqrstuv")

        record = Feature(
            coordinates=(D("11.0"), D("10.0")),
            properties={"record_id": "my_id"},
            simplegeohandle="SG_abcdefghijklmnopqrstuv",
        )
        self.failUnlessEqual(record.properties.get("record_id"), "my_id")
        self.failUnlessEqual(record.id, "SG_abcdefghijklmnopqrstuv")

        record = Feature(coordinates=(D("11.0"), D("10.0")))
        self.failUnlessEqual(record.properties.get("record_id"), None)
        self.failUnlessEqual(record.id, None)

        record = Feature((D("11.0"), D("10.0")), properties={"record_id": "my_id"})
        self.failUnlessEqual(record.properties.get("record_id"), "my_id")

        record = Feature((11.0, 10.0), properties={"record_id": "my_id"})
        self.failUnlessEqual(record.geomtype, "Point")
        self.failUnlessEqual(record.coordinates[0], 11.0)
        self.failUnlessEqual(record.coordinates[1], 10.0)
        self.failUnlessEqual(record.properties.get("record_id"), "my_id")

        record = Feature([[(11.0, 179.9), (12, -179.9)]], geomtype="Polygon")
        self.failUnlessEqual(record.geomtype, "Polygon")
        self.failUnlessEqual(len(record.coordinates[0]), 2)
        self.failUnlessEqual(record.coordinates[0][0], (11.0, 179.9))

        jsondict = record.to_dict()
        self.failUnlessEqual(jsondict["geometry"]["coordinates"][0][0], (179.9, 11.0))
    def test_record_constructor(self):
        self.failUnlessRaises(TypeError, Feature, D('11.0'), D('10.0'), properties={'record_id': 'my_id'})

        # lat exceeds bound
        self.failUnlessRaises(TypeError, Feature, (D('91.0'), D('10.1')), properties={'record_id': 'my_id'})

        # lon exceeds bound
        self.failUnlessRaises(TypeError, Feature, (D('10.1'), D('180.1')), properties={'record_id': 'my_id'}, strict_lon_validation=True)

        record = Feature(coordinates=(D('11.0'), D('10.0')), properties={'record_id': 'my_id'})
        self.failUnlessEqual(record.properties.get('record_id'), 'my_id')
        self.failUnlessEqual(record.id, None)
        self.failUnlessEqual(record.geomtype, 'Point')
        self.failUnlessEqual(record.coordinates[0], D('11.0'))
        self.failUnlessEqual(record.coordinates[1], D('10.0'))

        record = Feature(coordinates=(D('11.0'), D('10.0')), simplegeohandle='SG_abcdefghijklmnopqrstuv')
        self.failUnlessEqual(record.properties.get('record_id'), None)
        self.failUnlessEqual(record.id, 'SG_abcdefghijklmnopqrstuv')

        record = Feature(coordinates=(D('11.0'), D('10.0')), properties={'record_id': 'my_id'}, simplegeohandle='SG_abcdefghijklmnopqrstuv')
        self.failUnlessEqual(record.properties.get('record_id'), 'my_id')
        self.failUnlessEqual(record.id, 'SG_abcdefghijklmnopqrstuv')

        record = Feature(coordinates=(D('11.0'), D('10.0')))
        self.failUnlessEqual(record.properties.get('record_id'), None)
        self.failUnlessEqual(record.id, None)

        record = Feature((D('11.0'), D('10.0')), properties={'record_id': 'my_id'})
        self.failUnlessEqual(record.properties.get('record_id'), 'my_id')

        record = Feature((11.0, 10.0), properties={'record_id': 'my_id'})
        self.failUnlessEqual(record.geomtype, 'Point')
        self.failUnlessEqual(record.coordinates[0], 11.0)
        self.failUnlessEqual(record.coordinates[1], 10.0)
        self.failUnlessEqual(record.properties.get('record_id'), 'my_id')

        record = Feature([[(11.0, 179.9), (12, -179.9)]], geomtype='Polygon')
        self.failUnlessEqual(record.geomtype, 'Polygon')
        self.failUnlessEqual(len(record.coordinates[0]), 2)
        self.failUnlessEqual(record.coordinates[0][0], (11.0, 179.9))

        jsondict = record.to_dict()
        self.failUnlessEqual(jsondict['geometry']['coordinates'][0][0], (179.9, 11.))
    def test_record_to_dict_sets_id_correctly(self):
        handle = "SG_abcdefghijklmnopqrstuv"
        record_id = "this is my record #1. my first record. and it is mine"
        rec = Feature(coordinates=(D("11.03"), D("10.03")), simplegeohandle=handle, properties={"record_id": record_id})
        dic = rec.to_dict()
        self.failUnlessEqual(dic.get("id"), handle)
        self.failUnlessEqual(dic.get("properties", {}).get("record_id"), record_id)

        rec = Feature(coordinates=(D("11.03"), D("10.03")), simplegeohandle=handle, properties={"record_id": None})
        dic = rec.to_dict()
        self.failUnlessEqual(dic.get("id"), handle)
        self.failUnlessEqual(dic.get("properties", {}).get("record_id"), None)

        rec = Feature(coordinates=(D("11.03"), D("10.03")), simplegeohandle=handle, properties={"record_id": None})
        dic = rec.to_dict()
        self.failUnlessEqual(dic.get("id"), handle)
        self.failUnlessEqual(dic.get("properties", {}).get("record_id"), None)

        rec = Feature(coordinates=(D("11.03"), D("10.03")), simplegeohandle=None, properties={"record_id": None})
        dic = rec.to_dict()
        self.failUnlessEqual(dic.get("id"), None)
        self.failUnlessEqual(dic.get("properties", {}).get("record_id"), None)
    def test_record_to_dict_sets_id_correctly(self):
        handle = 'SG_abcdefghijklmnopqrstuv'
        record_id = 'this is my record #1. my first record. and it is mine'
        rec = Feature(coordinates=(D('11.03'), D('10.03')), simplegeohandle=handle, properties={'record_id': record_id})
        dic = rec.to_dict()
        self.failUnlessEqual(dic.get('id'), handle)
        self.failUnlessEqual(dic.get('properties', {}).get('record_id'), record_id)

        rec = Feature(coordinates=(D('11.03'), D('10.03')), simplegeohandle=handle, properties={'record_id': None})
        dic = rec.to_dict()
        self.failUnlessEqual(dic.get('id'), handle)
        self.failUnlessEqual(dic.get('properties', {}).get('record_id'), None)

        rec = Feature(coordinates=(D('11.03'), D('10.03')), simplegeohandle=handle, properties={'record_id': None})
        dic = rec.to_dict()
        self.failUnlessEqual(dic.get('id'), handle)
        self.failUnlessEqual(dic.get('properties', {}).get('record_id'), None)

        rec = Feature(coordinates=(D('11.03'), D('10.03')), simplegeohandle=None, properties={'record_id': None})
        dic = rec.to_dict()
        self.failUnlessEqual(dic.get('id'), None)
        self.failUnlessEqual(dic.get('properties', {}).get('record_id'), None)
    def test_record_from_dict_lon_validation(self):
        record_dict = {
                     'geometry' : {
                                   'type' : 'Point',
                                   'coordinates' : [D('181.0'), D('11.0')]
                                   },
                     'type' : 'Feature',
                     'properties' : {
                                     'record_id' : 'my_id',
                                     'key' : 'value'  ,
                                     'type' : 'object'
                                     }
                     }

        self.failUnlessRaises(TypeError, Feature.from_dict, record_dict, True)
        record = Feature.from_dict(record_dict, strict_lon_validation=False)
        self.assertEquals(record.coordinates[0], D('11.0'))
        self.assertEquals(record.coordinates[1], D('181.0'))
        self.assertEquals(record.properties.get('record_id'), 'my_id')
        self.assertEquals(record.properties['key'], 'value')
        self.assertEquals(record.properties['type'], 'object')
    def search_by_address(self, address, radius=None, query=None, category=None):
        """
        Search for places near the given address, within a radius (in
        kilometers).

        The server figures out the latitude and longitude from the
        street address and then does the same thing as search(), using
        that deduced latitude and longitude.
        """
        precondition(isinstance(address, basestring), address)
        precondition(address != '', address)
        precondition(radius is None or is_numeric(radius), radius)
        precondition(query is None or isinstance(query, basestring), query)
        precondition(category is None or isinstance(category, basestring), category)

        if isinstance(address, unicode):
            address = address.encode('utf-8')
        if isinstance(query, unicode):
            query = query.encode('utf-8')
        if isinstance(category, unicode):
            category = category.encode('utf-8')

        kwargs = { 'address': address }
        if radius:
            kwargs['radius'] = radius
        if query:
            kwargs['q'] = query
        if category:
            kwargs['category'] = category
        quargs = urllib.urlencode(kwargs)
        endpoint = self._endpoint('search_by_address', quargs=quargs)

        result = self._request(endpoint, 'GET')[1]

        fc = json_decode(result)
        return [Feature.from_dict(f) for f in fc['features']]
    def search(self, lat, lon, radius=None, query=None, category=None):
        """Search for places near a lat/lon."""
        precondition(is_valid_lat(lat), lat)
        precondition(is_valid_lon(lon), lon)
        precondition(radius is None or is_numeric(radius), radius)
        precondition(query is None or isinstance(query, basestring), query)
        precondition(category is None or isinstance(category, basestring), category)

        kwargs = {}
        if radius:
            kwargs["radius"] = radius
        if query:
            kwargs["q"] = query
        if category:
            kwargs["category"] = category
        quargs = urllib.urlencode(kwargs)
        if quargs:
            quargs = "?" + quargs
        endpoint = self._endpoint("search", lat=lat, lon=lon, quargs=quargs)

        result = self._request(endpoint, "GET")[1]

        fc = json_decode(result)
        return [Feature.from_dict(f) for f in fc["features"]]
 def test_geojson_is_correct(self):
     f = Feature(coordinates=[-90, D('171.0')], properties={'record_id': 'my_id'}, strict_lon_validation=True)
     stringy = f.to_json()
     self.failUnlessEqual(stringy, '{"geometry": {"type": "Point", "coordinates": [171.0, -90]}, "type": "Feature", "id": null, "properties": {"record_id": "my_id", "private": false}}')
    def test_record_from_dict(self):
        record_dict = {
                     'geometry' : {
                                   'type' : 'Point',
                                   'coordinates' : [D('10.0'), D('11.0')]
                                   },
                     'type' : 'Feature',
                     'properties' : {
                                     'record_id' : 'my_id',
                                     'key' : 'value'  ,
                                     'type' : 'object'
                                     }
                     }

        record = Feature.from_dict(record_dict)
        self.assertEquals(record.coordinates[0], D('11.0'))
        self.assertEquals(record.coordinates[1], D('10.0'))
        self.assertEquals(record.properties.get('record_id'), 'my_id')
        self.assertEquals(record.properties['key'], 'value')
        self.assertEquals(record.properties['type'], 'object')

        self.assertEquals(record.to_json(), '{"geometry": {"type": "Point", "coordinates": [10.0, 11.0]}, "type": "Feature", "id": null, "properties": {"record_id": "my_id", "type": "object", "private": false, "key": "value"}}')

        record_dict = {
                     'geometry' : {
                                   'type' : 'Point',
                                   'coordinates' : [D('10.0'), D('11.0')]
                                   },
                     'id' : 'SG_abcdefghijklmnopqrstuv',
                     'type' : 'Feature',
                     'properties' : {
                                     'key' : 'value'  ,
                                     'type' : 'object'
                                     }
                     }

        record = Feature.from_dict(record_dict)
        self.assertEquals(record.properties.get('record_id'), None)
        self.assertEquals(record.id, 'SG_abcdefghijklmnopqrstuv')

        record_dict = {
                     'geometry' : {
                                   'type' : 'Point',
                                   'coordinates' : [D('10.0'), D('11.0')]
                                   },
                     'id' : 'SG_abcdefghijklmnopqrstuv',
                     'type' : 'Feature',
                     'properties' : {
                                     'record_id' : 'my_id',
                                     'key' : 'value'  ,
                                     'type' : 'object'
                                     }
                     }

        record = Feature.from_dict(record_dict)
        self.assertEquals(record.properties.get('record_id'), 'my_id')
        self.assertEquals(record.id, 'SG_abcdefghijklmnopqrstuv')

        record_dict = {
                     'geometry' : {
                                   'type' : 'Point',
                                   'coordinates' : [10.0, 11.0]
                                   },
                     'type' : 'Feature',
                     'properties' : {
                                     'record_id' : 'my_id',
                                     'key' : 'value'  ,
                                     'type' : 'object'
                                     }
                     }

        record = Feature.from_dict(record_dict)
        self.assertEquals(record.coordinates[0], 11.0)
        self.assertEquals(record.coordinates[1], 10.0)