Exemplo n.º 1
0
from django.utils import timezone
from app.models import Project
from django.contrib.auth.models import User

with open('scripts/all.kml') as f:
    doc = parser.parse(f)
    
root = doc.getroot()

nProjects = 0

for placemark in root.Document.Folder.Placemark:
    
    p = Project()
    p.owner = User.objects.get(username = "******")
    p.name = placemark.name.text
    coordinates = placemark.Point.coordinates.text.split(',')
    p.longitude = coordinates[0]
    p.latitude = coordinates[1]
    p.description = re.search("description: (.*)", placemark.description.text).group(1)
    hundred_years = datetime.timedelta(days=36500)
    p.pub_date = datetime.datetime.now() - hundred_years
    p.is_imported = True
    p.save()
        
    nProjects += 1

print "Imported " + str(nProjects) + " projects."

    
Exemplo n.º 2
0
def add(request):
    
    print "Adding project"
    
    form = ImageUploadForm(request.POST, request.FILES)
    
    print form
    
    if form.is_valid():
        
        print "Form valid"
        
        p = Project()
        p.owner = request.user
        p.name = form.cleaned_data['name']
        p.architect = form.cleaned_data['architect']        
        
        try:
            p.address = form.cleaned_data['address']
            p.latitude = form.cleaned_data['latitude']
            p.longitude = form.cleaned_data['longitude']
        except KeyError:
            pass
        
        p.pub_date = timezone.now()
        p.image_file = form.cleaned_data['image']
        
        print form.cleaned_data
        
        p.save()
        
        image_file = request.FILES['image']
        
        image_str = ''
        for c in image_file.chunks():
            image_str += c
        image_file_strio = StringIO(image_str)
        image = PImage.open(image_file_strio)
        if image.mode != "RGB":
            image = image.convert('RGB')

        rotation_code = get_rotation_code(image)
        image = rotate_image(image, rotation_code)    
        
        width, height = image.size
        
        wh_ratio = float(width) / float(height)
                
        if width <= 400 and height <= 300:
            cropped_image = image
        else:
            if wh_ratio > 4.0/3.0:
                if height > 300:
                    ratio = 300.0 / float(height)
                    image.thumbnail((int(ceil(width * ratio)), 300), PImage.ANTIALIAS)
                    width, height = image.size
            else:
                if width > 400:
                    ratio = 400.0 / float(width)
                    image.thumbnail((400, int(ceil(height * ratio))), PImage.ANTIALIAS)
                    width, height = image.size
                    
            left = max(int(width/2)-200, 0)
            bottom = max(int(height/2)-150, 0)
            right = min(int(width/2)+200, width)
            top = min(int(height/2)+150, height)
            
            
            cropped_image = image.crop([max(int(width/2)-200, 0), max(int(height/2)-150, 0), min(int(width/2)+200, width), min(int(height/2)+150, height)])
                
                            
        
        
        filename, ext = os.path.splitext(p.image_file.name)
        thumb_filename = settings.MEDIA_ROOT + filename + "-thumb" + ext
        #cropped_image.save(thumb_filename, "JPEG")
        
        f = StringIO()
        try:
            cropped_image.save(f, format='jpeg')
            s = f.getvalue()
            print type(p.thumbnail_file)
            p.thumbnail_file.save(thumb_filename, ContentFile(s))
            p.save()
        finally:
            f.close()    
            
        print "Project added"
        
        return HttpResponse(p.id)
    
    
    return HttpResponse('')
nProjects = 0

for stmt in results.subjects(URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), URIRef("http://dbpedia.org/ontology/Building")):
    
    if nProjects%500 == 0:
        print nProjects

    resource = Resource(results, stmt)


    p = Project()
    p.owner = User.objects.get(username = "******")
        
    p.name = resource.value(RDFS.label)
    p.longitude = resource.value(URIRef("http://www.w3.org/2003/01/geo/wgs84_pos#long"))
    p.latitude = resource.value(URIRef("http://www.w3.org/2003/01/geo/wgs84_pos#lat"))
    p.wikipedia_page_id = resource.value(URIRef("http://dbpedia.org/ontology/wikiPageID"))

    # Retrieve and concatenate all architects names

    architects = [architect.value(RDFS.label) for architect in resource[URIRef("http://dbpedia.org/ontology/architect")]]
    #.encode('ascii', 'ignore'))
    if architects != []:
        p.architect = ",".join(architects)
    else:
        architect = resource.value(URIRef("http://dbpedia.org/property/architect"))
        if architect:
            if type(architect) == Resource:
                p.architect = architect.identifier.encode('ascii', 'ignore')
            else:
Exemplo n.º 4
0
 results = sparql.query().convert()
 
 for result in results['results']['bindings']:
     
     if nProjects%500 == 0 and nProjects > 0:
         print nProjects
         
     wikipedia_page_id = result['wiki_page_id']['value']
     
     if not Project.objects.filter(wikipedia_page_id=wikipedia_page_id).exists():
         
         p = Project()
         
         p.owner = User.objects.get(username = "******")
         p.name = result[ 'stripped_structure_name']['value']
         p.longitude = result['long']['value']
         p.latitude = result['lat']['value']
         p.wikipedia_page_id = wikipedia_page_id
         p.architect = ''
         if result.has_key('architect_prop'):
             p.architect = result['architect_prop']['value']
         if result.has_key('stripped_architect_name'):
             if p.architect != '':
                 p.architect += ', ' + result['stripped_architect_name']['value']
             else:
                 p.architect = result['stripped_architect_name']['value']
         if result.has_key('thumbnail'):
             thumb_url = result['thumbnail']['value']
             i = thumb_url.rindex('?')
             p.wikipedia_image_url = thumb_url[:i]