예제 #1
0
파일: csv_.py 프로젝트: imclab/ocgis
 def _write_(self):
     gid_file = OrderedDict()
     build = True
     is_aggregated = self.ops.aggregate
     with open(self.path,'w') as f:
         ocgis_lh(msg='opened csv file: {0}'.format(self.path),level=logging.DEBUG,
                  logger='conv.csv+')
         writer = csv.writer(f,dialect=OcgDialect)
         for coll in self:
             ocgis_lh('writing collection','conv.csv+',level=logging.DEBUG)
             if build:
                 ocgis_lh('starting build','conv.csv+',level=logging.DEBUG)
                 headers = coll.get_headers(upper=True)
                 if env.WRITE_TO_REFERENCE_PROJECTION:
                     projection = env.REFERENCE_PROJECTION
                 else:
                     projection = coll._archetype.spatial.projection
                 writer.writerow(headers)
                 build = False
                 ocgis_lh(msg='build finished'.format(self.path),level=logging.DEBUG,
                  logger='conv.csv+')
             for geom,row,geom_ids in coll.get_iter(with_geometry_ids=True):
                 if not is_aggregated:
                     ugid = geom_ids['ugid']
                     did = geom_ids['did']
                     gid = geom_ids['gid']
                     if ugid not in gid_file:
                         gid_file[ugid] = OrderedDict()
                     if did not in gid_file[ugid]:
                         gid_file[ugid][did] = OrderedDict()
                     gid_file[ugid][did][gid] = geom
                 writer.writerow(row)
             ocgis_lh('finished writing collection','conv.csv+',level=logging.DEBUG)
     
     if is_aggregated is True:
         ocgis_lh('creating a UGID-GID shapefile is not necessary for aggregated data. use UGID shapefile.',
                  'conv.csv+',
                  logging.WARN)
     else:
         ocgis_lh('writing UGID-GID shapefile','conv.csv+',logging.DEBUG)
         sc = ShpCabinet()
         shp_dir = os.path.join(self.outdir,'shp')
         try:
             os.mkdir(shp_dir)
         ## catch if the directory exists
         except OSError:
             if os.path.exists(shp_dir):
                 pass
             else:
                 raise
         shp_path = os.path.join(shp_dir,self.prefix+'_gid.shp')
         
         def iter_gid_file():
             for ugid,did_gid in gid_file.iteritems():
                 for did,gid_geom in did_gid.iteritems():
                     for gid,geom in gid_geom.iteritems():
                         yield({'geom':geom,'DID':did,
                                'UGID':ugid,'GID':gid})
         
         sc.write(iter_gid_file(),shp_path,sr=projection.sr)
예제 #2
0
 def write(self, path):
     geoms = []
     uid = self.spatial.uid
     for ii, geom in iter_array(self.spatial.geom, return_value=True):
         geoms.append({'geom': geom, 'ugid': uid[ii]})
     sc = ShpCabinet()
     sc.write(geoms, path, sr=self.spatial.projection.sr)
예제 #3
0
파일: geometry.py 프로젝트: imclab/ocgis
 def write(self,path):
     geoms = []
     uid = self.spatial.uid
     for ii,geom in iter_array(self.spatial.geom,return_value=True):
         geoms.append({'geom':geom,'ugid':uid[ii]})
     sc = ShpCabinet()
     sc.write(geoms,path,sr=self.spatial.projection.sr)
예제 #4
0
 def test_unwrap_pm(self):
     _pm = [-4.0,-10.0,-20.0,5.0]
     sc = ShpCabinet()
     for pm in _pm:
         geoms = sc.get_geoms('world_countries',unwrap=True,pm=pm)
         path = '/tmp/shp{0}.shp'.format(time.time())
         sc.write(geoms,path)
         for geom in geoms:
             bounds = geom['geom'].bounds
             self.assertTrue(bounds[0] >= pm)
예제 #5
0
 def write(self, path):
     geoms = []
     uid = self.spatial.uid
     attrs = self.spatial.attrs
     for ii, geom in iter_array(self.spatial.geom, return_value=True):
         dct = {'geom': geom, 'UGID': uid[ii]}
         for k, v in attrs.iteritems():
             dct[k] = v[ii]
         geoms.append(dct)
     sc = ShpCabinet()
     sc.write(geoms, path, sr=self.spatial.projection.sr)
예제 #6
0
파일: shp.py 프로젝트: imclab/ocgis
 def write(self,path):
     geoms = []
     uid = self.spatial.uid
     attrs = self.spatial.attrs
     for ii,geom in iter_array(self.spatial.geom,return_value=True):
         dct = {'geom':geom,'UGID':uid[ii]}
         for k,v in attrs.iteritems():
             dct[k] = v[ii]
         geoms.append(dct)
     sc = ShpCabinet()
     sc.write(geoms,path,sr=self.spatial.projection.sr)
예제 #7
0
파일: views.py 프로젝트: aashish24/ocgis
def get_shp(request, key=None):
    query = helpers.parse_qs(request.META['QUERY_STRING'])

    select_ugid = SelectUgid()
    select_ugid.parse_query(query)

    prefix = Prefix()
    prefix.parse_query(query)

    unwrap = Unwrap()
    unwrap.parse_query(query)

    pm = PrimeMeridian()
    pm.parse_query(query)

    sc = ShpCabinet()
    geom_dict = sc.get_geom_dict(key, attr_filter=select_ugid.value)

    ## unwrap coordinates if requested
    if unwrap.value:
        unwrap_geoms(geom_dict, pm.value)

    dir_path = get_temp_path(nest=True, only_dir=True, wd=env.DIR_OUTPUT)
    if prefix.value is None:
        out_name = key
    else:
        out_name = prefix.value
    filename = '{0}.shp'.format(out_name)
    path = os.path.join(dir_path, filename)
    path = sc.write(geom_dict, path)
    path = os.path.split(path)[0]

    resp = helpers._zip_response_(path,
                                  filename=filename.replace('shp', 'zip'))
    return (resp)
