def create_markers(places): counter = Counter() # TODO - change to regex blacklist = [u'מס.',u"מס'",u'א' ,u'ב' ,u'ג' ,u'ד',u"א'",u"ב'",u"ג'",u"ד'",u"ה'",u"רח'",u"רח"] for place in places: data = json.loads(place.data) address = re.sub('([\d]+)([\S]*)','\g<1>',data['address']) clean_address = ' '.join([x for x in address.split() if x not in blacklist]) geo_result = geo_code(clean_address, data['city']) city_result = geo_code(data['city'], data['city']) city_location = "" if city_result["status"] == "OK": city_location = city_result["results"][0]["geometry"]["location"] if geo_result["status"] != "OK": counter[geo_result["status"]] +=1 print "geo_result status of place id " + str(place.id) + "was " + geo_result["status"] continue for l in geo_result["results"]: location = l["geometry"]["location"] if location == city_location: counter["Failed"] += 1 continue marker = Placemark() marker.place = place marker.city = data['city'] marker.address = clean_address marker.lat = location["lat"] marker.lng = location["lng"] payload = {"size": "240x240", "location": clean_address + " " + data['city'], "sensor": "false"} print payload r = requests.get("http://maps.googleapis.com/maps/api/streetview", params=payload) print r.url input_file = StringIO(r.content) output_file = StringIO() img = Image.open(input_file) if img.mode != "RGB": img = img.convert("RGB") img.save(output_file, "JPEG") try: marker.save() counter["Newly added"] += 1 marker.image.save(str(marker.id) + ".jpg", ContentFile(output_file.getvalue()), save=True) except IntegrityError: counter["Already exist"] += 1 return counter
def handle(self, *args, **options): if len(args) <= 0: raise CommandError('please specify file name') if not os.path.exists(args[0]): raise CommandError("file %s doesn't exist" % args[0]) with open(args[0], 'r') as f: result = json.load(f) counter = Counter() try: for i in result: try: place = Place.objects.get(vendor_id=i['id']) except Place.DoesNotExist: print "%s place id doesn't exist" % i['id'] counter["badid"] += 1 continue counter["Valid locations"] += 1 geo_result = geo_code(i['address'], i['city']) if geo_result["status"] != "OK": counter[geo_result["status"]] += 1 continue print "results for station #%s" % (i['id']) for l in geo_result["results"]: marker = Placemark() marker.place = place marker.city = i['city'] marker.address = i['address'] location = l["geometry"]["location"] marker.lat = location["lat"] marker.lng = location["lng"] try: marker.save() counter["Newly added"] += 1 print "\t %f ,%f saved successfully" % (marker.lat, marker.lng) except IntegrityError: counter["Already exist"] += 1 print "\t %f ,%f record is not unique" % (marker.lat, marker.lng) continue print "import markers completed" finally: for k, v in counter.items(): print k, v
def handle(self, *args, **options): if len(args) <= 0: raise CommandError('please specify file name') if not os.path.exists(args[0]): raise CommandError("file %s doesn't exist" % args[0]) with open(args[0], 'r') as f: result = json.load(f) counter = Counter() try: for i in result: try: place = Place.objects.get(vendor_id=i['id']) except Place.DoesNotExist: print "%s place id doesn't exist" % i['id'] counter["badid"] += 1 continue counter["Valid locations"] += 1 geo_result = geo_code(i['address'], i['city']) if geo_result["status"] != "OK": counter[geo_result["status"]] +=1 continue print "results for station #%s" % (i['id']) for l in geo_result["results"]: marker = Placemark() marker.place = place marker.city = i['city'] marker.address = i['address'] location = l["geometry"]["location"] marker.lat = location["lat"] marker.lng = location["lng"] try: marker.save() counter["Newly added"] += 1 print "\t %f ,%f saved successfully" % (marker.lat, marker.lng) except IntegrityError: counter["Already exist"] += 1 print "\t %f ,%f record is not unique" % (marker.lat, marker.lng) continue print "import markers completed" finally: for k, v in counter.items(): print k, v