Exemplo n.º 1
0
def save_obj(vals):
    imp, transf, comunidade, name, desc, folder, tipo, geom = vals
    geom = eval(geom)
    print type(geom)
    print 'saving: ', vals
    if ',' in comunidade:
        comunidade = comunidade.split(',')[0]
    if comunidade == 'morro-do-INPS':
        comunidade = 'morro-do-inps'
    elif comunidade == 'mineiros':
        comunidade = 'morro-dos-mineiros'
    elif comunidade == 'conjunto-habitacional-BNH':
        comunidade = 'conjunto-habitacional-bnh'


    if transf == 'sim':
        tipo = 'polys'
        x,y, z = map(float, geom[0].split(','))
        geom = []
        geom.append('%s,%s,%s' % (x + 0.0005, y + 0.0005, z))
        geom.append('%s,%s,%s' % (x + 0.0005, y - 0.0005, z))
        geom.append('%s,%s,%s' % (x - 0.0005, y - 0.0005, z))
        geom.append('%s,%s,%s' % (x - 0.0005, y + 0.0005, z))
        geom.append('%s,%s,%s' % (x + 0.0005, y + 0.0005, z))

    if tipo == 'polys':
        coords = ''
        for i, coord in enumerate(geom):
            x,y, z = coord.split(',')
            coords += '%s %s' % (y, x)
            if not i == len(geom) - 1:
                coords += ', '
        geo_ref = 'GEOMETRYCOLLECTION ( POLYGON (( %s )))' % coords
    elif tipo == 'point':
        x,y, z = geom[0].split(',')
        geo_ref = 'GEOMETRYCOLLECTION ( POINT ( %s %s))' % (y, x)


    if imp == 'R':
        r = Resource()
        r.name = name
        r.description = desc
        if comunidade:
            c = Community.objects.get(slug=comunidade)
            r.community = c
        r.geometry = geo_ref
        r.save()

    elif imp == 'N':
        n = Need()
        n.title = name
        n.description = desc
        if comunidade:
            c = Community.objects.get(slug=comunidade)
            n.community = c
        n.geometry = geo_ref
        # n.target_audiences.add(1)
        n.save()

    elif imp == 'O':
        if name:
            o = Organization()
            o.name = name
            o.description = desc
            o.save()
            if comunidade:
                c = Community.objects.get(slug=comunidade)
                o.community.add(c)

            b = OrganizationBranch()
            b.name = name + ' - sede'
            b.geometry = geo_ref
            b.organization = o
            b.save()
Exemplo n.º 2
0
def save_obj(vals):
    imp, transf, comunidade, name, desc, folder, tipo, geom = vals
    geom = eval(geom)
    print type(geom)
    print 'saving: ', vals
    if ',' in comunidade:
        comunidade = comunidade.split(',')[0]

    if transf == 'sim':
        tipo = 'polys'
        x, y, z = map(float, geom[0].split(','))
        geom = []
        geom.append('%s,%s,%s' % (x + 0.0005, y + 0.0005, z))
        geom.append('%s,%s,%s' % (x + 0.0005, y - 0.0005, z))
        geom.append('%s,%s,%s' % (x - 0.0005, y - 0.0005, z))
        geom.append('%s,%s,%s' % (x - 0.0005, y + 0.0005, z))
        geom.append('%s,%s,%s' % (x + 0.0005, y + 0.0005, z))

    if tipo == 'polys':
        coords = ''
        for i, coord in enumerate(geom):
            x, y, z = coord.split(',')
            coords += '%s %s' % (y, x)
            if not i == len(geom) - 1:
                coords += ', '
        geo_ref = 'GEOMETRYCOLLECTION ( POLYGON (( %s )))' % coords
    elif tipo == 'point':
        x, y, z = geom[0].split(',')
        geo_ref = 'GEOMETRYCOLLECTION ( POINT ( %s %s))' % (y, x)

    if imp == 'R':
        r = Resource()
        r.name = name
        r.description = desc
        if comunidade:
            c = Community.objects.get(slug=comunidade)
            r.community = c
        r.geometry = geo_ref
        r.save()

    elif imp == 'N':
        n = Need()
        n.title = name
        n.description = desc
        if comunidade:
            c = Community.objects.get(slug=comunidade)
            n.community = c
        n.geometry = geo_ref
        # n.target_audiences.add(1)
        n.save()

    elif imp == 'O':
        if name:
            o = Organization()
            o.name = name
            o.description = desc
            o.save()
            if comunidade:
                c = Community.objects.get(slug=comunidade)
                o.community.add(c)

            b = OrganizationBranch()
            b.name = name + ' - sede'
            b.geometry = geo_ref
            b.organization = o
            b.save()
Exemplo n.º 3
0
def save_obj(vals):
    imp, transf, comunidade, name, desc, folder, tipo, geom = vals
    geom = eval(geom)
    print 'saving: ', vals

    user = User.objects.get(username='******')
    now = datetime.now()

    # Transform geometry data type
    if transf == 'sim':
        tipo = 'polys'
        x, y, z = map(float, geom[0].split(','))
        geom = []
        geom.append('%s,%s,%s' % (x + 0.0003, y + 0.0003, z))
        geom.append('%s,%s,%s' % (x + 0.0003, y - 0.0003, z))
        geom.append('%s,%s,%s' % (x - 0.0003, y - 0.0003, z))
        geom.append('%s,%s,%s' % (x - 0.0003, y + 0.0003, z))
        geom.append('%s,%s,%s' % (x + 0.0003, y + 0.0003, z))

    # build geomtry info
    if tipo == 'polys':
        coords = ''
        for i, coord in enumerate(geom):
            x, y, z = coord.split(',')
            coords += '%s %s' % (y, x)
            if not i == len(geom) - 1:
                coords += ', '
        geo_ref = 'GEOMETRYCOLLECTION ( POLYGON (( %s )))' % coords
    elif tipo == 'point':
        x, y, z = geom[0].split(',')
        geo_ref = 'GEOMETRYCOLLECTION ( POINT ( %s %s))' % (y, x)

    # save Resource
    if imp == 'R':

        #fix comunidade
        tags = []
        if ',' in comunidade:
            split = [c.strip() for c in comunidade.split(',')]
            comunidade, tags = split[0], split[1:]

        r = Resource()
        r.name = name
        r.description = desc

        # comunity is the firt
        if comunidade:
            c = Community.objects.get(slug=comunidade)
            r.community = c

        r.creator = user
        r.creation_date = now
        r.geometry = geo_ref
        r.save()

        # load remaining comunities as tags
        for tag in tags:
            r.tags.add(tag)

    # save Need
    elif imp == 'N':

        #fix comunidade
        tags = []
        if ',' in comunidade:
            split = [c.strip() for c in comunidade.split(',')]
            comunidade, tags = split[0], split[1:]

        n = Need()
        n.title = name
        n.description = desc

        if comunidade:
            c = Community.objects.get(slug=comunidade)
            n.community = c

        n.creator = user
        n.creation_date = now
        n.geometry = geo_ref
        # n.target_audiences.add(1)
        n.save()

        # load remaining comunities as tags
        for tag in tags:
            n.tags.add(tag)

    # save organization
    elif imp == 'O':
        if name:
            o = Organization()
            o.name = name
            o.description = desc
            o.creation_date = now
            o.creator = user
            o.save()
            for comm in [c.strip() for c in comunidade.split(',')]:
                c = Community.objects.get(slug=comm)
                o.community.add(c)

            b = OrganizationBranch()
            b.name = name + ' - sede'
            b.geometry = geo_ref
            b.organization = o
            b.creator = user
            b.creation_date = now
            b.save()