def test_simplify(self): # Note second coordinate in relation to the first, simplify will remove it if the tolerance is large enough gcomplex = GEOSGeometry('SRID=4326;POLYGON ((-120.42 34.37, -120.422 34.372, -119.64 34.32, -119.63 34.12, -122.44 34.15, -120.42 34.37))') gsimple = GEOSGeometry('SRID=4326;POLYGON ((-120.42 34.37, -119.64 34.32, -119.63 34.12, -122.44 34.15, -120.42 34.37))') gsimplified_large = gcomplex.simplify(0.01) self.assertEqual(gsimple, gsimplified_large) gsimplified_small = gcomplex.simplify(0.000001) self.assertEqual(gcomplex, gsimplified_small)
def test_simplify(self): # Note second coordinate in relation to the first, simplify will remove it if the tolerance is large enough gcomplex = GEOSGeometry( 'SRID=4326;POLYGON ((-120.42 34.37, -120.422 34.372, -119.64 34.32, -119.63 34.12, -122.44 34.15, -120.42 34.37))' ) gsimple = GEOSGeometry( 'SRID=4326;POLYGON ((-120.42 34.37, -119.64 34.32, -119.63 34.12, -122.44 34.15, -120.42 34.37))' ) gsimplified_large = gcomplex.simplify(0.01) self.assertEqual(gsimple, gsimplified_large) gsimplified_small = gcomplex.simplify(0.000001) self.assertEqual(gcomplex, gsimplified_small)
def cargar_municipios(apps, schema_editor): Entidad = apps.get_model("covid_data", "Entidad") Municipio = apps.get_model("covid_data", "Municipio") fuente = DataSource(RUTA_MUNICIPIOS_SHP) capa = fuente[0] for municipio in capa: descripcion = municipio.get('NOMGEO') clave_municipio = municipio.get('CVE_MUN') clave_entidad = municipio.get('CVE_ENT') clave = municipio.get('CVEGEO') entidad = Entidad.objects.get(clave=clave_entidad) geometria = municipio.geom geometria = GEOSGeometry(geometria.wkt, srid=6372) geometria_web = geometria.transform(3857, clone=True) geometria.transform(4326) centroide = geometria.centroid centroide_web = geometria_web.centroid geometria_simplificada = geometria.simplify(tolerance=0.0, preserve_topology=True) geometria_web_simplificada = geometria_web.simplify( tolerance=0.0, preserve_topology=True) if geometria.geom_type == 'Polygon': geometria = MultiPolygon(geometria, srid=4326) geometria_web = MultiPolygon(geometria_web, srid=3857) geometria_simplificada = MultiPolygon(geometria_simplificada, srid=4326) geometria_web_simplificada = MultiPolygon( geometria_web_simplificada, srid=3857) municipio, creado = Municipio.objects.get_or_create( clave=clave, descripcion=descripcion, defaults=dict( clave_municipio=clave_municipio, entidad=entidad, geometria=geometria, geometria_web=geometria_web, centroide=centroide, centroide_web=centroide_web, geometria_simplificada=geometria_simplificada, geometria_web_simplificada=geometria_web_simplificada)) if creado: print(f'Municipio creado {municipio}')
def cargar_entidades(apps, schema_editor): Entidad = apps.get_model("covid_data", "Entidad") fuente = DataSource(RUTA_ENTIDADES_SHP) capa = fuente[0] for entidad in capa: descripcion = entidad.get('NOMGEO') clave = entidad.get('CVE_ENT') geometria = entidad.geom geometria = GEOSGeometry(geometria.wkt, srid=6372) geometria_web = geometria.transform(3857, clone=True) geometria.transform(4326) centroide = geometria.centroid centroide_web = geometria_web.centroid geometria_simplificada = geometria.simplify( tolerance=0.0, preserve_topology=True) geometria_web_simplificada = geometria_web.simplify( tolerance=0.0, preserve_topology=True) if geometria.geom_type == 'Polygon': geometria = MultiPolygon(geometria, srid=4326) geometria_web = MultiPolygon(geometria_web, srid=3857) geometria_simplificada = MultiPolygon(geometria_simplificada, srid=4326) geometria_web_simplificada = MultiPolygon(geometria_web_simplificada, srid=3857) entidad, creado = Entidad.objects.get_or_create( clave=clave, descripcion=descripcion, defaults=dict( geometria=geometria, geometria_web=geometria_web, centroide=centroide, centroide_web=centroide_web, geometria_simplificada=geometria_simplificada, geometria_web_simplificada=geometria_web_simplificada)) if creado: print(f'entidad creado {entidad}')
def _handle_geom(self, value): """ Geometry processing (in place), depending on options """ if value is None: geometry = None elif isinstance(value, dict) and 'type' in value: geometry = value else: if isinstance(value, GEOSGeometry): geometry = value else: try: # this will handle string representations (e.g. ewkt, bwkt) geometry = GEOSGeometry(value) except ValueError: # if the geometry couldn't be parsed. # we can't generate valid geojson error_msg = 'The field ["%s", "%s"] could not be parsed as a valid geometry' % ( self.geometry_field, value) raise SerializationError(error_msg) # Optional force 2D if self.options.get('force2d'): wkb_w = WKBWriter() wkb_w.outdim = 2 geometry = GEOSGeometry(wkb_w.write(geometry), srid=geometry.srid) # Optional geometry simplification simplify = self.options.get('simplify') if simplify is not None: geometry = geometry.simplify(tolerance=simplify, preserve_topology=True) # Optional geometry reprojection if geometry.srid and geometry.srid != self.srid: geometry.transform(self.srid) # Optional bbox if self.options.get('bbox_auto'): self._current['bbox'] = geometry.extent self._current['geometry'] = geometry
def _handle_geom(self, value): """ Geometry processing (in place), depending on options """ if value is None: geometry = None elif isinstance(value, dict) and 'type' in value: geometry = value else: if isinstance(value, GEOSGeometry): geometry = value else: try: # this will handle string representations (e.g. ewkt, bwkt) geometry = GEOSGeometry(value) except ValueError: # if the geometry couldn't be parsed. # we can't generate valid geojson error_msg = 'The field ["%s", "%s"] could not be parsed as a valid geometry' % ( self.geometry_field, value ) raise SerializationError(error_msg) # Optional force 2D if self.options.get('force2d'): wkb_w = WKBWriter() wkb_w.outdim = 2 geometry = GEOSGeometry(wkb_w.write(geometry), srid=geometry.srid) # Optional geometry simplification simplify = self.options.get('simplify') if simplify is not None: geometry = geometry.simplify(tolerance=simplify, preserve_topology=True) # Optional geometry reprojection if geometry.srid and geometry.srid != self.srid: geometry.transform(self.srid) # Optional bbox if self.options.get('bbox_auto'): self._current['bbox'] = geometry.extent self._current['geometry'] = geometry
def search(request): ''' Takes GET params: q: search string bbox: bounding box string - if "false", will search for places without geos - if empty string / not specified, will search for places with and without geos, not constrained by bbox per_page: results per page int, default=100 page: page no (starting with 1) int, default=1 Returns: GeoJSON feed of search results Extra properties of feed: total: total number of results max_score: max score in results (?) page: page number ''' query = request.GET.get("q", "") if query == "": query = '*' #FIXME: remove feature_type from API as feature type queries are handled by query-string feature_type = request.GET.get("feature_type", None) if feature_type: if FeatureCode.objects.filter(typ=feature_type).count() > 0: query += ' feature_code:%s' % feature_type.upper() per_page = int(request.GET.get( "per_page", 100)) #FIXME: add error handling if int conversion fails page = int(request.GET.get("page", 1)) page_0 = page - 1 #model method requires page number to be zero-based whereas API accepts 1-based. bbox_string = request.GET.get("bbox", "") start_date = request.GET.get("start_date", None) end_date = request.GET.get("end_date", None) year_regex = re.compile(r'^[0-9]{4}') if start_date and year_regex.match(start_date): start_date += "-01-01" if end_date and year_regex.match(end_date): end_date += "-12-31" sort = request.GET.get("sort", None) VALID_SORTS = [ 'relevance', 'feature_code', 'start', 'end', 'name', 'uris', 'distance' ] if sort and sort not in VALID_SORTS: sort = None #Should we throw an error here? order = request.GET.get("order", None) if order and order not in ['asc', 'desc']: order = None if bbox_string and bbox_string != "false": bbox = [float(b) for b in bbox_string.split(",")] elif bbox_string == 'false': bbox = False else: bbox = None result = Place.objects.search(query, bbox=bbox, start_date=start_date, end_date=end_date, per_page=per_page, page=page_0, sort=sort, order=order) total = result['total'] pages = int(math.ceil(total / (per_page + .0))) #get total number of pages simplify = request.GET.get("simplify", False) if simplify and total >= 1: if bbox: degrees = bbox[2] - bbox[0] map_width = 700 #(roughly) tolerance = (degrees / map_width) / 2 else: tolerance = 0.01 for place in result["places"]: if place.geometry and (place.geometry["type"] == "Polygon" or place.geometry["type"] == "MultiPolygon"): coords = [ item for sublist in place.geometry["coordinates"] for item in sublist ] if len( coords ) > 200: #only simplify the larger polygons. TODO - determine best number for this try: geos_geom = GEOSGeometry(json.dumps(place.geometry)) if geos_geom.valid: simplified_geom = geos_geom.simplify( tolerance, preserve_topology=True) place.geometry = json.loads(simplified_geom.json) else: #invalid geometry so we will nullify it - we could try to fix this though place.geometry = {} except OGRException: place.geometry = {} format = request.GET.get("format", "geojson") if format == "csv": from django.http import HttpResponse import csv response = HttpResponse(content_type='text/csv') response[ 'Content-Disposition'] = 'attachment; filename="gazetteer_results.csv"' dictwriter = csv.DictWriter(response, delimiter='\t', fieldnames=Place.CSV_FIELDNAMES) dictwriter.writer.writerow(dictwriter.fieldnames) for p in result['places']: dictwriter.writerow(p.to_csv_dict()) return response ret = { 'type': 'FeatureCollection', 'features': [p.to_geojson() for p in result['places']], 'total': total, 'page': result['page'], 'pages': pages, 'per_page': result['per_page'], 'max_score': result['max_score'] } return render_to_json_response(ret)
def search(request): ''' Takes GET params: q: search string bbox: bounding box string - if "false", will search for places without geos - if empty string / not specified, will search for places with and without geos, not constrained by bbox per_page: results per page int, default=100 page: page no (starting with 1) int, default=1 Returns: GeoJSON feed of search results Extra properties of feed: total: total number of results max_score: max score in results (?) page: page number ''' query = request.GET.get("q", "") if query == "": query = '*' #FIXME: remove feature_type from API as feature type queries are handled by query-string feature_type = request.GET.get("feature_type", None) if feature_type: if FeatureCode.objects.filter(typ=feature_type).count() > 0: query += ' feature_code:%s' % feature_type.upper() per_page = int(request.GET.get("per_page", 100)) #FIXME: add error handling if int conversion fails page = int(request.GET.get("page", 1)) page_0 = page - 1 #model method requires page number to be zero-based whereas API accepts 1-based. bbox_string = request.GET.get("bbox", "") start_date = request.GET.get("start_date", None) end_date = request.GET.get("end_date", None) year_regex = re.compile(r'^[0-9]{4}') if start_date and year_regex.match(start_date): start_date += "-01-01" if end_date and year_regex.match(end_date): end_date += "-12-31" sort = request.GET.get("sort", None) VALID_SORTS = ['relevance', 'feature_code', 'start', 'end', 'name', 'uris', 'distance'] if sort and sort not in VALID_SORTS: sort = None #Should we throw an error here? order = request.GET.get("order", None) if order and order not in ['asc', 'desc']: order = None if bbox_string and bbox_string != "false": bbox = [float(b) for b in bbox_string.split(",")] elif bbox_string == 'false': bbox = False else: bbox = None result = Place.objects.search(query, bbox=bbox, start_date=start_date, end_date=end_date, per_page=per_page, page=page_0, sort=sort, order=order) total = result['total'] pages = int(math.ceil(total / (per_page + .0))) #get total number of pages simplify = request.GET.get("simplify", False) if simplify and total >= 1: if bbox: degrees = bbox[2] - bbox[0] map_width = 700 #(roughly) tolerance = (degrees / map_width) / 2 else: tolerance = 0.01 for place in result["places"]: if place.geometry and (place.geometry["type"] == "Polygon" or place.geometry["type"] == "MultiPolygon"): coords = [item for sublist in place.geometry["coordinates"] for item in sublist] if len(coords) > 200: #only simplify the larger polygons. TODO - determine best number for this try: geos_geom = GEOSGeometry(json.dumps(place.geometry)) if geos_geom.valid: simplified_geom = geos_geom.simplify(tolerance, preserve_topology=True) place.geometry = json.loads(simplified_geom.json) else: #invalid geometry so we will nullify it - we could try to fix this though place.geometry = {} except OGRException: place.geometry = {} format = request.GET.get("format", "geojson") if format == "csv": from django.http import HttpResponse import csv response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; filename="gazetteer_results.csv"' dictwriter = csv.DictWriter(response, delimiter='\t', fieldnames=Place.CSV_FIELDNAMES) dictwriter.writer.writerow(dictwriter.fieldnames) for p in result['places']: dictwriter.writerow(p.to_csv_dict()) return response ret = { 'type': 'FeatureCollection', 'features': [p.to_geojson() for p in result['places']], 'total': total, 'page': result['page'], 'pages': pages, 'per_page': result['per_page'], 'max_score': result['max_score'] } return render_to_json_response(ret)
def insereposicao(request): if request.is_ajax(): if request.method == 'POST': dicionario = dict(request.POST) id_viagem = dicionario['id_vg'][0] nome_linha = dicionario['nome_linha'][0] print nome_linha ponto = Point(float(dicionario['ponto[]'][0]), float(dicionario['ponto[]'][1])) obj_viagem = Viagens.objects.get(id_viagem=id_viagem) print ponto obj_linha = Linhas.objects.get(nome_linha=nome_linha) obj = Posicao(id_viagem=obj_viagem, geom=ponto, nome_linha=obj_linha) obj.save() """objpa = Posicao_atualII(id_pa = 'atual', id_viagem = obj_viagem, nome_linha = obj_linha, geom = ponto) objpa.save() objpa = Posicao_atualIII(id_pa = 'atual', id_viagem = obj_viagem, nome_linha = obj_linha, geom = ponto) objpa.save() objpa = Posicao_atualIV(id_pa = 'atual', id_viagem = obj_viagem, nome_linha = obj_linha, geom = ponto) objpa.save() objpa = Posicao_atualFerias(id_pa = 'atual', id_viagem = obj_viagem, nome_linha = obj_linha, geom = ponto) objpa.save()""" if nome_linha == 'INTERCAMPI I': Posicao_atual.objects.filter(id_pa='atual').update( id_viagem=obj_viagem, nome_linha=obj_linha, geom=ponto) elif nome_linha == 'INTERCAMPI II': Posicao_atualII.objects.filter(id_pa='atual').update( id_viagem=obj_viagem, nome_linha=obj_linha, geom=ponto) elif nome_linha == 'INTERCAMPI III': Posicao_atualIII.objects.filter(id_pa='atual').update( id_viagem=obj_viagem, nome_linha=obj_linha, geom=ponto) elif nome_linha == 'INTERCAMPI IV': Posicao_atualIV.objects.filter(id_pa='atual').update( id_viagem=obj_viagem, nome_linha=obj_linha, geom=ponto) elif nome_linha == 'INTERCAMPI EXTRA': Posicao_atualExtra.objects.filter(id_pa='atual').update( id_viagem=obj_viagem, nome_linha=obj_linha, geom=ponto) else: Posicao_atualFerias.objects.filter(id_pa='atual').update( id_viagem=obj_viagem, nome_linha=obj_linha, geom=ponto) todosospontos = Posicao.objects.filter( nome_linha=obj_linha).order_by('id_posicao') """a = Posicao.objects.filter(nome_linha = obj_linha).order_by('-id_posicao') Posicao_atual_Linha.Entry.objects.all()[:3] print a""" ordenados = Posicao.objects.filter( nome_linha=obj_linha).order_by('-id_posicao').all()[:3] print ordenados ordem = [] for o in ordenados: ordem.append(o.geom) print ordem pontos = [] for p in todosospontos: pontos.append(p.geom) if len(pontos) >= 2: linestring = LineString(pontos) linhaPosicaoAtual = LineString(ordem) print linestring print linhaPosicaoAtual ExcluirAnteriores(obj_linha) GEOSGeometry.simplify(linestring, tolerance=0.0) objRA = Rota_atual(id_viagem=obj_viagem, nome_linha=obj_linha, geom=linestring) objRA.save() """objpa = Posicao_atual_Linha(id_pa = 'atual', id_viagem = obj_viagem, nome_linha = obj_linha, geom = linestring) objpa.save()""" Posicao_atual_Linha.objects.filter(id_pa='atual').update( id_viagem=obj_viagem, nome_linha=obj_linha, geom=linhaPosicaoAtual) print len(pontos) resposta = '{result:ok}' return JsonResponse(json.dumps(resposta), safe=False)