Example #1
0
    def handle(self, *args, **options):
        from sigeo.obcine.models import Obcina
        import csv
        from django.contrib.gis.geos import GEOSGeometry
        from sigeo.preprocessing import get_coordtransform
        import StringIO

        s = StringIO.StringIO()
        w = csv.writer(s)
        trans = get_coordtransform()
        w.writerow(['id', 'ime', 'uime', 'tip', 'povrsina', 'center', 'geometrija'])

        for ob in Obcina.objects.all():
            center_pt = 'SRID=3787;POINT(%d %d)' % (ob.y_c, ob.x_c)
            pt = GEOSGeometry(center_pt)
            pt.transform(trans)

            row = [
                ob.ob_id,
                ob.ob_ime,
                ob.ob_uime,
                ob.ob_tip,
                ob.ob_pov,
                pt.kml,
                ob.the_geom.kml,
            ]
            w.writerow([unicode(i).encode('utf-8') for i in row])

        print s.getvalue()
Example #2
0
 def handle(self, *args, **options):
     from django.db import connection
     from sigeo.obcine.models import Obcina
     from sigeo.preprocessing import get_coordtransform
     import subprocess
     
     drop = bool(options.get('drop'))
     table_name = Obcina._meta.db_table
     
     dump_file = args[0]
     shp2pgsql = '/usr/bin/shp2pgsql'
     cmd = [shp2pgsql, '-g', 'the_geom', '-s', '3787', '-W', 'WINDOWS-1250', dump_file, table_name]
     p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
     
     sql = p.stdout.read()
     p.wait()
     
     cur = connection.cursor()
     
     print 'Loading...'
     if drop:
         cur.execute('DROP TABLE %s' % table_name)
         connection.connection.commit()
     cur.execute(sql)
     
     # For some strange reason a new connection needs to be made.
     connection.connection.commit()
     connection.connection.close()
     connection.connection = None
     
     print 'Preprocessing...'
     
     cur = connection.cursor()
     
     trans = get_coordtransform()
     cur.execute('''ALTER TABLE %s DROP CONSTRAINT enforce_srid_the_geom;''' % table_name)
     for ob in Obcina.objects.all():
         ob.the_geom.transform(trans)
         ob.save()
     cur.execute('''ALTER TABLE %s ADD CONSTRAINT enforce_srid_the_geom CHECK (st_srid(the_geom) = 4326);''' % table_name)
     cur.execute('''UPDATE geometry_columns SET srid=4326 WHERE f_table_name='%s' AND f_geometry_column = 'the_geom';''' % table_name)
     
     # check now is everything is ok
     from django.contrib.gis.geos import GEOSGeometry
     c = Obcina.objects.get(ob_id=1)
     assert c.ob_ime.upper() == u'AJDOVŠČINA'
     expected = GEOSGeometry('POINT (13.9075089930651767 45.8993739353968309)')
     assert abs(c.the_geom.centroid.x - expected.x) < 0.0000001, (c.ob_ime.upper(), c.the_geom.centroid.x - expected.x)
     assert abs(c.the_geom.centroid.y - expected.y) < 0.0000001, (c.ob_ime.upper(), c.the_geom.centroid.y - expected.y)
     z = Obcina.objects.get(ob_id=23)
     assert z.ob_ime.upper() == u'DOMŽALE'
     expected = GEOSGeometry('POINT (14.6268742810941141 46.1450992072178892)')
     assert abs(z.the_geom.centroid.x - expected.x) < 0.0000001, (z.ob_ime.upper(), z.the_geom.centroid.x - expected.x)
     assert abs(z.the_geom.centroid.y - expected.y) < 0.0000001, (z.ob_ime.upper(), z.the_geom.centroid.y - expected.y)
     
     print 'Done.'
Example #3
0
	def handle(self, *args, **options):
		from django.db import connection, transaction
		from sigeo.ue.models import UpravnaEnota
		from sigeo.preprocessing import get_coordtransform
		import subprocess
		
		with transaction.commit_on_success():
			drop = bool(options.get('drop'))
			table_name = UpravnaEnota._meta.db_table
			
			dump_file = args[0]
			shp2pgsql = '/usr/bin/shp2pgsql'
			cmd = [shp2pgsql, '-s', '3787', '-g', 'the_geom', '-W', 'WINDOWS-1250', dump_file, table_name]
			print ' '.join(cmd)
			p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
			
			sql = p.stdout.read()
			p.wait()
			
			cur = connection.cursor()
			
			print 'Loading...'
			if drop:
				cur.execute('DROP TABLE %s' % table_name)
				connection.connection.commit()
			cur.execute(sql)
			
			# For some strange reason a new connection needs to be made.
			connection.connection.commit()
			connection.connection.close()
			connection.connection = None
			
			print 'Preprocessing...'
			
			cur = connection.cursor()
			
			trans = get_coordtransform()
			cur.execute('''ALTER TABLE %s DROP CONSTRAINT enforce_srid_the_geom;''' % table_name)
			for ob in UpravnaEnota.objects.all():
				ob.the_geom.transform(trans)
				ob.save()
			cur.execute('''ALTER TABLE %s ADD CONSTRAINT enforce_srid_the_geom CHECK (st_srid(the_geom) = 4326);''' % table_name)
			cur.execute('''UPDATE geometry_columns SET srid=4326 WHERE f_table_name='%s' AND f_geometry_column = 'the_geom';''' % table_name)
			
			# check now is everything is ok
			from django.contrib.gis.geos import GEOSGeometry
			cs = UpravnaEnota.objects.get(ue_id=1)
			assert cs.ue_ime.upper() == u'AJDOVŠČINA'
			assert round(cs.the_geom.centroid.x, 7) == 13.9309896
			assert round(cs.the_geom.centroid.y, 7) == 45.8757997
			z = UpravnaEnota.objects.get(ue_id=6)
			assert z.ue_ime.upper() == u'DOMŽALE'
			assert round(z.the_geom.centroid.x, 7) == 14.6950667
			assert round(z.the_geom.centroid.y, 7) == 46.1539159
			
			print 'Done.'
