def kml2pykml(): "Parse a KML file and generates a pyKML script" import urllib2 from pykml.parser import parse from optparse import OptionParser parser = OptionParser( usage="usage: %prog FILENAME_or_URL", version="%prog 0.1", ) (options, args) = parser.parse_args() if len(args) != 1: parser.error("wrong number of arguments") else: uri = args[0] try: with open(uri) as f: doc = parse(f, schema=None) except IOError: try: f = urllib2.urlopen(uri) doc = parse(f, schema=None) finally: #pass try: f except NameError: pass #variable was not defined else: f.close print write_python_script_for_kml_document(doc)
def test_parse_kml_document(self): "Tests the parsing of an valid KML file object" test_kml = '<kml xmlns="http://www.opengis.net/kml/2.2"/>' fileobject = StringIO(test_kml) schema = Schema("ogckml22.xsd") tree = parse(fileobject, schema=schema) self.assertEquals(etree.tostring(tree), test_kml) tree = parse(fileobject, schema=schema) self.assertEquals(etree.tostring(tree), test_kml)
def test_parse_kml_document(self): """Tests the parsing of an valid KML file object""" test_kml = b'<kml xmlns="http://www.opengis.net/kml/2.2"/>' fileobject = BytesIO(test_kml) schema = Schema('ogckml22.xsd') tree = parse(fileobject, schema=schema) self.assertEqual(etree.tostring(tree), test_kml) tree = parse(fileobject, schema=schema) self.assertEqual(etree.tostring(tree), test_kml)
def test_parse_kml_file_with_cdata(self): "Tests the parsing of a local KML file, with a CDATA description string" test_datafile = path.join( path.dirname(__file__), 'testfiles', 'google_kml_tutorial/using_the_cdata_element.kml' ) # parse with validation with open(test_datafile) as f: doc = parse(f, schema=Schema('ogckml22.xsd')) self.assertEqual( etree.tostring(doc).decode(), '<kml xmlns="http://www.opengis.net/kml/2.2">' '<Document>' '<Placemark>' '<name>CDATA example</name>' '<description>' '<![CDATA[\n' ' <h1>CDATA Tags are useful!</h1>\n' ' <p><font color="red">Text is <i>more readable</i> and \n' ' <b>easier to write</b> when you can avoid using entity \n' ' references.</font></p>\n' ' ]]>' '</description>' '<Point>' '<coordinates>102.595626,14.996729</coordinates>' '</Point>' '</Placemark>' '</Document>' '</kml>' ) # parse without validation with open(test_datafile) as f: doc2 = parse(f) self.assertEqual( etree.tostring(doc2).decode(), '<kml xmlns="http://www.opengis.net/kml/2.2">' '<Document>' '<Placemark>' '<name>CDATA example</name>' '<description>' '<![CDATA[\n' ' <h1>CDATA Tags are useful!</h1>\n' ' <p><font color="red">Text is <i>more readable</i> and \n' ' <b>easier to write</b> when you can avoid using entity \n' ' references.</font></p>\n' ' ]]>' '</description>' '<Point>' '<coordinates>102.595626,14.996729</coordinates>' '</Point>' '</Placemark>' '</Document>' '</kml>' )
def test_parse_kml_file_with_cdata(self): "Tests the parsing of a local KML file, with a CDATA description string" test_datafile = path.join( path.dirname(__file__), 'testfiles', 'google_kml_tutorial/using_the_cdata_element.kml' ) # parse with validation with open(test_datafile) as f: doc = parse(f, schema=Schema('ogckml22.xsd')) self.assertEquals( etree.tostring(doc), '<kml xmlns="http://www.opengis.net/kml/2.2">' '<Document>' '<Placemark>' '<name>CDATA example</name>' '<description>' '<![CDATA[\n' ' <h1>CDATA Tags are useful!</h1>\n' ' <p><font color="red">Text is <i>more readable</i> and \n' ' <b>easier to write</b> when you can avoid using entity \n' ' references.</font></p>\n' ' ]]>' '</description>' '<Point>' '<coordinates>102.595626,14.996729</coordinates>' '</Point>' '</Placemark>' '</Document>' '</kml>' ) # parse without validation with open(test_datafile) as f: doc2 = parse(f) self.assertEquals( etree.tostring(doc2), '<kml xmlns="http://www.opengis.net/kml/2.2">' '<Document>' '<Placemark>' '<name>CDATA example</name>' '<description>' '<![CDATA[\n' ' <h1>CDATA Tags are useful!</h1>\n' ' <p><font color="red">Text is <i>more readable</i> and \n' ' <b>easier to write</b> when you can avoid using entity \n' ' references.</font></p>\n' ' ]]>' '</description>' '<Point>' '<coordinates>102.595626,14.996729</coordinates>' '</Point>' '</Placemark>' '</Document>' '</kml>' )
def test_parse_kml_file(self): "Tests the parsing of a local KML file, with validation" test_datafile = path.join( path.dirname(__file__), 'testfiles', 'google_kml_developers_guide/complete_tour_example.kml' ) # parse with validation with open(test_datafile) as f: doc = parse(f, schema=Schema('kml22gx.xsd')) # parse without validation with open(test_datafile) as f: doc = parse(f) self.assertTrue(True)
def test_parse_invalid_ogc_kml_document(self): """Tests the parsing of an invalid KML document. Note that this KML document uses elements that are not in the OGC KML spec. """ url = 'https://developers.google.com/kml/documentation/kmlfiles/altitudemode_reference.kml' context = ssl._create_unverified_context() try: with urlopen(url, context=context) as fileobject: with self.assertRaises(etree.XMLSyntaxError): # tree = parse(fileobject, schema=Schema('ogckml22.xsd')) except URLError: print('Unable to access the URL. Skipping test...')
def ParseKml(text): output = [] with open(text) as f: object = parser.parse(f, {}).getroot() placemarks = getPlacemarks(object) if placemarks == None: match = re.search(r'[0-9]{4}-[0-9]{2}-[0-9]{2}', object.Document.name.text) group = match.group() data = convKmlDateOnly(group) return [{'begin': data, 'end': data, 'description': 'No data available for this day'}] for mark in range(len(placemarks)): metadata = tryParseMetadataFromDescription(placemarks[mark]) if (metadata): print("Found metadata in ${tryParseName(placemark)}:", metadata) retval = tryParsePoint(placemarks[mark], metadata) #funziona if (retval is not None): output.append(retval) continue retval = tryParseLineString(placemarks[mark], metadata) if (retval is not None): output.append(retval) continue return output
def parse(request): context = RequestContext(request) context_dict = {'graffitis': graffitis} if not graffitis: url = 'http://blueberrynfig.pythonanywhere.com/static/graffiki_app/graffiti.kml' fileobject = urllib.request.urlopen(url) doc = parser.parse(fileobject).getroot() coordinates = doc.findall( './/{http://www.opengis.net/kml/2.2}coordinates') for coordinate in coordinates: coordArray = str(coordinate).split(',') graffiti = Graffiti() graffiti.lon = float(coordArray[0]) graffiti.lat = float(coordArray[1]) graffiti.save() graffitis.append(graffiti) return render_to_response('graffiki_app/index.html', context_dict, context) return render_to_response('graffiki_app/index.html', context_dict, context)
def readOrigFolder(foldername): if not os.path.exists(foldername): return {} onlyfiles = [f for f in os.listdir(foldername) if os.path.isfile(os.path.join(foldername, f))] result = {} for f in onlyfiles: fullpath = os.path.join(foldername, f) with open(fullpath) as kmlfile: kmldoc = parser.parse(kmlfile) root = kmldoc.getroot() for x in root.Document.Folder.Placemark: # print etree.tostring(x, pretty_print=True) coords = [y for y in str(x.LineString.coordinates).rstrip().lstrip().split(" ")] coords_filtered = [] for y in coords: lat, lon, h = y.split(",") coords_filtered.append([float(lat), float(lon)]) #if "Path" in str(x.name) or "Line" in x.name or "," in str(x.name) or "." in str(x.name): # print x.name try: name = str(x.name).replace("Path", "").replace("Line", "").replace(",", "").replace(".", "").rstrip().lstrip() #if int(name) in result.keys(): # print "Error: key in orig already exists!", name, result[int(name)], coords result[int(name)] = coords_filtered except: print "##################", fullpath, x.name return result
def main(): indir, history = "", "" try: opts, args = getopt.getopt(sys.argv[1:], "i:l:v", ["images", "locations"]) except getopt.GetoptError: print("Exception") print _HELP sys.exit(2) for opt, arg in opts: if opt == "-h": print _HELP elif opt in ("-i", "--images"): indir = arg if not path.isdir(indir): print "Images location must be a directory" sys.exit(2) elif opt in ("-l", "--locations"): history = arg if not history.endswith(".kml"): print "Location history must be a kml file" sys.exit(2) elif not path.isfile(history): print "Location history file must exist" sys.exit(2) print "Tagging images in {} with {}".format(indir, history) with open(history) as his: tag(indir, parser.parse(his).getroot())
def get_ap_polygons(): fpath = opath.join(geo_dpath, 'terminalCoords.pkl') if not opath.exists(fpath): from pykml import parser terminal_coords = {} poly_names = ['T1', 'T2', 'T3', 'BudgetT'] ifpath = opath.join(geo_dpath, 'queues.kml') with open(ifpath) as f: kml_doc = parser.parse(f).getroot().Document assert kml_doc is not None for pm in kml_doc.Placemark: if pm.name in poly_names: str_coords = str( pm.Polygon.outerBoundaryIs.LinearRing.coordinates) points = [] for l in ''.join(str_coords.split()).split(',0')[:-1]: _lng, _lat = l.split(',') points.append([eval(_lng), eval(_lat)]) terminal_coords[pm. name if pm.name != 'BudgetT' else 'B'] = points with open(fpath, 'wb') as fp: pickle.dump(terminal_coords, fp) else: with open(fpath, 'rb') as fp: terminal_coords = pickle.load(fp) ap_polygons = [] for tn, points in terminal_coords.items(): ap_poly = poly(points) ap_poly.name = tn ap_polygons.append(ap_poly) return ap_polygons
def poly_from_kml(self, kml_file): """Creates a MultiPOlygon from a kml file This is not a generic function and does not work with kml files of all formats. It only supports kml files as the default kml used by ServiceAuditor found in ../files/data Args: @kml_file: the kml file to be parsed """ xpath_poly = ".//{http://www.opengis.net/kml/2.2}Polygon" if not os.path.isfile(kml_file): raise SystemExit("No such file") with open(kml_file) as kmlf: doc = kparser.parse(kmlf).getroot() multi = [] for polygon in doc.Document.findall(xpath_poly): boundary = [] boundstr = polygon.outerBoundaryIs.LinearRing.coordinates # get outer boundaries of polygon for poly in str(boundstr).rstrip().split(' '): # transform with given projection coords = poly.split(',') # we ignore z coordinate # XXX this assumes that coords are in format [lon, lat]! coords = self.proj(float(coords[0]), float(coords[1])) # add to polygon boundary.append(coords) multi.append(Polygon(boundary)) return MultiPolygon(multi)
def get_radius(kml): """ Calculate viewcone radius using Wikidata depicts or trigonometry if not available """ with open(kml, "r") as f: KML = parser.parse(f).getroot() id = str(KML.PhotoOverlay.name) tilt = KML.PhotoOverlay.Camera.tilt df = pd.read_csv( os.environ["METADATA"], index_col="Source ID", ) depicts = df.loc[id, "Depicts"] if isinstance(depicts, str): depicts = depicts.split("||") distances = [] points = [] for depict in depicts: q = re.search("(?<=\/)Q\d+", depict).group(0) point = query_wikidata(q) if point: points.append(point[0]) else: continue for point in points: lnglat = re.search("\((-\d+\.\d+) (-\d+\.\d+)\)", point) lng = lnglat.group(1) lat = lnglat.group(2) depicted = reproject((float(lng), float(lat))) origin = reproject( ( KML.PhotoOverlay.Camera.longitude, KML.PhotoOverlay.Camera.latitude, ) ) distance = origin.distance(depicted) distances.append(distance) if distances: radius = max(distances) else: return None else: if tilt <= 89: tan = math.tan((tilt * math.pi) / 180) radius = KML.PhotoOverlay.Camera.altitude * tan if radius < 400: return None else: return None return radius
def draw_feature(kml, properties, radius=0.2): """ Use pre-obtained radius to draw circular sector (viewcone) for each image """ with open(kml, "r") as f: KML = parser.parse(f).getroot() camera = KML.PhotoOverlay.Camera viewvolume = KML.PhotoOverlay.ViewVolume point = Point(camera.longitude, camera.latitude) center = geojson.Feature(geometry=point) start_angle = camera.heading - viewvolume.rightFov end_angle = camera.heading - viewvolume.leftFov if start_angle > end_angle: start_angle = start_angle - 360 else: pass cone = sector( center, radius, start_angle, end_angle, options={"properties": properties, "steps": 200}, ) return cone
def getImagesFromGSV(kmlfile): svdirectory = "streetviewimages" if not os.path.exists(svdirectory): os.makedirs(svdirectory) family = os.path.basename(kmlfile).split('.')[0] fov=120 pitches = [5]#,-11 with open(kmlfile) as f: doc = parser.parse(f) root = doc.getroot() for placemark in root.Document.Placemark: pt = placemark.find(".//{http://earth.google.com/kml/2.1}Point") name = placemark.name if pt is not None: lon,lat,x = placemark.Point.coordinates.text.strip().split(',') for data in placemark.ExtendedData.findall(".//*[@name='headings']/{http://earth.google.com/kml/2.1}value"): headings = data.text.strip().split(",") h_count = 0 for h in headings: for pitch in pitches: imgname = str(family) + '_' + str(name) + '_' + str(h_count) + '.jpg' p = os.path.join(svdirectory,imgname) if not os.path.isfile(p): gsv_base_url = "http://maps.googleapis.com/maps/api/streetview?size=640x360&location="+str(lat)+","+str(lon) gsv_url = gsv_base_url + "&fov="+str(fov) + "&heading="+str(h) + "&pitch=" + str(pitch) +"&key=" + GSV_KEY urllib.urlretrieve(gsv_url, p) print "Downloaded", p else: print p, "Image already exist" h_count+=1
def get_districtPoly(): poly_fpath_PKL = opath.join(pf_dpath, 'DistrictsPolygon.pkl') if not opath.exists(poly_fpath_PKL): distPoly = {} kml_fpath = opath.join(ef_dpath, 'MP14_SUBZONE_WEB_PL.kml') with open(kml_fpath) as f: kml_doc = parser.parse(f).getroot().Document for pm in kml_doc.Folder.Placemark: str_coords = str(pm.MultiGeometry.Polygon.outerBoundaryIs. LinearRing.coordinates) poly_coords = [] for l in ''.join(str_coords.split()).split(',0')[:-1]: lng, lat = map(eval, l.split(',')) poly_coords.append([lat, lng]) district_name = str(pm.name).title() if "'S" in district_name: district_name = district_name.replace("'S", "'s") if "S'Pore" in district_name: district_name = district_name.replace("S'Pore", "S'pore") if district_name in xConsiderDist: continue distPoly[district_name] = poly_coords with open(poly_fpath_PKL, 'wb') as fp: pickle.dump(distPoly, fp) else: with open(poly_fpath_PKL, 'rb') as fp: distPoly = pickle.load(fp) # return distPoly
def file_to_objet(self): kml_file = self.file route_obj = {} points = [] with open(kml_file) as file: doc = parser.parse(file).getroot().Document route_obj["name"] = doc.name for pm in doc.iterchildren(): print pm.__dict__ if hasattr(pm, "LineString"): coord = str(pm.LineString.coordinates) route_obj["coordinates"] = coord.replace("\n", "").replace( " ", "|").replace(" ", "|").split("|")[1:-1] route_obj["colorLine"] = "" # route_obj["colorLine"] = pm.Style.LineStyle.color # route_obj["widthLine"] = pm.Style.LineStyle.width route_obj["widthLine"] = "" if hasattr(pm, "Point"): point = str(pm.Point.coordinates) point = point.replace("\n", "").replace( " ", "").replace(" ", "") points.append(point) route_obj["points"] = points return route_obj
def _load_waypoints(kml_stream): """Loads and returns the waypoints from a KML string.""" def get_child(element, tag_name): """Returns the child element with the given tag name.""" try: return getattr(element, tag_name) except AttributeError: raise ValueError('No {tag} element found'.format(tag=tag_name)) root = parser.parse(kml_stream).getroot() if 'kml' not in root.tag: raise ValueError('Not a KML file') document = get_child(root, 'Document') placemark = get_child(document, 'Placemark') line_string = get_child(placemark, 'LineString') # Unlike all of the other tag names, "coordinates" is not capitalized coordinates = get_child(line_string, 'coordinates') waypoints = [] text = coordinates.text.strip() for csv in re.split(r'\s', text): ( longitude, latitude, altitude # pylint: disable=unused-variable ) = csv.split(',') waypoints.append(( Telemetry.longitude_to_m_offset(float(longitude), float(latitude)), Telemetry.latitude_to_m_offset(float(latitude)) )) return waypoints
def kml2coords(kmlfile='sites/KSC_Antenna_Sites.kml'): """ Read Google Earth kml file and return the site information :param sitefile: Input kml file from Google Earth :return: antnames: names of the antennas, string array lons: longitudes of the antennnas in degrees, float array lats: latitudes of the antennnas in degrees, float array """ from pykml import parser with open(kmlfile) as f: doc = parser.parse(f) antnames = [] lons = [] lats = [] for e in doc.findall('.//{http://www.opengis.net/kml/2.2}Placemark'): try: name = e.name.name.text except: name = e.name.text try: coord = e.Point.coordinates.text.split(',') antnames.append(name) lons.append(float(coord[0])) lats.append(float(coord[1])) except: pass return lons, lats, antnames
def getcoordinatesfromKML(path_to_kml): ''' Parsing KML coordinates into long and lat Return the coordinates for longitude and latitude ''' with open(path_to_kml) as f: doc = parser.parse(f) root = doc.getroot() list_of_coor = root.Document.Placemark.Polygon.outerBoundaryIs.LinearRing.coordinates.text.replace('0 ','').split(',') # Remove Unwanted data for i in list_of_coor: if i == '0': print(i) list_of_coor.remove(i) lon_coor=[] lat_coor=[] for i in range(len(list_of_coor)): if i%2==0: print("long : ",list_of_coor[i]) lon_coor.append(float(list_of_coor[i])) else: print("lat : ",list_of_coor[i]) lat_coor.append(float(list_of_coor[i])) return lon_coor,lat_coor
def parse(file_path): kml_file = path.join(file_path) with open(kml_file) as f: document = parser.parse(f).getroot() return parse_doc(document)
def parse(src): kml_file = urllib.urlopen(build_url(os.path.join(os.environ['KML_DATA'], src), 'file')) doc = parser.parse(kml_file).getroot() JSONpolygon = [] try: for el in doc.Document.Folder.Placemark: polygon = el.Polygon.outerBoundaryIs.LinearRing data = el.ExtendedData.SchemaData[0].SimpleData props = {} for prop in data: props.update({prop.attrib['name']: prop.text}) # Clean coordinates data c_coords = [float(c.replace('0-','-')) for c in polygon.coordinates.text.strip().replace('\n',',').replace(' ','').split(',') if c != '0'] coords = [str(lat)+' '+str(lon) for lat,lon in zip(c_coords[0::2],c_coords[1::2])] JSONpolygon.append({ 'type': 'Feature', 'geometry': { 'type': 'Polygon', 'coordinates': coords }, 'properties': props }) except Exception: print(traceback.format_exc()) return JSONpolygon
def readStub(stubname): # read in the drifter KML stub, and return the root element # this is for testing. fid = open(stubname,'r') drifterKML = parse(fid).getroot() fid.close() return drifterKML
def parse(file_path, created_json_path): kml_file = path.join(file_path) with open(kml_file) as f: document = parser.parse(f).getroot() new_file = open(created_json_path, "w+") new_file.write(parse_doc(document))
def validate_kml(): """Validate a KML file Example: validate_kml test.kml """ from pykml.parser import parse from optparse import OptionParser parser = OptionParser( usage="usage: %prog FILENAME_or_URL", version="%prog 0.1", ) parser.add_option( "--schema", dest="schema_uri", help="URI of the XML Schema Document used for validation") (options, args) = parser.parse_args() if len(args) != 1: parser.error("wrong number of arguments") else: uri = args[0] try: # try to open as a file fileobject = open(uri) except IOError: try: import urllib2 fileobject = urllib2.urlopen(uri) except ImportError: from urllib.request import urlopen fileobject = urlopen(uri) except ValueError: raise ValueError('Unable to load URI {0}'.format(uri)) except: raise doc = parse(fileobject, schema=None) if options.schema_uri: schema = Schema(options.schema_uri) else: # by default, use the OGC base schema sys.stdout.write("Validating against the default schema: {0}\n".format( OGCKML_SCHEMA)) schema = Schema(OGCKML_SCHEMA) sys.stdout.write("Validating document...\n") if schema.validate(doc): sys.stdout.write("Congratulations! The file is valid.\n") else: sys.stdout.write("Uh-oh! The KML file is invalid.\n") sys.stdout.write(schema.assertValid(doc)) # close the fileobject, if needed try: fileobject except NameError: pass #variable was not defined else: fileobject.close
def checkKML(filename): with open(filename) as f: doc = parser.parse(f) if schema_ogc.validate(doc) or schema_gx.validate(doc): return True else: return False
def process(): shapes = ['shape_id', 'shape_pt_lat', 'shape_pt_lon', 'shape_pt_sequence'] # Open system.kml file in go/data/routes with open('{}/route/kml/shapes.kml'.format(DATA_PATH)) as file: doc = parser.parse(file) shape = None for t in doc.getiterator(): # If the tag is a description, find the shape_id in the CDATA if re.sub('\{.*\}', '', t.tag).lower() == 'description': shape = int(parse_cdata(t.text)) if shape in Segment.objects: shape = Shape(shape) if shape not in Shape.objects else Shape.objects[shape] else: print('Shape {} does not have a matching segment.'.format(shape)) shape = None # Save coordinates if re.sub('\{.*\}', '', t.tag).lower() == 'coordinates' and shape: # A new Path must be created for the discovered coordinates shape.add_path([re.split(',', x) for x in re.split('\s', t.text) if len(re.split(',', x)) == 3]) # Open writer writer = open('{}/gtfs/files/shapes.txt'.format(REPORT_PATH), 'w') writer.write('{}\n'.format(','.join(shapes))) for obj in sorted(Shape.objects.keys()): shape = Shape.objects[obj] shape.order_paths() for path, reverse in shape.order: for point in path.get_points(reverse): writer.write('{}\n'.format(','.join([str(s) for s in [shape.id, point[1], point[0], shape.index]]))) shape.index += 1
def read_mask_kml(mask_file): cwd = os.getcwd() + '/' mask_dir = cwd + 'masks/' ref_kml = mask_file doc = kml_parser.parse(ref_kml).getroot() print 'Parsing %s and creating .ref files with coordinate pairs' % mask_file for i in range(len(cats)): cat = doc.Document.Folder[0].Folder[i] cat_name = str(cat.name) if os.path.isfile(mask_dir + cat_name + '.ref'): os.remove(mask_dir + cat_name + '.ref') f = open(mask_dir + cat_name + '.ref', 'w') cur_mark = cat.Placemark[0] while cur_mark is not None: # Ugh, we have to format the stupid string print 'Creating coordinates from polygon called %s' % cur_mark.name coord_str = str( cur_mark.Polygon.outerBoundaryIs.LinearRing.coordinates) xv, yv = parse_coord_str(coord_str, altitude_coord=True) write_coord_pairs(xv, yv, f) cur_mark = cur_mark.getnext()
def parseKML(file): ''' 接收kml文件路径,解析kml文件,返回文件中的所有点。 全局变量points包含kml文件中的所有点,每个点由经度,纬度和海拔高度表示。精度为小数点后8位。 points = [[lon, lat, alt], [lon, lat, alt], ...] ''' points = [] try: with open(file, 'r') as f: kml = parser.parse(f).getroot() placemarks = kml.findall( './/{http://www.opengis.net/kml/2.2}Placemark') for each in placemarks: coords = str(each.LineString.coordinates) points_seg = coords.strip().split(' ') for each in points_seg: each = each.split(',') each = [float(each[0]), float(each[1]), float(each[2])] points.append(each) print('File <%s> has %d placemarksc and %d points.' % (file, len(placemarks), len(points))) return points except: tm.showinfo('提示', '请检查kml文件')
def buildCwbStaticStationDict(): url = 'http://www.cwb.gov.tw/wwwgis/kml/cwbobs.kml' #url = 'http://code.google.com/apis/kml/documentation/KML_Samples.kml' fileobject = urlopen(url) root = parser.parse(fileobject).getroot() cwbStaticStationDict={} for i in range(100): try: k=root.Document.Placemark[i] stationName=k.name.text[0:len(k.name.text)-9] stationNumber=k.name.text[-5:] longitude=float(k.LookAt.longitude.text) latitude=float(k.LookAt.latitude.text) altitude=float(k.LookAt.altitude.text) #print stationName,stationNumber,longitude,latitude,altitude cwbStaticStationDict[stationName.encode('utf-8')]=[stationNumber,longitude,latitude,altitude] except IndexError: pass #print i, 'index out of range' #check if dic is OK #print cwbStaticStationDict['金門'], len(cwbStaticStationDict) return(cwbStaticStationDict)
def read_kml(filename): from pykml import parser from pykml.factory import nsmap namespace = {"ns": nsmap[None]} with open(filename) as f: root = parser.parse(f).getroot() pms = root.findall(".//ns:Placemark", namespaces=namespace) lst = root.findall(".//ns:LinearRing", namespaces=namespace) polygons = [] for pm in pms: poly = pm.name polygons.append(poly) coord_all = [] for ls in lst: coord = ls.coordinates coord_all.append(coord) coord_all_df = pd.DataFrame(coord_all) polygons_df = pd.DataFrame(polygons) data_kml = pd.merge(polygons_df, coord_all_df, left_index=True, right_index=True) data_kml.columns = ['PName', 'geometry'] print(data_kml.head()) print("Step 2 complete!") return data_kml
def parseXML(fileName): """ 功能:解析kml文件。提取kml文件中所有placemark中的点。当文件路径不正确时,程序退出。\n 输入:kml文件的路径\n 输出:kml文件中的所有点的经纬度 """ path = os.path.dirname(fileName) kmlFileName = path + '/1.kml' placemarksDic = [] # each element is a dictionaty with open(kmlFileName, 'r') as f: kml = parser.parse(f).getroot() placemarks = kml.findall( './/{http://www.opengis.net/kml/2.2}Placemark') print('File <%s> has %d placemarks.' % (kmlFileName, len(placemarks))) for each in placemarks: placemark = { 'name': each.name, 'styleUrl': each.styleUrl, 'coor': each.Point.coordinates } placemarksDic.append(placemark) return placemarksDic
def parseKML(kml_path, nrows, ncols, epsg_code=4326): """ reads the xmin,xmax,ymin,and ymax from reefmaster kml inputs: kml_path: filepath to the kml epsg_code (optional): in case the data is not in wgs84 lat lon outputs: geotransform: gdal geotransform object proj: gdal projection object """ with open(kml_path) as f: tree = parser.parse(f) root = tree.getroot() ymax = float(root.Document.GroundOverlay.LatLonBox.north.text) ymin = float(root.Document.GroundOverlay.LatLonBox.south.text) xmax = float(root.Document.GroundOverlay.LatLonBox.east.text) xmin = float(root.Document.GroundOverlay.LatLonBox.west.text) xres = (xmax - xmin) / float(ncols) yres = (ymax - ymin) / float(nrows) geotransform = (xmin, xres, 0, ymax, 0, -yres) proj = osr.SpatialReference() # Establish its coordinate encoding proj.ImportFromEPSG(epsg_code) # This one specifies WGS84 lat long. return geotransform, proj
def load_single_file(path): """ Load a single kml file. """ with open(path) as f: folder = parser.parse(f).getroot().Document.Folder output = [] for ap_id, pm in enumerate(folder.Placemark): device_type = str(pm.description).split('\n')[5] if device_type.split(' ')[1] == 'WIFI': output.append({ 'type': 'Feature', 'geometry': { 'type': 'Point', 'coordinates': (float(str(pm.Point.coordinates).split(',')[0]), float(str(pm.Point.coordinates).split(',')[1])) }, 'properties': { 'ap_id': ap_id, 'name': str(pm.name), 'network_id': str(pm.description).split('\n')[0], 'encryption': str(pm.description).split('\n')[1], 'time': str(pm.description).split('\n')[2], 'signal': str(pm.description).split('\n')[3], 'accuracy': str(pm.description).split('\n')[4], 'type': str(device_type), }, }) return output
def get_boundaries(): """ Parse the KML file to get the boundaries """ with open(kml_file) as f: tree = parser.parse(f) root = tree.getroot() N = 0 placemarks = {} for ch in root.Document.Folder.getchildren(): if 'Placemark' in ch.tag: # Found a placemark N += 1 pname = int(ch.name.text) # Get the coordinates pcoords = ch.MultiGeometry.Polygon.outerBoundaryIs.LinearRing.coordinates.text pcoords = pcoords.strip() pcoords = re.split(',| ', pcoords) pcoords = [float(c) for c in pcoords] lons = pcoords[0::3] lats = pcoords[1::3] assert len(lons) == len(lats) #print "Polygon has %d vertices" % len(lons) placemarks[pname] = (lats, lons) print "Found %d placemarks" % N return placemarks
def parse(self): available = 0 traveling = 0 live = self.vcub() with open(current_dir + '/doc.kml') as f: doc = parser.parse(f) placemarks = [] for placemark in doc.getroot().Document.Folder.Placemark: coordinates = str(placemark.Point.coordinates).split(',') coordinates = [coordinates[1], coordinates[0]] data = {} for child in placemark.ExtendedData.SchemaData.SimpleData: data[child.get('name')] = child.text try: station = live[data['NOM']] for key in station.keys(): data[key] = station[key] if data['etat'] == 'DECONNECTEE': data['class'] = 'offline' else: if int(data['nbvelos']) == 0: data['class'] = 'velos_red' if int(data['nbplaces']) == 0: data['class'] += ' places_red' if int(data['nbplaces']) <= 3: data['class'] += ' places_orange' if int(data['nbplaces']) > 3: data['class'] += ' places_green' elif int(data['nbvelos']) <= 3: data['class'] = 'velos_orange' if int(data['nbplaces']) == 0: data['class'] += ' places_red' if int(data['nbplaces']) <= 3: data['class'] += ' places_orange' if int(data['nbplaces']) > 3: data['class'] += ' places_green' elif int(data['nbvelos']) > 3: data['class'] = 'velos_green' if int(data['nbplaces']) == 0: data['class'] += ' places_red' if int(data['nbplaces']) <= 3: data['class'] += ' places_orange' if int(data['nbplaces']) > 3: data['class'] += ' places_green' except: data['class'] = 'offline' try: available += int(data['nbvelos']) traveling += int(data['NBSUPPOR']) - int(data['nbplaces']) except: pass placemarks.append({'coordinates': coordinates, 'data': data}) self.data_vcub = { 'available': available, 'traveling': traveling, 'stations': sorted(placemarks, key=lambda k: k['data']['NOM']), 'updated': placemarks[0]['data']['heure'] } return self.data_vcub
def get_districtPopPoly(): csv_fpath = opath.join(pf_dpath, 'DistrictsPopulation.csv') pop_fpath_PKL = opath.join(pf_dpath, 'DistrictsPopulation.pkl') poly_fpath_PKL = opath.join(pf_dpath, 'DistrictsPolygon.pkl') if not opath.exists(csv_fpath): with open(csv_fpath, 'w') as w_csvfile: writer = csv.writer(w_csvfile, lineterminator='\n') new_headers = ['Name', 'Population'] writer.writerow(new_headers) # distPop = {} xls_fpath = opath.join(ef_dpath, 'ResidentPopulation2015.xls') book = open_workbook(xls_fpath) sh = book.sheet_by_name('T7(Total)') for i in range(sh.nrows): district_name = sh.cell(i, 2).value if district_name in ['Subzone', 'Total', '']: continue population = int(sh.cell( i, 3).value) if sh.cell(i, 3).value != '-' else 0 if district_name in xConsiderDist: continue distPop[district_name] = population # distPoly = {} kml_fpath = opath.join(ef_dpath, 'MP14_SUBZONE_WEB_PL.kml') with open(kml_fpath) as f: kml_doc = parser.parse(f).getroot().Document for pm in kml_doc.Folder.Placemark: str_coords = str(pm.MultiGeometry.Polygon.outerBoundaryIs. LinearRing.coordinates) poly_coords = [] for l in ''.join(str_coords.split()).split(',0')[:-1]: lng, lat = map(eval, l.split(',')) poly_coords.append([lat, lng]) district_name = str(pm.name).title() if "'S" in district_name: district_name = district_name.replace("'S", "'s") if "S'Pore" in district_name: district_name = district_name.replace("S'Pore", "S'pore") if district_name in xConsiderDist: continue assert district_name in distPop distPoly[district_name] = poly_coords with open(csv_fpath, 'a') as w_csvfile: writer = csv.writer(w_csvfile, lineterminator='\n') writer.writerow([district_name, distPop[district_name]]) # with open(pop_fpath_PKL, 'wb') as fp: pickle.dump(distPop, fp) with open(poly_fpath_PKL, 'wb') as fp: pickle.dump(distPoly, fp) else: with open(pop_fpath_PKL, 'rb') as fp: distPop = pickle.load(fp) with open(poly_fpath_PKL, 'rb') as fp: distPoly = pickle.load(fp) # return distPop, distPoly
def extract_tracks(f): from pykml import parser doc = parser.parse(f) root = doc.getroot() tracks = find_tracks(root.Document.Placemark) return tracks
def extractKML( ridingID, path ) : kmlFileName = path + 'ridingMapData_' + str( ridingID ) + '.kml' with open(kmlFileName, 'r') as f: doc = parser.parse(f) print "Opening riding map file:", ridingID poll_list = [] pol = {} firstPoll = True for element in doc.iter("{http://www.opengis.net/kml/2.2}ExtendedData", "{http://www.opengis.net/kml/2.2}Polygon" ): if element.tag == "{http://www.opengis.net/kml/2.2}ExtendedData": if firstPoll == False: poll_list.append( pol.copy()) firstPoll = False pol = {} parseData( pol, element ) elif element.tag == "{http://www.opengis.net/kml/2.2}Polygon": # or element.tag == "{http://www.opengis.net/kml/2.2}MultiGeometry": #pprint( pol ) parsePolygon( pol, element ) poll_list.append( pol.copy()) f.close() riding = {} riding['ridingID'] = ridingID riding['num_polls'] = len(poll_list) riding['polls'] = poll_list outputFile = path + 'ridingMapData_' + str( ridingID ) + '.json' with open(outputFile, 'w') as f: json.dump(riding, f) f.close() print "Successfully wrote riding map file:", ridingID outputFile = path + 'ridingMapBoundaryData_' + str( ridingID ) + '.json' boundary = {} boundary['ridingID'] = ridingID boundary['coords'] = calculateRidingBoundary( poll_list ) with open(outputFile, 'w') as f: json.dump(boundary, f) f.close() print "Successfully wrote riding map boundary file:", ridingID return True
def get_station_objects(start_year=1980, end_year=2010, sel_names=None): # read ice depth values df = get_obs_data() lon_min, lon_max = -100, 0 lat_min, lat_max = 40, 90 nvals_min = 100 p = parser.parse(STATION_COORDS_FILE.open()) root = p.getroot() station_elts = root.Document.Placemark # select points based on the lat/lon limits? stations = [] for el in station_elts: lon, lat, _ = [float(c.strip()) for c in el.Point.coordinates.text.split(",")] # Check if the station if sel_names is not None: is_ok = False for sel_name in sel_names: if sel_name.lower() in el.name.text.lower(): is_ok = True break if not is_ok: continue if (lon_min <= lon <= lon_max) and (lat_min <= lat <= lat_max): print("{}: {}".format(el.name, el.Point.coordinates)) df_s = df.loc[df.station_name.str.lower().str.startswith(el.name.text.lower())] df_s = df_s.loc[(df_s.year >= start_year) & (df_s.year <= end_year)] if len(df_s) < nvals_min: continue print(len(df_s)) d_to_v = dict(zip(df_s["Date"][:], df_s["ice_depth"][:])) # df_s.plot(x="Date", y="ice_depth") # plt.title(el.name.text) # plt.show() # print(df_s.station_name) stations.append(Station(st_id=df_s.station_name.iloc[0], lon=lon, lat=lat, date_to_value=d_to_v)) return stations
def kml2shapely(kmlfile): with open(kmlfile) as f: doc = parser.parse(f) root = doc.getroot() coords = root.Document.Placemark.Polygon.outerBoundaryIs.LinearRing.coordinates str1 = (str(coords)).split(' ')[0] allcoords = re.findall('-?\d+\.*\d*', str(coords)) coordarray = np.array(allcoords, dtype=float).reshape(len(allcoords)/3, 3)[:,:2] ring = GeoPolygon(LONG_LAT, *coordarray.transpose()) return ring
def load_kml_file(filename, schema_type = "kml22gx.xsd"): with open(filename) as fh: schema_gx = Schema(schema_type) doc = parser.parse(fh) if (schema_gx.validate(doc)): return doc.getroot() else: print "Invalid kml format for file: "+str(filename) return None
def test_parse_invalid_kml_document(self): "Tests the parsing of an invalid KML document" fileobject = StringIO('<bad_element />') try: tree = parse(fileobject, schema=Schema("ogckml22.xsd")) self.assertTrue(False) except etree.XMLSyntaxError: self.assertTrue(True) except: self.assertTrue(False)
def test_write_python_script_for_kml_document_with_comments(self): """Tests the creation of an OGC KML document with several comments""" import os import tempfile from pykml.parser import parse from pykml.factory import write_python_script_for_kml_document test_datafile = path.join( path.dirname(__file__), 'testfiles', 'simple_file_with_comments.kml' ) schema = Schema('kml22gx.xsd') with open(test_datafile) as f: doc = parse(f, schema=schema) script = write_python_script_for_kml_document(doc) # create a temporary python file handle, tfile = tempfile.mkstemp(suffix='.py') #print tfile # Useful for debugging with open(tfile, 'w') as f: f.write(script) # execute the temporary python file to create a KML file import subprocess current_env = os.environ.copy() current_env["PYTHONPATH"] = os.path.abspath(os.path.join(os.getcwd(),'..')) handle, temp_kml_file = tempfile.mkstemp(suffix='.kml') #print temp_kml_file # Useful for debugging with open(temp_kml_file, 'w') as f: exit_code = subprocess.call(["python",tfile], stdout=f, env=current_env) self.assertEqual(exit_code, 0) # parse and validate the KML generated by the temporary script doc2 = parse(temp_kml_file, schema=schema) # test that the root element is as expected self.assertEqual(doc2.docinfo.root_name, 'kml') self.assertEqual(etree.tostring(doc), etree.tostring(doc2)) #import ipdb; ipdb.set_trace() pass
def parse(self): available = 0 traveling = 0 live = self.vcub() with open(current_dir + '/doc.kml') as f: doc = parser.parse(f) placemarks = [] for placemark in doc.getroot().Document.Folder.Placemark: coordinates = str(placemark.Point.coordinates).split(',') coordinates = [coordinates[1], coordinates[0]] data = {} for child in placemark.ExtendedData.SchemaData.SimpleData: data[child.get('name')] = child.text try: station = live[data['NOM']] for key in station.keys(): data[key] = station[key] if data['etat'] == 'DECONNECTEE': data['class'] = 'offline' else: if int(data['nbvelos']) == 0: data['class'] = 'velos_red' if int(data['nbplaces']) == 0: data['class'] += ' places_red' if int(data['nbplaces']) <= 3: data['class'] += ' places_orange' if int(data['nbplaces']) > 3: data['class'] += ' places_green' elif int(data['nbvelos']) <= 3: data['class'] = 'velos_orange' if int(data['nbplaces']) == 0: data['class'] += ' places_red' if int(data['nbplaces']) <= 3: data['class'] += ' places_orange' if int(data['nbplaces']) > 3: data['class'] += ' places_green' elif int(data['nbvelos']) > 3: data['class'] = 'velos_green' if int(data['nbplaces']) == 0: data['class'] += ' places_red' if int(data['nbplaces']) <= 3: data['class'] += ' places_orange' if int(data['nbplaces']) > 3: data['class'] += ' places_green' except: data['class'] = 'offline' try: available += int(data['nbvelos']) traveling += int(data['NBSUPPOR']) - int(data['nbplaces']) except: pass placemarks.append({'coordinates': coordinates, 'data': data}) self.data_vcub = {'available': available, 'traveling': traveling, 'stations': sorted(placemarks, key=lambda k: k['data']['NOM']), 'updated': placemarks[0]['data']['heure']} return self.data_vcub
def test_write_python_script_for_kml_document_with_namespaces(self): """Tests the creation of an OGC KML document with several namespaces""" import os import tempfile from pykml.parser import parse from pykml.factory import write_python_script_for_kml_document test_datafile = path.join( path.dirname(__file__), 'testfiles', 'google_kml_developers_guide/complete_tour_example.kml' ) schema = Schema('kml22gx.xsd') with open(test_datafile) as f: doc = parse(f, schema=schema) script = write_python_script_for_kml_document(doc) # create a temporary python file handle, tfile = tempfile.mkstemp(suffix='.py') #print tfile #uncomment to print the temporary filename with open(tfile, 'w') as f: f.write(script) # execute the temporary python file to create a KML file # set the PYTHONPATH variable so that it references the root # of the pyKML library import subprocess current_env = os.environ.copy() current_env["PYTHONPATH"] = os.path.abspath( os.path.join(path.dirname(__file__),'../..') ) handle, temp_kml_file = tempfile.mkstemp(suffix='.kml') #print temp_kml_file with open(temp_kml_file, 'w') as f: exit_code = subprocess.call(["python",tfile], stdout=f, env=current_env) self.assertEqual(exit_code, 0) # parse and validate the KML generated by the temporary script doc2 = parse(temp_kml_file, schema=schema) # test that the root element is as expected self.assertEqual(doc2.docinfo.root_name, 'kml')
def test_parse_kml_url_2(self): "Tests the parsing of a KML URL" url = 'http://code.google.com/apis/kml/documentation/kmlfiles/animatedupdate_example.kml' fileobject = urllib2.urlopen(url) tree = parse(fileobject, schema=Schema('kml22gx.xsd')) self.assertEquals( etree.tostring(tree)[:137], '<kml xmlns="http://www.opengis.net/kml/2.2" ' 'xmlns:gx="http://www.google.com/kml/ext/2.2">' '<Document>' '<name>gx:AnimatedUpdate example</name>' )
def validate_kml(): """Validate a KML file Example: validate_kml test.kml """ from pykml.parser import parse from optparse import OptionParser parser = OptionParser( usage="usage: %prog FILENAME_or_URL", version="%prog 0.1", ) parser.add_option("--schema", dest="schema_uri", help="URI of the XML Schema Document used for validation") (options, args) = parser.parse_args() if len(args) != 1: parser.error("wrong number of arguments") else: uri = args[0] try: # try to open as a file fileobject = open(uri) except IOError: try: fileobject = urllib2.urlopen(uri) except ValueError: raise ValueError('Unable to load URI {0}'.format(uri)) except: raise doc = parse(fileobject, schema=None) if options.schema_uri: schema = Schema(options.schema_uri) else: # by default, use the OGC base schema sys.stdout.write("Validating against the default schema: {0}\n".format(OGCKML_SCHEMA)) schema = Schema(OGCKML_SCHEMA) sys.stdout.write("Validating document...\n") if schema.validate(doc): sys.stdout.write("Congratulations! The file is valid.\n") else: sys.stdout.write("Uh-oh! The KML file is invalid.\n") sys.stdout.write(schema.assertValid(doc)) # close the fileobject, if needed try: fileobject except NameError: pass #variable was not defined else: fileobject.close
def get_ns_polygon(): kml_doc = None with open(sg_loc_polygons_fpath) as f: kml_doc = parser.parse(f).getroot().Document for pm in kml_doc.Placemark: if pm.name == 'Night Safari': str_coords = str(pm.Polygon.outerBoundaryIs.LinearRing.coordinates) points = [] for l in ''.join(str_coords.split()).split(',0')[:-1]: _long, _lat = l.split(',') points.append([eval(_long), eval(_lat)]) return poly(points)
def __init__(self): print "KML Parser initialized" # retrieve the kmz file from data vancouver url kmzData = urllib.urlretrieve(self.url, "data.kmz") # unzip the file kmz = ZipFile(kmzData[0], 'r') # open the kml file in the archive kml = kmz.open('bikeways.kml', 'r') # get real data in kml file self.content = parser.parse(kml).getroot() # all placemarks in kml file self.placemarks = self.content.Document.Folder.Placemark
def test_parse_invalid_ogc_kml_document(self): """Tests the parsing of an invalid KML document. Note that this KML document uses elements that are not in the OGC KML spec. """ url = 'http://code.google.com/apis/kml/documentation/kmlfiles/altitudemode_reference.kml' fileobject = urllib2.urlopen(url) try: tree = parse(fileobject, schema=Schema("ogckml22.xsd")) self.assertTrue(False) except etree.XMLSyntaxError: self.assertTrue(True) except: self.assertTrue(False)
def test_parse_kml_url(self): "Tests the parsing of a KML URL" url = 'http://code.google.com/apis/kml/documentation/kmlfiles/altitudemode_reference.kml' fileobject = urllib2.urlopen(url) tree = parse(fileobject, schema=Schema('kml22gx.xsd')) self.assertEquals( etree.tostring(tree)[:185], '<kml xmlns="http://www.opengis.net/kml/2.2" ' 'xmlns:gx="http://www.google.com/kml/ext/2.2">' '<!-- required when using gx-prefixed elements -->' '<Placemark>' '<name>gx:altitudeMode Example</name>' )
def add_column_kml(full_data_csv_name, new_kml_name, feature_name, lower=False): # Open both csv files. main_csv = open(full_data_csv_name, 'r') output_csv = open('temp.csv', 'w') # Make sure this feature has not already been added. header = main_csv.readline().split("|") header = [s.strip() for s in header] if feature_name in header: print "That feature has already been added." return # Append header. header.append(feature_name) output_csv.write('|'.join(header)+'\n') # Count number of entries below cutoff. for line in main_csv.readlines(): # Skip empty lines. if len(line) < 5: continue values = line.split('|') values = [val.strip() for val in values] if len(values[main_lat_col]) < 2: wrong_count += 1 continue house_lat = float(values[main_lat_col]) house_long = float(values[main_long_col]) new_feature_kml = parser.parse(open(new_kml_name, 'r')) # Count how many building in the given csv are within a given cutoff of the current house. count = 0 if lower: for building in new_feature_kml.getroot().document.placemark: building_values = str(building.point.coordinates).split(",") building_lat = float(building_values[1]) building_long = float(building_values[0]) distance = math.sqrt((house_lat - building_lat)**2 + (house_long-building_long)**2) if distance < distance_cutoff: count += 1 else: for building in new_feature_kml.getroot().Document.Placemark: building_values = str(building.Point.coordinates).split(",") building_lat = float(building_values[1]) building_long = float(building_values[0]) distance = math.sqrt((house_lat - building_lat)**2 + (house_long-building_long)**2) if distance < distance_cutoff: count += 1 values.append(str(count)) output_csv.write('|'.join(values) + '\n') output_csv.close() main_csv.close() os.remove(full_data_csv_name) os.rename('temp.csv', full_data_csv_name)
def main(argv=None): if argv is None: argv = sys.argv try: try: opts, args = getopt.getopt(argv[1:], "h", ["help"]) except getopt.error, msg: raise Usage(msg) # main code schema = Schema("kml22gx.xsd") filename = argv[1] with open(filename) as f: kmldoc = parse(f, schema=schema) print write_python_script_for_kml_document(kmldoc)
def test_parse_kml_url(self): "Tests the parsing of a KML URL" url = 'http://code.google.com/apis/kml/documentation/KML_Samples.kml' #url = 'http://kml-samples.googlecode.com/svn/trunk/kml/Document/doc-with-id.kml' #url = 'http://code.google.com/apis/kml/documentation/kmlfiles/altitudemode_reference.kml' #url = 'http://code.google.com/apis/kml/documentation/kmlfiles/animatedupdate_example.kml' fileobject = urllib2.urlopen(url) tree = parse(fileobject, schema=Schema("ogckml22.xsd")) self.assertEquals( etree.tostring(tree)[:78], '<kml xmlns="http://www.opengis.net/kml/2.2">' '<Document>' '<name>KML Samples</name>' )