def __init__(self, places, width, height): self.width = width self.height = height reporting = Reporting() reporting.progress_start("Creating grid %ix%i" % (width, height), len(places) * 2) left, right, top, bottom = 180, -180, -90, 90 for p in places: reporting.progress() left = min(left, p.lon) right = max(right, p.lon) top = max(top, p.lat) bottom = min(bottom, p.lat) reporting.output_msg("info", "Bounding box: (%r,%r,%r,%r)" % ( left, bottom, right, top)) self.left = left self.right = right self.top = top self.bottom = bottom self.lon_ratio = (right - left) * 1.01 / width self.lat_ratio = (top - bottom) * 1.01 / height reporting.output_msg("info", "lon_ratio: %r, lat_ratio: %r" % ( self.lon_ratio, self.lat_ratio)) self.cells = {} for x in range(0, width): for y in range(0, height): self.cells[(x,y)] = Cell(x,y) for place in places: reporting.progress() cell = self.get_cell(place) cell.add_place(place) reporting.progress_stop()
def load_osm_boundary(filename): """Loads a boundary relation from an OSM file. The file must also contain all the nodes and way used by the boundary. Only the first boundary is read.""" reporting = Reporting() elem_count = count_elements(filename, "node") elem_count += count_elements(filename, "way") elem_count += 1 reporting.progress_start(u"Ładuję %s" % (filename,), elem_count) nodes = [] ways = [] relation = None for event, elem in ElementTree.iterparse(filename): if event != 'end': continue if elem.tag == 'node': nodes.append(elem) elif elem.tag == 'way': ways.append(elem) elif elem.tag == 'relation' and not relation: relation = elem else: continue reporting.progress() reporting.progress_stop() if not relation: reproting.output_msg("errors", u"Nie znaleziono relacji") raise ValueError, "Relation not found" reporting.output_msg("stats", u"Załadowano relację, %i dróg i %i węzłów." % (len(ways), len(nodes))) boundary = OSM_Boundary(relation, ways, nodes) return boundary
def load_terc(): reporting = Reporting() row_count = count_elements("data/TERC.xml", "row") reporting.progress_start(u"Ładowanie data/TERC.xml", row_count) for event, elem in ElementTree.iterparse("data/TERC.xml"): if event == 'end' and elem.tag == 'row': load_terc_object(elem) reporting.progress() reporting.progress_stop() reporting.output_msg("stats", u"Załadowano %i województw, %i powiatów i %i gmin" % ( Wojewodztwo.count(), Powiat.count(), Gmina.count()))
def load_simc(): load_wmrodz() reporting = Reporting() row_count = count_elements("data/SIMC.xml", "row") reporting.progress_start(u"Ładowanie data/SIMC.xml", row_count) for event, elem in ElementTree.iterparse("data/SIMC.xml"): if event == 'end' and elem.tag == 'row': SIMC_Place.from_element(elem) reporting.progress() reporting.progress_stop() reporting.output_msg("stats", u"Załadowano %i miejscowości" % (SIMC_Place.count(),)) SIMC_Place.link_parents()
def load_osm(): reporting = Reporting() row_count = count_elements("data/data.osm", "node") reporting.progress_start(u"Ładuję data/data.osm", row_count) for event, elem in ElementTree.iterparse("data/data.osm"): if event == 'end' and elem.tag == 'node': osm_place = OSM_Place(elem) reporting.progress() reporting.progress_stop() reporting.output_msg("stats", u"Załadowano %i miejsc." u"Dopasowano %i województw, %i powiatów i %i gmin." % ( OSM_Place.count(), OSM_Place.woj_matched, OSM_Place.pow_matched, OSM_Place.gmi_matched))
def write_changes(updated_places, created_by): for filename in glob.glob("output/*.osc") + glob.glob("output/*.comment"): os.unlink(filename) reporting = Reporting() reporting.progress_start(u"Preparing osmChange files", len(updated_places)) woj_trees = {} for place in updated_places: woj_name = place.wojewodztwo.name if not woj_name in woj_trees: root = ElementTree.Element(u"osmChange", version = u"0.3", generator = created_by) tree = ElementTree.ElementTree(root) modify = ElementTree.Element(u"modify", version = u"0.3", generator = created_by) root.append(modify) woj_trees[woj_name] = tree else: root = woj_trees[woj_name].getroot() modify = root[0] node = ElementTree.Element(u"node", id = place.id, lon = str(place.lon), lat = str(place.lat), version = place.version, changeset = place.changeset) modify.append(node) for k, v in place.tags.items(): if k == 'teryt:updated_by': continue tag = ElementTree.Element(u"tag", k = k, v = v) node.append(tag) tag = ElementTree.Element(u"tag", k = u"teryt:updated_by", v = created_by) node.append(tag) reporting.progress() reporting.progress_stop() reporting = Reporting() reporting.progress_start(u"Writting osmChange files", len(woj_trees)) for woj_name, tree in woj_trees.items(): basename = os.path.join("output", woj_name.encode("utf-8")) tree.write(basename + ".osc", "utf-8") comment_file = codecs.open(basename + ".comment", "w", "utf-8") print >> comment_file, u"TERYT import, województwo %s, prepared by %s" % ( woj_name, created_by) comment_file.close() reporting.progress() reporting.progress_stop()
time.sleep(0.2) reporting.progress() time.sleep(0.2) reporting.output_msg("debug", u"Cośtam cośtam") time.sleep(0.2) reporting2 = Reporting() reporting.progress() time.sleep(0.2) reporting2.output_msg("debug", u"Cośtam cośtam") time.sleep(0.2) reporting2.output_msg("debug", u"Cośtam cośtam") reporting.progress() reporting.output_msg("debug", u"Cośtam cośtam") reporting.output_msg("debug", u"Cośtam cośtam") time.sleep(0.2) reporting.progress_stop() for level in range(0,4): channel = "test%i" % (level,) reporting.config_channel(channel, split_level = level) reporting.output_msg(channel, "loc1", LocatedObject(None, None, None)) reporting.output_msg(channel, "loc2", LocatedObject("1", None, None)) reporting.output_msg(channel, "loc3", LocatedObject("1", "1/1", None)) reporting.output_msg(channel, "loc4", LocatedObject("1", "1/2", None)) reporting.output_msg(channel, "loc5", LocatedObject("1", "1/1", "1/1/1")) reporting.output_msg(channel, "loc6", LocatedObject("1", "1/1", "1/1/2")) reporting.output_msg(channel, "loc7", LocatedObject("1", "1/2", "1/2/1")) reporting.output_msg(channel, "loc8", LocatedObject("2", None, None)) reporting.output_msg(channel, "loc9", LocatedObject("2", "2/1", None)) reporting.output_msg(channel, "loc10", LocatedObject("2", "2/2", None)) reporting.output_msg(channel, "loc11", LocatedObject("2", "2/1", "2/1/1"))
def match_names(pass_no, places_to_match, grid = None): reporting = Reporting() places_count = len(places_to_match) if grid: reporting.progress_start( u"Dopasowywanie nazw %i miejsc, przebieg %i, z siatką %s" % (places_count, pass_no, grid), places_count) else: reporting.progress_start( u"Dopasowywanie nazw %i miejsc, przebieg %i" % (places_count, pass_no), places_count) osm_matched = set() simc_matched = set() places = [ (str(p), p) for p in places_to_match ] for name, osm_place in places: reporting.progress() if osm_place.name is None: reporting.output_msg("errors", u"%r: brak nazwy" % (osm_place,), osm_place) continue # Find matching entry in SIMC try: matching_simc_places = SIMC_Place.by_name(osm_place.name) except KeyError: reporting.output_msg("not_found", u"%s: nie znaleziono w TERYT" % (osm_place,), osm_place) places_to_match.remove(osm_place) continue simc_places = [place for place in matching_simc_places if place.type == osm_place.normalized_type and place.osm_place is None] if not simc_places: types_found = [ place.type for place in matching_simc_places ] reporting.output_msg("bad_type", u"%s: nie znalezionow w TERYT" u" obiektu właściwego typu (%r, znaleziono: %r)" % ( osm_place, osm_place.type, types_found), osm_place) continue cell = None if grid: try: cell = grid.get_cell(osm_place) except KeyError: pass if cell: simc_places = [ p for p in simc_places if p.powiat in cell.powiaty ] if len(simc_places) > 1: simc_places = [ p for p in simc_places if p.gmina in cell.gminy ] if not simc_places: reporting.output_msg("not_found", u"%s: nie znaleziono w TERYT miejsca" u" pasującego do komórki %s" % (osm_place, cell), osm_place) continue if len(simc_places) > 1: if grid: reporting.output_msg("ambigous%i" % (pass_no,), u"%s z OSM pasuje do wielu obiektów" u" SIMC w komórce %s: %s" % (osm_place, cell, u", ".join([str(p) for p in simc_places])), osm_place) else: reporting.output_msg("ambigous%i" % (pass_no,), u"%s z OSM pasuje do wielu obiektów w SIMC: %s" % (osm_place, u", ".join([str(p) for p in simc_places])), osm_place) continue simc_place = simc_places[0] # now check if reverse assignment is not ambigous matching_osm_places = OSM_Place.by_name(simc_place.name) confl_osm_places = [] for place in matching_osm_places: if place is osm_place: continue if cell: try: g_cell = grid.get_cell(place) except KeyError: g_cell = None if g_cell is not cell: continue if place.gmina and place.gmina != simc_place.gmina: continue if place.powiat and place.powiat != simc_place.powiat: continue if place.wojewodztwo and place.wojewodztwo != simc_place.wojewodztwo: continue confl_osm_places.append(place) if confl_osm_places: reporting.output_msg("ambigous%i" % (pass_no,), u"%s z SIMC pasuje do wielu obiektów w OMS: %s" % (simc_place, ", ".join([str(p) for p in confl_osm_places])), osm_place) continue if simc_place.osm_place: reporting.output_msg("ambigous%i" % (pass_no,), u"%s z SIMC ma już przypisany obiekt OSM: %s" % ( simc_place, simc_place.osm_place), osm_place) # good match osm_place.assign_simc(simc_place) simc_place.assign_osm(osm_place) reporting.output_msg("match", u"%s w OSM to %s w SIMC" % (osm_place, simc_place), osm_place) osm_matched.add(osm_place) simc_matched.add(simc_place) places_to_match.remove(osm_place) reporting.progress_stop() reporting.output_msg("stats", u"Przebieg %i: znaleziono w SIMC %i z %i miejscowości OSM" % ( pass_no, len(osm_matched), places_count)) return osm_matched, simc_matched