def process_feature(self, feature): print "Processing feature ", feature.name if isinstance(feature, kml.Placemark): # Search marker code s = re.split('-(\d+)', feature.name) if len(s) < 2: print "Placemark name is not correct. Discard" return route = s[0] order = s[1] station = ObservationStation() try: station.observation_route = ObservationRoute.objects.get(code=route) except ObjectDoesNotExist: print "Route code", route, " does no exist. Discard" return station.order = int(order) station.name = feature.name station.position = Point(feature.geometry.x, feature.geometry.y) station.save() if getattr(feature, 'features', None): for f in feature.features(): self.process_feature(f)
def process_beach_station(self, beach_el): print "Processing beach element", beach_el['NOM_PLATJA'],\ " - ", beach_el['ELEMENT'] route_code = "P-" + beach_el['CD_PLATJA'] # search route try: route = ObservationRoute.objects.get( code__iexact=route_code) except ObjectDoesNotExist: route = ObservationRoute() route.route_type = 'B' route.code = route_code route.name = beach_el['NOM_PLATJA'] route.island = beach_el['NOM_ILLA'] route.municipality = beach_el['NOM_MUNI'] route.save() # Check if station exists: if ObservationStation.objects.filter(name=beach_el['ELEMENT']).exists(): return station = ObservationStation() station.observation_route = route # extract order from element (T-15001-01, digits after second -) s = re.split('-(\d+)', beach_el['ELEMENT']) if len(s) < 4: print "Beach element name is not as expected. Leave order 0" station.order = 0 else: station.order = int(s[3]) station.name = beach_el['ELEMENT'] station.station_type = beach_el['TIPUS_PUNT'] if station.station_type == 'N': station.order = station.order + 10 elif station.station_type == 'O': station.order = station.order + 20 x = float(beach_el['X'].replace(',', '.')) y = float(beach_el['Y'].replace(',', '.')) pnt = Point(x, y) pnt.set_srid(int(beach_el['SRID'])) if pnt.srid != 4326: pnt.transform(4326) station.position = pnt station.save()