Change coordinates precision while serializing geodjano objects - geojson

I have a GeoDjango polygon model as below
class PolygonFeature(models.Model):
shapefile = models.ForeignKey(Shapefile,
on_delete=models.CASCADE, related_name='polygon_shp')
geom = models.PolygonField(srid=4326, blank=True, null=True)
def __str__(self):
return str(self.shapefile.filename) + "-" + str(self.pk)
I am adding polygon to model via LayerMapping in which out is the shapefile
mapping = {
'shapefile':{'id':'survey_pk'},
'geom': geom,
}
lm = LayerMapping(model, out, mapping, transform=transform, encoding='iso-8859-1')
lm.save(verbose=False)
I need to change coordinates precision while serializing polygon object geometry to GeoJSOn
feat = PolygonFeature.objects.filter(shapefile=obj)
json_obj = serialize('geojson', feat)
Current
{
"type": "FeatureCollection",
"crs": {
"type": "name",
"name": "EPSG:4326"
},
"features": [
{
"type": "Feature",
"properties": {
"shapefile": 53,
"pk": "45269"
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
75.6083423241,
31.315054275
],
[
75.5543398801,
31.30543373742
],
[
75.5970812345,
31.2980635331
],
[
75.6543504465,
31.30345354185
],
[
75.6035348241,
31.315035375
]
]
]
}
}
]
}

Related

convert grib to geojson

I want to convert a grib2 file to a geojson with the following format:
{
"type": "FeatureCollection",
"features": [
{ "type": "Feature", "properties": { "ID": 0, "sigwaveht": 1.000000 }, "geometry": { "type": "LineString", "coordinates": [ [ 20.5, 77.559374979743737 ], [ 20.756756711040964, 77.5 ], [ 21.0, 77.426829270065582 ], [ 21.5, 77.426829270065582 ] ] } },
{ "type": "Feature", "properties": { "ID": 1, "sigwaveht": 1.000000 }, "geometry": { "type": "LineString", "coordinates": [ [ 17.5, 76.879518074163784 ], [ 18.0, 76.840000001907356 ], [ 18.555555592348554, 77.0 ], [ 18.555555592348554, 77.5 ] ] } },
{ "type": "Feature", "properties": { "ID": 2, "sigwaveht": 1.000000 }, "geometry": { "type": "LineString", "coordinates": [ [ 28.5, 76.732142838136269 ], [ 29.0, 76.634146323734484 ], [ 29.937500058207661, 77.0 ], [ 29.937500058207661, 77.5 ] ] } },
I can accomplish this by using ogr2ogr2 to convert a shape file to a geojson in this format but what can I do to convert a grib2 to a geoJSON of this format?
You can't convert a GRIB, which is a raster format, to GeoJSON, which is a vector format.
What do you expect to achieve? Vector data composed of points where each point is one of the pixels of the raster format?
If this is what you want, you will probably have to code it yourself, I don't think there are any standard tools to do this. Just make a loop over the raster data pixels and write one point feature for every pixel.

How can I covert the dxf/dwg coordinate to geojson coordinate?

I'm a junior devloper.
I converted the CAD files(dxf and dwg) to GeoJSON. but the coordinates in geojson were not like latitude and longitude. The coordinates were very big numbers.
So How can I convert the scale from dwg/dxf to geojson?
Below are the codes.
converted file (.geojson)
{
"type": "FeatureCollection",
"name": "entities",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::3857" } },
"features": [
{ "type": "Feature", "properties": { "Layer": "FOR", "SubClasses": "AcDbEntity:AcDbText:AcDbText", "EntityHandle": "8F", "Text": "지상4층 평면도" }, "geometry": { "type": "Point", "coordinates": [ 355070.370331178826746, -551568.797326237778179, 0.0 ] } },
{ "type": "Feature", "properties": { "Layer": "FOR", "SubClasses": "AcDbEntity:AcDbText:AcDbText", "EntityHandle": "93", "Text": "SCALE = 1/200" }, "geometry": { "type": "Point", "coordinates": [ 360192.897985583578702, -552534.170035884599201, 0.0 ] } },
{ "type": "Feature", "properties": { "Layer": "0", "SubClasses": "AcDbEntity:AcDbLine", "EntityHandle": "94" }, "geometry": { "type": "LineString", "coordinates": [ [ 352255.966381403210107, -551834.165067565510981, 0.0 ], [ 352255.966381405014545, -553667.498400898533873, 0.0 ] ] } },
{ "type": "Feature", "properties": { "Layer": "0", "SubClasses": "AcDbEntity:AcDbLine", "EntityHandle": "95" }, "geometry": { "type": "LineString", "coordinates": [ [ 350422.633048069197685, -551834.165067565510981, 0.0 ], [ 365040.71385543467477, -551834.165067565510981, 0.0 ] ] } },
{ "type": "Feature", "properties": { "Layer": "0", "SubClasses": "AcDbEntity:AcDbCircle", "EntityHandle": "96" }, "geometry": { "type": "LineString", "coordinates": [ [ 353644.855270292086061, -551834.165067565510981, 0.0 ], [ 353642.951846340089105, -551906.853895680746064, 0.0 ], [ 353637.246791636920534, -551979.343488770537078, 0.0 ], [ 353627.755743340880144, -552051.435157899162732, 0.0 ], [ 353614.504715755698271, -552122.931304812431335, 0.0 ], [ 353597.530029026907869, -552193.635963541222736, 0.0 ], [ 353576.878209590911865, -552263.355337530723773, 0.0 ], [ 353552.605862649332266, -552331.898330822819844, 0.0 ], [ 353524.779517017945182, -552399.077071837498806, 0.0 ], [ 353493.475442775932606, -552464.70742831483949, 0.0 ], [ 353458.779442214930896, -552528.609512010007165, 0.0 ], [ 353420.786614660755731, -552590.608171753003262, 0.0 ],
....
dxf file
0
SECTION
2
HEADER
9
$ACADVER
1
AC1018
9
$ACADMAINTVER
70
0
9
$DWGCODEPAGE
3
ANSI_949
9
$LASTSAVEDBY
1
cloudconvert
9
$INSBASE
10
0.0
20
0.0
30
0.0
9
$EXTMIN
10
76682.88756262069
20
-557971.2818526919
30
-0.2959696236149014
9
$EXTMAX
10
566395.620293251
20
-506981.3117800786
30
0.2959696236149014
9
$LIMMIN
10
0.0
20
0.0
9
$LIMMAX
....

500 Internal Server Error when uploading GeoJSON to Azure Maps Data Service

I always receive 500 Internal Service Error
POST: https://atlas.microsoft.com/mapData/upload?subscription-key=&api-version=1.0&dataFormat=geojson
Sample data:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-122.13393688201903,
47.63829579223815
],
[
-122.13389128446579,
47.63782047131512
],
[
-122.13240802288054,
47.63783312249837
],
[
-122.13238388299942,
47.63829037035086
],
[
-122.13393688201903,
47.63829579223815
]
]
]
},
"properties": {
"geometryId": "1"
}
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-122.13374376296996,
47.63784758098976
],
[
-122.13277012109755,
47.63784577367854
],
[
-122.13314831256866,
47.6382813338708
],
[
-122.1334782242775,
47.63827591198201
],
[
-122.13374376296996,
47.63784758098976
]
]
]
},
"properties": {
"geometryId": "2",
"validityTime": {
"expiredTime": "2019-01-15T00:00:00",
"validityPeriod": [
{
"startTime": "2019-01-08T01:00:00",
"endTime": "2019-01-08T17:00:00",
"recurrenceType": "Daily",
"recurrenceFrequency": 1,
"businessDayOnly": true
}
]
}
}
}
]
}
Need help

d3.geoPath() returns Error: <path> attribute d: Expected number, "M,ZM,ZM,ZM,Z"

Edit: recreated the logic on jsfiddle https://jsfiddle.net/exLtcgrq/1/
I am trying to parse a simple GeoJSON file to D3 using the D3 V4 API.
My GeoJSON is simple:
{ "type": "FeatureCollection",
"features": [
{ "type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[10.0, 10.0], [60.0, 40.0], [50.0, 75.0],[20.0, 60.0]
]
},
"properties": {
"id": "1",
"Type": "campingspot"
}
},
{ "type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[20.0, 65.0], [50.0, 80.0], [50.0, 110.0],[20.0, 115.0]
]
},
"properties": {
"id": "1",
"Type": "campingspot"
}
}
]
}
I load thus using the d3.json() method and try using the d3-geo api to convert it to a path with this code:
var jsonData2 = d3.json("campingGeojson.json", function(error, json){
svg.selectAll("path")
.data(json.features)
.enter()
.append("path")
.attr("d", d3.geoPath())
.attr("stroke", "black")
.attr("stroke-width", 1)
.attr("fill", "green")
});
The console output on chrome tells me the following
Error: <path> attribute d: Expected number, "M,ZM,ZM,ZM,Z".
Any suggestions what is going wrong with using the geoPath method is highly appreciated.
Thank you.
Coordinates for geoJson polygons are an array of coordinate arrays (with the coordinates themselves being arrays). The first array indicates the shell, following arrays indicate holes.
So I think your geoJson should look more like:
"coordinates": [
[ [10.0, 10.0], [60.0, 40.0], [50.0, 75.0],[20.0, 60.0] ]
]

