Error loading GeoJSON data

I created a geoportal using django and im trying to visualize imported data (type GeoJSON). The importation is successful as I can see the data in my database in postgreSQL. but when I try to visualize it I see nothing on the map and the only error that Im getting is this :

Error loading GeoJSON data: 'memoryview' object has no attribute 'read'
[30/Jun/2024 16:16:36,134] - Broken pipe from ('127.0.0.1', 56956)
Error loading GeoJSON data: 'memoryview' object has no attribute 'read'
[30/Jun/2024 16:16:36] "GET / HTTP/1.1" 200 26997
[30/Jun/2024 16:16:40] "GET /accounts/login/ HTTP/1.1" 200 19257
Not Found: /favicon.ico
[30/Jun/2024 16:16:40] "GET /favicon.ico HTTP/1.1" 404 2711
[30/Jun/2024 16:16:43] "POST /accounts/login/ HTTP/1.1" 302 0
[30/Jun/2024 16:16:43] "GET /upload_vector_data/ HTTP/1.1" 200 18668
[30/Jun/2024 16:16:50] "POST /upload_vector_data/ HTTP/1.1" 302 0
[30/Jun/2024 16:16:50] "GET /accounts/success/Upload%20successful.%20Name:%20test,%20File:%20test1.geojson/ HTTP/1.1" 200 1873
Error loading GeoJSON data: 'memoryview' object has no attribute 'read'
[30/Jun/2024 16:16:54] "GET / HTTP/1.1" 200 26997

the files related to this matter:

  • views.py:
def upload_vector_data(request):
    if request.method == 'POST':
        name = request.POST.get('name')
        vector_file = request.FILES.get('vector_file')

        # Server-side validation for GeoJSON file
        if vector_file and vector_file.name.endswith('.geojson'):
            vector_file_instance = VectorData(name=name, vector_file=vector_file)
            vector_file_instance.save()
            messages.success(request, f'Upload successful. Name: {name}, File: {vector_file.name}')
            return redirect('success', message='Upload successful. Name: {}, File: {}'.format(name, vector_file.name))
        else:
            messages.error(request, 'Invalid file type. Only GeoJSON files are allowed.')
            return redirect('upload_vector_data')
    
    return render(request, 'registration/upload_vector_data.html')
def success(request, message):
    return render(request, 'registration/success.html', {'message': message})
  • models.py:
class VectorData(models.Model):
    name = models.CharField(max_length=255)
    vector_file = models.FileField(upload_to='uploads/')
    uploaded_at = models.DateTimeField(auto_now_add=True)
  • success.html:
<a href="{% url 'upload_vector_data' %}">Upload another file</a>
    <a class="link-secondary me-3" href="/">Visualize</a>


    <!-- Intégration du script de Leaflet -->
 <script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function () {
            var visualizeBtn = document.getElementById('visualize-btn');
            visualizeBtn.addEventListener('click', function () {
                var fileInput = document.getElementById('vector-file');
                var file = fileInput.files[0];
                
                if (file && file.name.endsWith('.geojson')) {
                    var reader = new FileReader();
                    reader.onload = function (e) {
                        // Effacez les anciennes couches GeoJSON s'il y en a
                        map.eachLayer(function (layer) {
                            if (layer instanceof L.GeoJSON) {
                                map.removeLayer(layer);
                            }
                        });

                        var geojsonLayer = L.geoJSON(JSON.parse(e.target.result)).addTo(map);
                        map.fitBounds(geojsonLayer.getBounds());
                    };
                    reader.readAsText(file);
                } else {
                    alert('Please select a GeoJSON file.');
                }
            });
        });
    </script> 
  • forms.py:
from django import forms
from .models import VectorData

class VectorDataForm(forms.ModelForm):
    class Meta:
        model = VectorData
        fields = ['name']

for the record am using Folium to fetch the map.

Welcome @EatableTea !

When requesting with an error here, please post the complete traceback with the error, along with the function where the error is occurring.

Also, please don’t post images of code or error messages here. Copy / paste the text of the code (or error message) into the body of your post, between lines of three backtick - ` characters. This means you’ll have a line of ```, then the text, then another line of ```. (This forces the forum software to keep that text properly formatted.) The lines of ``` must be lines by themselves.

I hope I modified it right !

1 Like

Really close - I took the liberty of doing a little clean-up on your post.

1 Like

I’m a little confused about what’s going on here.

Typically, the error messages get printed before the log message for the request being made, such that in a case such as this:

this error is usually caused as a result of the GET /, not the GET /accounts/... request that appears in the previous line.

What is the view being called for /?

I dont know , I tried fixing the problem and now I got another type of error:
Error loading GeoJSON data: 'memoryview' object has no attribute 'open' [30/Jun/2024 18:08:20] "GET / HTTP/1.1" 200 26006
the vector data is FieldField storage and yet Im still getting the error of memoryview