Exemple #1
0
    def entry_to_feature(self, placemark_dom):
        feature = Feature()
        points = placemark_dom.getElementsByTagName("Point")
        lines = placemark_dom.getElementsByTagName("LineString")
        polys = placemark_dom.getElementsByTagName("Polygon")
        if len(points):
            coords = points[0].getElementsByTagName("coordinates")[0].firstChild.nodeValue.strip().split(",")
            feature.geometry = {"type": "Point", "coordinates": [map(float, coords)]}
        elif len(lines):
            coordstring = lines[0].getElementsByTagName("coordinates")[0].firstChild.nodeValue.strip()
            coords = coordstring.split(" ")
            coords = map(lambda x: x.split(","), coords)
            feature.geometry = {"type": "Line", "coordinates": coords}
        elif len(polys):
            rings = []
            poly = polys[0]
            outer = poly.getElementsByTagName("outerBoundaryIs")[0]
            outer_coordstring = outer.getElementsByTagName("coordinates")[0].firstChild.nodeValue.strip()
            outer_coords = outer_coordstring.split(" ")
            outer_coords = map(lambda x: x.split(","), outer_coords)
            rings.append(outer_coords)
            inners = poly.getElementsByTagName("innerBoundaryIs")
            for inner in inners:
                inner_coords = inner.getElementsByTagName("coordinates")[0].firstChild.nodeValue.strip().split(" ")
                inner_coords = map(lambda x: x.split(","), inner_coords)
                rings.append(inner_coords)

            feature.geometry = {"type": "Polygon", "coordinates": rings}

        else:
            raise Exception(
                "KML parser only understands points and lines, and polys. You seem to be missing something."
            )
        nodeList = placemark_dom.childNodes
        if len(placemark_dom.getElementsByTagName("Metadata")):
            nodeList += placemark_dom.getElementsByTagName("Metadata")[0].childNodes
        for node in nodeList:
            try:
                attr_name = node.tagName.split(":")[-1]
                value = node.firstChild.nodeValue
                if node.tagName not in ["Point", "LineString", "Polygon", "name", "Metadata"] and not value.startswith(
                    "Properties:"
                ):
                    feature.properties[attr_name] = value
            except:
                pass

            try:
                feature.properties["title"] = placemark_dom.getElementsByTagName("name")[0].firstChild.nodeValue
            except:
                pass

        return feature
Exemple #2
0
 def entry_to_feature(self, entry_dom):
     id = 1 
     try:
         id = entry_dom.getElementsByTagName("id")[0].firstChild.nodeValue
     except:
         id = 1
     feature = Feature(str(id))
     
     geometry = self.extract_entry_geometry(entry_dom)
     
     if not geometry: return None
     
     feature.geometry = geometry
     
     for node in entry_dom.childNodes:
         try:
             attr_name = node.tagName.split(":")[-1]
             if attr_name not in ['point', 'line', 'polygon', 'id', 'where']:
                 try:
                     feature.properties[attr_name] = node.firstChild.nodeValue
                 except:
                     pass
         except:
             pass
             
     feature.properties['timestamp'] = time.time()        
     
     return feature    
Exemple #3
0
 def createFeature(self, feature_dict, id = None):
     feature = Feature(id)
     if feature_dict.has_key('geometry'):
         feature.geometry = feature_dict['geometry']
     if feature_dict.has_key('properties'):
         feature.properties = feature_dict['properties']
     return feature 
Exemple #4
0
 def createFeature(self, feature_dict, id = None):
     feature = Feature(id)
     if feature_dict.has_key('geometry'):
         feature.geometry = feature_dict['geometry']
         if feature.geometry['type'] == "Point":
             feature.geometry['coordinates'] = [feature.geometry['coordinates']] 
         if feature.geometry['type'] == "LineString":
             feature.geometry['type'] = "Line"
     if feature_dict.has_key('properties'):
         feature.properties = feature_dict['properties']
     return feature 
Exemple #5
0
    def freeze_features (self, features):
        result = []
        for ogrfeat in features:
            feat = Feature(ogrfeat.GetFID())

            geom = ogrfeat.GetGeometryRef()
            feat.geometry = OGR.freeze_geometry(geom)

            for n, defn in enumerate(self.fields):
                value = ogrfeat.GetField(n)
                if isinstance(value, str): value = unicode(value, "utf-8")
                feat.properties[defn.GetName()] = value 

            result.append(feat)
            ogrfeat.Destroy() 

        return result