Show and Hide Features of layers openlayers 3

I am trying to hide Feature of a layer from jason where I define the feature by category. I tried Jonatas Walker method but my code is different not working http://jsfiddle.net/jonataswalker/z10de36z/ but my code is different so not working
Below is my json
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"name": "Missing Person",
"src": "resources/icon.png",
"category": "cat1"
},
"geometry": {
"type": "Point",
"coordinates": [-0.45755, 51.507222]
}
},
{
"type": "Feature",
"properties": {
"name": "Wanted",
"src": "resources/icon.png",
"category": "cat1"
},
"geometry": {
"type": "Point",
"coordinates": [-0.12755, 52.507222]
}
},
{
"type": "Feature",
"properties": {
"name": "Missing 1",
"src": "resources/Blue_pointer.png",
"category": "cat2"
},
"geometry": {
"type": "Point",
"coordinates": [-1.12755, 52.507222]
}
},
{
"type": "Feature",
"properties": {
"name": "Wanted 3",
"src": "resources/icon.png",
"category": "cat1"
},
"geometry": {
"type": "Point",
"coordinates": [-2.12755, 53.507222]
}
},
{
"type": "Feature",
"properties": {
"name": "Wanted 7",
"src": "resources/icon.png",
"category": "cat1"
},
"geometry": {
"type": "Point",
"coordinates": [-0.1287, 53.507222]
}
},
{
"type": "Feature",
"properties": {
"name": "Wanted 9",
"src": "resources/Blue_pointer.png",
"category": "cat2"
},
"geometry": {
"type": "Point",
"coordinates": [-3.12755, 50.907222]
}
},
{
"type": "Feature",
"properties": {
"name": "Missing 8",
"src": "resources/Blue_pointer.png",
"category": "cat2"
},
"geometry": {
"type": "Point",
"coordinates": [-3.12755, 51.907222]
}
},
{
"type": "Feature",
"properties": {
"name": "Missing 18",
"src": "resources/Blue_pointer.png",
"category": "cat2"
},
"geometry": {
"type": "Point",
"coordinates": [-4.12755, 51.907222]
}
}
]
}
Openlayer code
var styleFunction1 = function(feature) {
var styles = {
'Point': [
new ol.style.Style({
image: new ol.style.Icon({
src: feature.get('src'),
anchor: [0.5, 1]
})
})],
'LineString': [
new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'gray',
width: 5
})
})]
};
return styles[feature.getGeometry().getType()];
};
vector = new ol.layer.Vector({
source: new ol.source.Vector({
projection : 'EPSG:4326',
format: new ol.format.GeoJSON(),
url: 'resources/multipoint.geojson'
}),
style: styleFunction1
});
map = new ol.Map({
target: target,
layers: [bingMapsRoad,myPetrolPlan,vector],
view: new ol.View({
center: ol.proj.transform([-0.12755, 51.507222], 'EPSG:4326', 'EPSG:3857'),
loadTilesWhileAnimating: true,
loadTilesWhileInteracting: true,
zoom: 6
}),
controls: ol.control.defaults({ attribution: false }),
loadTilesWhileInteracting: true
});
To Hide I am trying something like
hideVectorLayer: function () {
var abc = ConnectWebMap;
var featureCount = vector.getSource().getFeatures();
var featureCat = feature.get('category');
console.log(featureCat);
featureCount.forEach(function(feature) {
if(feature){
if(featureCat == 'cat1'){
console.log('a');
}
}
});
}
You can remove from the VectorSource using removeFeature method
you can make another layer ( a temporary one ) where you copy the features from the desired category and display it while setting the visibility of your main layer to false:
var tmp=new ol.layer.vector({ source : new ol.source.Vector()});
hideVectorLayer: function () {
var abc = ConnectWebMap;
var featureCount = vector.getSource().getFeatures();
var featureCat = feature.get('category');
console.log(featureCat);
featureCount.forEach(function(feature) {
if(feature){
if(featureCat == 'cat1'){
tmp.getSource().getFeatures().push(feature);
}
}
});
tmp.setStyle(vector.getStyle()); // set the same style to the tmp layer
map.addLayer(tmp);// add it to the map
vector.setVisible(false); // set the vector layer visibility to false
}

Resources