def wdpapoint2mpa(): wpoints = WdpaPoint.objects.all().order_by("wdpaid") count = 0 for wpoint in wpoints: try: mpa, created = Mpa.objects.get_or_create(wdpa_id=wpoint.wdpaid) except: mpa = Mpa() # create a new object if wpoint has no wdpaid print "wdpaid error", wpoint.wdpaid, wpoint.pk count += 1 print count mpa.is_point = True mpa.name = wpoint.name mpa.wdpa_id = wpoint.wdpaid mpa.country = wpoint.country mpa.sub_location = wpoint.sub_loc mpa.designation = wpoint.desig mpa.designation_eng = wpoint.desig_eng mpa.designation_type = wpoint.desig_type mpa.iucn_category = wpoint.iucn_cat mpa.int_criteria = wpoint.int_crit mpa.marine = wpoint.marine mpa.status = wpoint.status mpa.status_year = wpoint.status_yr mpa.rep_m_area = wpoint.rep_m_area mpa.rep_area = wpoint.rep_area mpa.gov_type = wpoint.gov_type mpa.mgmt_auth = wpoint.mang_auth mpa.mgmt_plan_ref = wpoint.mang_plan # mpa.point_geom_smerc = wpoint.point_geom_smerc # mpa.point_geom = wpoint.point_geom # mpa.point_geog = wpoint.point_geog mpa.save() # SQL update all geometry rows, much faster than through django print "UPDATE point geometry columns" cursor = connection.cursor() cursor.execute("UPDATE mpa_mpa SET point_geog = w.geog FROM wdpa_wdpapoint as w WHERE mpa_mpa.wdpa_id = w.wdpaid") transaction.commit_unless_managed() print "UPDATE complete" Mpa.set_all_geom_from_geog()