Beispiel #1
0
    def encode(self, features, props=None, fixed_props=False, **kwargs):
        """
        >>> feat = Feature(1, {"type":"Point", "coordinates":[1,1]}, {"a":"b"})
        >>> c = CSV()
        >>> c.encode([feat]).replace("\\r\\n", " ")
        'id,a,geometry 1,b,"1,1" '
        >>> c.encode([feat], ["geometry","a","b","id"]).replace("\\r\\n", " ")
        'geometry,a,b,id "1,1",b,,1 '
        >>> c.encode([feat], props=["geometry","id"],fixed_props=True).replace("\\r\\n", " ")
        'geometry,id "1,1",1 '
        """

        s = StringIO.StringIO()
        w = csv.writer(s)

        if props == None:
            props = []

        if not "id" in props and self.include_id:
            props.append("id")

        if not fixed_props:
            for feature in features:
                for key in feature.properties.keys():
                    if not key in props:
                        props.append(key)

        if not "geometry" in props:
            props.append("geometry")

        w.writerow(props)

        for feature in features:
            #if feature.geometry['type'] != "Point":
            #    continue
            row = []
            for key in props:
                if key == "id":
                    row.append(feature.id)
                elif key == "geometry":
                    geom = to_wkt(feature.geometry)
                    #geom = ",".join(map(str, feature.geometry['coordinates']))
                    row.append(geom)
                elif feature.properties.has_key(key):
                    val = feature.properties[key]
                    if isinstance(val, unicode):
                        val = val.encode("utf-8")
                    row.append(val)
                else:
                    row.append("")
            w.writerow(row)
        s.seek(0)
        return s
Beispiel #2
0
    def encode(self, features, props = None, fixed_props = False, **kwargs):
        """
        >>> feat = Feature(1, {"type":"Point", "coordinates":[1,1]}, {"a":"b"})
        >>> c = CSV()
        >>> c.encode([feat]).replace("\\r\\n", " ")
        'id,a,geometry 1,b,"1,1" '
        >>> c.encode([feat], ["geometry","a","b","id"]).replace("\\r\\n", " ")
        'geometry,a,b,id "1,1",b,,1 '
        >>> c.encode([feat], props=["geometry","id"],fixed_props=True).replace("\\r\\n", " ")
        'geometry,id "1,1",1 '
        """
        
        s = StringIO.StringIO()
        w = csv.writer(s)
        
        if props == None:
            props = []
        
        if not "id" in props and self.include_id:
            props.append("id")
        
        if not fixed_props:
            for feature in features:
                for key in feature.properties.keys():
                    if not key in props:
                        props.append(key)
        
        if not "geometry" in props:
            props.append("geometry")
        
        w.writerow(props)

        for feature in features:
            #if feature.geometry['type'] != "Point":
            #    continue
            row = []
            for key in props:
                if key == "id":
                    row.append(feature.id)
                elif key == "geometry":
                    geom = to_wkt(feature.geometry)
                    #geom = ",".join(map(str, feature.geometry['coordinates']))
                    row.append(geom)
                elif feature.properties.has_key(key):
                    val = feature.properties[key]
                    if isinstance(val, unicode):
                        val = val.encode("utf-8")
                    row.append(val)
                else:
                    row.append("")
            w.writerow(row)
        s.seek(0)
        return s
Beispiel #3
0
 def update(self, action):
     obj = self.model.get_by_id(int(action.id))
     obj.geometry = to_wkt(action.feature.geometry)
     for key, value in action.feature.properties.items():
         setattr(obj, str(key), value)
     if geohash_support:
         bbox = action.feature.get_bbox()
         union = Geoindex(bbox[0:2]) + Geoindex(bbox[2:])
         obj.geohash = str(union)
     obj.save()
     action.id = int(obj.key().id())
     return self.select(action)
Beispiel #4
0
 def update(self, action):
     obj = self.model.get_by_id(int(action.id))
     obj.geometry = to_wkt(action.feature.geometry)
     for key, value in action.feature.properties.items():
         setattr(obj, str(key), value)
     if geohash_support:
         bbox = action.feature.get_bbox()
         union = Geoindex(bbox[0:2]) + Geoindex(bbox[2:])
         obj.geohash = str(union)
     obj.save()
     action.id = int(obj.key().id())
     return self.select(action)
