TransWikia.com

Query a region within a larger region with GeoDjango REST API

Geographic Information Systems Asked on July 2, 2021

I have a working Django REST API which can query all the counties in a country.

//My model
class kenya_counties(models.Model):
  objectid = models.BigIntegerField()
  id_field = models.BigIntegerField()
  county_nam = models.CharField(max_length=80)
  const_code = models.BigIntegerField()
  constituen = models.CharField(max_length=80)
  county_cod = models.BigIntegerField()
  shape_leng = models.FloatField()
  shape_area = models.FloatField()
  geom = models.MultiPolygonField(srid=4326)

  def __str__(self):
      return self.county_nam

//My Serializer
class CountiesSerializer(serializers.ModelSerializer):
  class Meta:
     model = kenya_counties
     fields = ('objectid', 'id_field', 'county_nam', 'const_code', 'constituen', 'county_nam', 'county_cod', 'shape_leng', 'shape_area')

//My view
@api_view(['GET', 'POST'])
def counties(request):
   if request.method == 'GET':
       data = kenya_counties.objects.all()

       serializer = CountiesSerializer(data, context={'request': request}, many=True)

       return Response(serializer.data)

   elif request.method == 'POST':
       serializer = CountiesSerializer(data=request.data)
       if serializer.is_valid():
           serializer.save()
           return Response(status=status.HTTP_201_CREATED)
        
       return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

//My url
    re_path(r'^api/gis/counties/$', views.counties),

I get this.

How can I query just one county and display it on a react-leaflet map as shown below?

<MapContainer center={position} zoom={13} scrollWheelZoom={false}>
    <TileLayer
      attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
      url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
    />
</MapContainer>

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP