Exemple #1
0
def extract(data, start, end):
    if not data.startswith(start):
        raise HXLException('Malformed WKT: Couldn\'t find %s' %
                           (repr(start), ))

    if not data.endswith(end):
        raise HXLException('Malformed WKT: Couldn\'t find %s' % (repr(end), ))

    return data[len(start):-len(end)]
Exemple #2
0
    def make_request(self, need_auth, method, url, body=None):
        conn = httplib.HTTPConnection(self.server, self.port)
        headers = {'Accept': 'application/xml,text/plain'}

        if body is None:
            pass
        elif type(body) is dict:
            headers['Content-type'] = 'application/json'
            body = json.dumps(body)

        elif type(body) is et._Element:
            headers['Content-type'] = 'application/xml'
            body = et.tostring(body)

        elif type(body) is str:
            headers['Content-type'] = 'text/plain'

        else:
            raise HXLException('Can\'t send %s' % type(body).__name__)

        if need_auth:
            headers['Authorization'] = self.auth

        conn.request(method, self.path + '/' + url, body, headers)
        return conn.getresponse()
Exemple #3
0
    def create_layer(self, layer_name, layer_title, attrs):
        attributes = []

        for (name, binding) in attrs.items():
            attributes.append({'name': name, 'binding': binding})

        operation = {
            'featureType': {
                'name': layer_name,
                'nativeName': layer_name,
                'title': layer_title,
                'abstract': 'HXL imported data (%s)' % (time.ctime(), ),
                'srs': 'EPSG:4326',
                'attributes': {
                    'attribute': attributes
                }
            }
        }

        response = self.make_rest_request(
            'POST', 'workspaces/topp/datastores/%s/featuretypes.json' %
            (self.datastore, ), operation)

        if response.status != 201:
            raise HXLException(response.read())
Exemple #4
0
    def update_layer(self, layer_name, featuretype):
        response = self.make_rest_request('PUT',
                                          self.featuretype_url(layer_name),
                                          featuretype)

        if response.status != 200:
            raise HXLException(response.read())
Exemple #5
0
def parse_coord(data):
    coord = data.split(' ')

    try:
        (x, y) = coord
        return (float(x), float(y))
    except:
        raise HXLException('Malformed co-ordinate %s' % (repr(coord), ))
Exemple #6
0
def bounding_box(polygons):
    if not len(polygons):
        raise HXLException('len(polygons) == 0')

    minx, miny, maxx, maxy = None, None, None, None

    for polygon in polygons:
        if type(polygon) is Polygon:
            for (x, y) in polygon.coords:
                minx, miny, maxx, maxy = update_bounding_box(
                    minx, miny, maxx, maxy, x, y)

        elif type(polygon) is Point:
            (x, y) = polygon.coord
            minx, miny, maxx, maxy = update_bounding_box(
                minx, miny, maxx, maxy, x, y)

        else:
            raise HXLException('Unknown polygon %s' % type(polygon).__name__)

    return minx, miny, maxx, maxy
Exemple #7
0
def parse_wkt(data):
    '''Parse a WKT string and return a tuple of (type, coordinates)'''

    if data.startswith('POLYGON '):
        data = extract(data, 'POLYGON ((', '))')
        return [Polygon(parse_coords(data))]

    elif data.startswith('MULTIPOLYGON '):
        data = extract(data, 'MULTIPOLYGON (((', ')))')
        polygons = data.split(')),((')
        return [Polygon(parse_coords(polygon)) for polygon in polygons]

    elif data.startswith('POINT '):
        data = extract(data, 'POINT (', ')')
        return Point(parse_coord(data))

    else:
        raise HXLException('Unknown WKT type %s' %
                           (repr(data.split(' ', 1)[0])))
Exemple #8
0
def encode_polygons(polygons):
    f = StringIO.StringIO()
    f.write('MULTIPOLYGON (')

    for (pindex, polygon) in enumerate(polygons):
        if type(polygon) is not Polygon:
            raise HXLException('Wanted Polygon, got %s' % (type(polygon), ))

        f.write('((')
        f.write(','.join('%f %f' % coord for coord in polygon.coords))
        f.write('))')

        if pindex != len(polygons) - 1:
            f.write(',')

    f.write(')')

    buf = f.getvalue()
    f.close()

    return buf