예제 #1
0
def excel2py(filename):
    book = xlrd.open_workbook(filename)  #open our xls file,
    sheet = book.sheets()[0]
    r = sheet.row(0)
    r = sheet.row_values(0)

    data = []
    #
    for i in xrange(sheet.nrows):
        #
        if i == 0:
            k = sheet.row_values(i)
        else:
            combo = zip(k, sheet.row_values(i))
            combo = dict(combo)
            data.append(combo)
            #data.append(sheet.row_values(i)) #drop all the values in the rows into data
            #print len(sheet.row_values(i))

    #now load our queryset-like object into the GR
    counter = 0
    txfailed = 0
    for i in data:
        #format the geometry_coordinates
        print "Uploading record #%s" % (counter)
        if i.has_key('name'):
            i['name'] = "%s" % i['name']
            i['name'] = slugify.slugify(i['name'])
            print i['name']
        if i.has_key('geometry_coordinates'):
            splitgeo = i['geometry_coordinates'].split(" ")
            i['geometry_coordinates'] = "[ %s, %s ]" % (splitgeo[0],
                                                        splitgeo[1])
        elif i.has_key('lat') and i.has_key('lon'):
            i['geometry_coordinates'] = "[ %s, %s ]" % (i['lat'], i['lon'])
            del i['lat']
            del i['lon']
        if not i.has_key('geometry_type'):
            i['geometry_type'] = "Point"

        if not i.has_key('subdivision_code'):
            i['subdivision_code'] = "XX"

        if not i.has_key('classifiers'):
            i['classifiers'] = classifiers

        x = create_feature_in_georegistry(i, GR_SERVER, GR_USER, GR_PASS)
        #print x
        x = json.loads(x)
        if x["status"] != "200":
            txfailed += 1
            print "Failed"
        counter += 1
    print "Done! %s records processed. %s Failures." % (counter, txfailed)
예제 #2
0
def excel2py(filename):
    book = xlrd.open_workbook(filename) #open our xls file,
    sheet=book.sheets()[0]
    r = sheet.row(0)
    r= sheet.row_values(0)
    
    data = []
    #
    for i in xrange(sheet.nrows):
        #
        if i==0:
            k=sheet.row_values(i)
        else:
            combo=zip(k, sheet.row_values(i))
            combo=dict(combo)
            data.append(combo)
            #data.append(sheet.row_values(i)) #drop all the values in the rows into data
            #print len(sheet.row_values(i))
            
            
    #now load our queryset-like object into the GR
    counter=0
    txfailed=0
    for i in data:
        #format the geometry_coordinates
        print "Uploading record #%s" % (counter)
        if i.has_key('name'):
            i['name']="%s" %i['name']
            i['name']=slugify.slugify(i['name'])
            print i['name']
        if i.has_key('geometry_coordinates'):
            splitgeo=i['geometry_coordinates'].split(" ")
            i['geometry_coordinates']="[ %s, %s ]" %  (splitgeo[0], splitgeo[1])
        elif i.has_key('lat') and i.has_key('lon'):
            i['geometry_coordinates']="[ %s, %s ]" %  (i['lat'], i['lon'])
            del i['lat']
            del i['lon']
        if not i.has_key('geometry_type'):
            i['geometry_type']="Point"
            
        if not i.has_key('subdivision_code'):
            i['subdivision_code']="XX"
        
        if not i.has_key('classifiers'):
            i['classifiers']=classifiers  
        
        x= create_feature_in_georegistry(i, GR_SERVER, GR_USER, GR_PASS)
        #print x
        x=json.loads(x)
        if x["status"]!="200":
          txfailed+=1
          print "Failed"
        counter+=1
    print "Done! %s records processed. %s Failures." % (counter, txfailed )
def geojson2georegistry(filename, extra_properties={}):
    txfailed=0
    txsuccess=0
    total=0
    FILE = open(filename,"r")
    # Write all the lines at once:
    gj=FILE.read()
    FILE.close()
    gj=json.loads(gj)
    features=gj['features']
    
    for i in features:
        if i.has_key('type'):
            del i['type']
        if i.has_key('properties'):
            if i['properties'].has_key('Id'):
                del i['properties']['Id']   
        
            for k,v in i['properties'].items():
                i[k]=v
            del i['properties']
        
        i['geometry_type']=i['geometry']['type']
        i['geometry_coordinates']=i['geometry']['coordinates']
        del i['geometry']
        if extra_properties:
            i.update(extra_properties)
        r=create_feature_in_georegistry(i, GR_SERVER,GR_USER, GR_PASS)
        total+=1
        print "Processing record # %s" % (total)
        try:
            r=json.loads(r)
        except:
            FILE = open("out.html","w")
            # Write all the lines at once:
            FILE.writelines(x)
            FILE.close()
            sys.exit(1)
        if r["status"]!="200":
            txfailed+=1
            print r
        else:
            txsuccess+=1
            
    print "Done!  %s records processed. %s Failures." % (txsuccess,txfailed )

    return None
예제 #4
0
def geojson2georegistry(filename, extra_properties={}):
    txfailed = 0
    txsuccess = 0
    total = 0
    FILE = open(filename, "r")
    # Write all the lines at once:
    gj = FILE.read()
    FILE.close()
    gj = json.loads(gj)
    features = gj['features']

    for i in features:
        if i.has_key('type'):
            del i['type']
        if i.has_key('properties'):
            if i['properties'].has_key('Id'):
                del i['properties']['Id']

            for k, v in i['properties'].items():
                i[k] = v
            del i['properties']

        i['geometry_type'] = i['geometry']['type']
        i['geometry_coordinates'] = i['geometry']['coordinates']
        del i['geometry']
        if extra_properties:
            i.update(extra_properties)
        r = create_feature_in_georegistry(i, GR_SERVER, GR_USER, GR_PASS)
        total += 1
        print "Processing record # %s" % (total)
        try:
            r = json.loads(r)
        except:
            FILE = open("out.html", "w")
            # Write all the lines at once:
            FILE.writelines(x)
            FILE.close()
            sys.exit(1)
        if r["status"] != "200":
            txfailed += 1
            print r
        else:
            txsuccess += 1

    print "Done!  %s records processed. %s Failures." % (txsuccess, txfailed)

    return None
        subdivision_code = sys.argv[7]

        # fetch the geometry from the file

        d = {
            "name": name,
            "classifiers": "place.locality",
            "country_code": country_code,
            "subdivision_code": subdivision_code,
        }

        # grab all the shapes in the file and retun a python list
        l = extractshapes4gr(filename, d)

        counter = 0
        txfailed = 0

        for i in l:
            x = create_feature_in_georegistry(i, grserver, gruser, grpass)
            # print x
            x = json.loads(x)
            if x["status"] != "200":
                txfailed += 1
                # print "Failed"
            counter += 1
        print "Done!  %s records processed. %s Failures." % (counter, txfailed)

    except:
        print "Error."
        print sys.exc_info()
        subdivision_code = sys.argv[7]

        #fetch the geometry from the file

        d = {
            'name': name,
            'classifiers': "place.locality",
            'country_code': country_code,
            'subdivision_code': subdivision_code,
        }

        #grab all the shapes in the file and retun a python list
        l = extractshapes4gr(filename, d)

        counter = 0
        txfailed = 0

        for i in l:
            x = create_feature_in_georegistry(i, grserver, gruser, grpass)
            #print x
            x = json.loads(x)
            if x["status"] != "200":
                txfailed += 1
                #print "Failed"
            counter += 1
        print "Done!  %s records processed. %s Failures." % (counter, txfailed)

    except:
        print "Error."
        print sys.exc_info()