Beispiel #5
0
 def create(self, action):
     props = {}
     for key, value in action.feature.properties.items():
         props[str(key)] = value
     obj = self.model(**props)
     obj.geometry = to_wkt(action.feature.geometry)
     if geohash_support:
         bbox = action.feature.get_bbox()
         union = Geoindex(bbox[0:2]) + Geoindex(bbox[2:])
         obj.geohash = str(union)
     obj.save()
     action.id = int(obj.key().id())
     return self.select(action)
Beispiel #6
0
 def create(self, action):
     props = {}
     for key, value in action.feature.properties.items():
         props[str(key)] = value
     obj = self.model(**props)
     obj.geometry = to_wkt(action.feature.geometry)
     if geohash_support:
         bbox = action.feature.get_bbox()
         union = Geoindex(bbox[0:2]) + Geoindex(bbox[2:])
         obj.geohash = str(union)
     obj.save()
     action.id = int(obj.key().id())
     return self.select(action)
    def update(self, action):
        #obj = self.model.get_by_id(int(action.id))
        kn = self.get_keyname(action.id)
        obj = self.model.get_by_key_name(kn)
        obj.geometry = to_wkt(action.feature.geometry)

        for key, value in action.feature.properties.items():
            setattr(obj, str(key), value)
        obj.update_location()
        try: obj.save()
        except: obj.save()
        #action.id = obj.key().id()
        return self.select(action)
Beispiel #8
0
    def update(self, action):
        #obj = self.model.get_by_id(int(action.id))
        kn = self.get_keyname(action.id)
        obj = self.model.get_by_key_name(kn)
        obj.geometry = to_wkt(action.feature.geometry)

        for key, value in action.feature.properties.items():
            setattr(obj, str(key), value)
        obj.update_location()
        try:
            obj.save()
        except:
            obj.save()
        #action.id = obj.key().id()
        return self.select(action)
 def insert(self, action):
     props = {}
     coords = action.feature.geometry['coordinates']
     for key, value in action.feature.properties.items():
         props[str(key)] = value
     props['location'] = db.GeoPt(coords[1],coords[0])
     obj = self.model(**props)
     obj.geometry = to_wkt(action.feature.geometry)
     try:
         obj.update_location()
     except:
         raise Exception(str([props['location'],obj.location]))
     try: obj.save()
     except: obj.save()
     if not action.id: 
         action.id = obj.key().id()
     return self.select(action)
Beispiel #10
0
 def insert(self, action):
     props = {}
     coords = action.feature.geometry['coordinates']
     for key, value in action.feature.properties.items():
         props[str(key)] = value
     props['location'] = db.GeoPt(coords[1], coords[0])
     obj = self.model(**props)
     obj.geometry = to_wkt(action.feature.geometry)
     try:
         obj.update_location()
     except:
         raise Exception(str([props['location'], obj.location]))
     try:
         obj.save()
     except:
         obj.save()
     if not action.id:
         action.id = obj.key().id()
     return self.select(action)
Beispiel #11
0
    def geometry_to_gml(self, geometry, srs):
        """
        >>> w = WFS()
        >>> print w.geometry_to_gml({'type':'Point', 'coordinates':[1.0,2.0]})
        <gml:Point><gml:coordinates>1.0,2.0</gml:coordinates></gml:Point>
        >>> w.geometry_to_gml({'type':'LineString', 'coordinates':[[1.0,2.0],[2.0,1.0]]})
        '<gml:LineString><gml:coordinates>1.0,2.0 2.0,1.0</gml:coordinates></gml:LineString>'
        """
        
        if "EPSG" not in str(srs):
            srs = "EPSG:" + str(srs)

        if geometry['type'].lower() in \
                ['point', 'linestring', 'polygon', 'multipolygon', 'multilinestring', 'multipoint']:
            wkt = to_wkt(geometry)
            geom_wkt = ogr.CreateGeometryFromWkt(wkt)

            gml = geom_wkt.ExportToGML()
            return gml
        else:
            raise Exception("Could not convert geometry of type %s." % geometry['type'])