예제 #8
0
파일: views.py 프로젝트: aashish24/ocgis
def get_shp(request,key=None):
    query = helpers.parse_qs(request.META['QUERY_STRING'])
    
    select_ugid = SelectUgid()
    select_ugid.parse_query(query)
    
    prefix = Prefix()
    prefix.parse_query(query)
    
    unwrap = Unwrap()
    unwrap.parse_query(query)
    
    pm = PrimeMeridian()
    pm.parse_query(query)
    
    sc = ShpCabinet()
    geom_dict = sc.get_geom_dict(key,attr_filter=select_ugid.value)
    
    ## unwrap coordinates if requested
    if unwrap.value:
        unwrap_geoms(geom_dict,pm.value)
    
    dir_path = get_temp_path(nest=True,only_dir=True,wd=env.DIR_OUTPUT)
    if prefix.value is None:
        out_name = key
    else:
        out_name = prefix.value
    filename = '{0}.shp'.format(out_name)
    path = os.path.join(dir_path,filename)
    path = sc.write(geom_dict,path)
    path = os.path.split(path)[0]
    
    resp = helpers._zip_response_(path,filename=filename.replace('shp','zip'))
    return(resp)
예제 #9
0
파일: csv_.py 프로젝트: aashish24/ocgis
 def _write_(self):
     gid_file = []
     build = True
     with open(self.path,'w') as f:
         writer = csv.writer(f,dialect=OcgDialect)
         for coll in self:
             if build:
                 headers = coll.get_headers(upper=True)
                 if env.WRITE_TO_REFERENCE_PROJECTION:
                     projection = constants.reference_projection
                 else:
                     projection = coll._archetype.spatial.projection
                 ugid_idx = headers.index('UGID')
                 gid_idx = headers.index('GID')
                 did_idx = headers.index('DID')
                 writer.writerow(headers)
                 build = False
             for geom,row in coll.get_iter():
                 gid_file.append({'geom':geom,'did':row[did_idx],
                                  'ugid':row[ugid_idx],'gid':row[gid_idx]})
                 writer.writerow(row)
     
     if self.ops.aggregate is True and self.ops.abstraction == 'point':
         if env.VERBOSE:
             print('creating a UGID-GID shapefile is not necessary for aggregated point data. use UGID shapefile.')
     else:
         sc = ShpCabinet()
         shp_dir = os.path.join(self.outdir,'shp')
         try:
             os.mkdir(shp_dir)
         ## catch if the directory exists
         except OSError:
             if os.path.exists(shp_dir):
                 pass
             else:
                 raise
         shp_path = os.path.join(shp_dir,self.prefix+'_gid.shp')
         sc.write(gid_file,shp_path,sr=projection.sr)
예제 #10
0
    def _write_(self):
        gid_file = OrderedDict()
        build = True
        is_aggregated = self.ops.aggregate
        with open(self.path, 'w') as f:
            ocgis_lh(msg='opened csv file: {0}'.format(self.path),
                     level=logging.DEBUG,
                     logger='conv.csv+')
            writer = csv.writer(f, dialect=OcgDialect)
            for coll in self:
                ocgis_lh('writing collection',
                         'conv.csv+',
                         level=logging.DEBUG)
                if build:
                    ocgis_lh('starting build',
                             'conv.csv+',
                             level=logging.DEBUG)
                    headers = coll.get_headers(upper=True)
                    if env.WRITE_TO_REFERENCE_PROJECTION:
                        projection = env.REFERENCE_PROJECTION
                    else:
                        projection = coll._archetype.spatial.projection
                    writer.writerow(headers)
                    build = False
                    ocgis_lh(msg='build finished'.format(self.path),
                             level=logging.DEBUG,
                             logger='conv.csv+')
                for geom, row, geom_ids in coll.get_iter(
                        with_geometry_ids=True):
                    if not is_aggregated:
                        ugid = geom_ids['ugid']
                        did = geom_ids['did']
                        gid = geom_ids['gid']
                        if ugid not in gid_file:
                            gid_file[ugid] = OrderedDict()
                        if did not in gid_file[ugid]:
                            gid_file[ugid][did] = OrderedDict()
                        gid_file[ugid][did][gid] = geom
                    writer.writerow(row)
                ocgis_lh('finished writing collection',
                         'conv.csv+',
                         level=logging.DEBUG)

        if is_aggregated is True:
            ocgis_lh(
                'creating a UGID-GID shapefile is not necessary for aggregated data. use UGID shapefile.',
                'conv.csv+', logging.WARN)
        else:
            ocgis_lh('writing UGID-GID shapefile', 'conv.csv+', logging.DEBUG)
            sc = ShpCabinet()
            shp_dir = os.path.join(self.outdir, 'shp')
            try:
                os.mkdir(shp_dir)
            ## catch if the directory exists
            except OSError:
                if os.path.exists(shp_dir):
                    pass
                else:
                    raise
            shp_path = os.path.join(shp_dir, self.prefix + '_gid.shp')

            def iter_gid_file():
                for ugid, did_gid in gid_file.iteritems():
                    for did, gid_geom in did_gid.iteritems():
                        for gid, geom in gid_geom.iteritems():
                            yield ({
                                'geom': geom,
                                'DID': did,
                                'UGID': ugid,
                                'GID': gid
                            })

            sc.write(iter_gid_file(), shp_path, sr=projection.sr)