Example #4
0
	def handle(self, *args, **options):
		from django.db import connection
		from sigeo.kataster.models import KatastrskaObcina
		from sigeo.preprocessing import get_coordtransform
		import subprocess
		
		drop = bool(options.get('drop'))
		table_name = KatastrskaObcina._meta.db_table
		
		dump_file = args[0]
		shp2pgsql = '/usr/bin/shp2pgsql'
		cmd = [shp2pgsql, '-s', '3787', '-W', 'CP852', dump_file, table_name]
		p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
		
		sql = p.stdout.read()
		p.wait()
		
		cur = connection.cursor()
		
		print 'Loading...'
		if drop:
			cur.execute('DROP TABLE %s' % table_name)
			connection.connection.commit()
		cur.execute(sql)
		
		# For some strange reason a new connection needs to be made.
		connection.connection.commit()
		connection.connection.close()
		connection.connection = None
		
		print 'Preprocessing...'
		
		cur = connection.cursor()
		
		trans = get_coordtransform()
		cur.execute('''ALTER TABLE %s DROP CONSTRAINT enforce_srid_the_geom;''' % table_name)
		for ob in KatastrskaObcina.objects.all():
			ob.the_geom.transform(trans)
			ob.save()
		cur.execute('''ALTER TABLE %s ADD CONSTRAINT enforce_srid_the_geom CHECK (st_srid(the_geom) = 4326);''' % table_name)
		cur.execute('''UPDATE geometry_columns SET srid=4326 WHERE f_table_name='%s' AND f_geometry_column='the_geom';''' % table_name)
		
		# check it's ok
		from django.contrib.gis.geos import GEOSGeometry
		c = KatastrskaObcina.objects.get(sifko=6)
		assert c.imeko.upper() == u'ČEPINCI'
		assert c.the_geom.centroid == GEOSGeometry('POINT (16.2078106090320908 46.8565645279100167)')
		s = KatastrskaObcina.objects.get(sifko=1)
		assert s.imeko.upper() == u'HODOŠ'
		assert s.the_geom.centroid == GEOSGeometry('POINT (16.3215871980173901 46.8371080639927513)')
		z = KatastrskaObcina.objects.get(sifko=11)
		assert z.imeko.upper() == u'ŽENAVLJE'
		assert z.the_geom.centroid == GEOSGeometry('POINT (16.1710307426937661 46.8380629177534189)')
		
		print 'Done.'
Example #5
0
	def handle(self, *args, **options):
		from django.db import connection, transaction
		from sigeo.regije.models import Regija
		from sigeo.preprocessing import get_coordtransform
		import subprocess
		
		with transaction.commit_on_success():
			drop = bool(options.get('drop'))
			table_name = Regija._meta.db_table
			
			dump_file = args[0]
			shp2pgsql = '/usr/bin/shp2pgsql'
			cmd = [shp2pgsql, '-s', '3787', '-g', 'the_geom', '-W', 'WINDOWS-1250', dump_file, table_name]
			print ' '.join(cmd)
			p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
			
			sql = p.stdout.read()
			p.wait()
			
			cur = connection.cursor()
			
			print 'Loading...'
			if drop:
				cur.execute('DROP TABLE %s' % table_name)
				connection.connection.commit()
			cur.execute(sql)
			
			# For some strange reason a new connection needs to be made.
			connection.connection.commit()
			connection.connection.close()
			connection.connection = None
			
			print 'Preprocessing...'
			
			cur = connection.cursor()
			
			trans = get_coordtransform()
			cur.execute('''ALTER TABLE %s DROP CONSTRAINT enforce_srid_the_geom;''' % table_name)
			for ob in Regija.objects.all():
				ob.the_geom.transform(trans)
				ob.save()
			cur.execute('''ALTER TABLE %s ADD CONSTRAINT enforce_srid_geom CHECK (st_srid(the_geom) = 4326);''' % table_name)
			cur.execute('''UPDATE geometry_columns SET srid=4326 WHERE f_table_name='%s' AND f_geometry_column = 'the_geom';''' % table_name)
			
			# check now is everything is ok
			from django.contrib.gis.geos import GEOSGeometry
			cs = Regija.objects.get(id='03')
			
			assert cs.ime.upper() == u'KOROŠKA'
			
			print 'Done.'