Azure Maps Spatial APIs V1 & 2022-08-01 Retirement - alternative?

red-bread 0 Reputation points
2024-09-29T10:33:39.6533333+00:00

Hi,

It appears the Spatial V1 API is now deprecated and to be entirely deleted by next year. I would like to move to the new version while there's plenty of time. But there is no information about which alternative version to use from the announcement page here: https://azure.microsoft.com/en-us/updates/v2/Azure-Maps-Spatial-APIs-V1-2022-08-01

Which alternate version of the API can I use?

Thanks so much!

Azure Maps
Azure Maps
An Azure service that provides geospatial APIs to add maps, spatial analytics, and mobility solutions to apps.
716 questions
{count} votes

1 answer

Sort by: Most helpful
  1. rbrundritt 17,981 Reputation points Microsoft Employee
    2024-09-30T16:11:53.8466667+00:00

    By geofence I'm assuming polygon, so you would be looking for a point in polygon algorithm. This can be done fairly quickly, even in JavaScript code.

    Turf.js is a fairly popular JavaScript library that does spatial calculations. With that you would most likely want to use the booleanPointInPolygon function: https://turfjs.org/docs/api/booleanPointInPolygon

    That said, the actual code for point in polygon test is fairly small and you could simply do the following without a library:

    function pointInPolygon(point, polygonRing) {
        let x = point[0], y = point[1];
        let inside = false;
        for (let i = 0, j = polygonRing.length - 1; i < polygonRing.length; j = i++) {
            let xi = polygonRing[i][0], yi = polygonRing[i][1];
            let xj = polygonRing[j][0], yj = polygonRing[j][1];
            
            let intersect = ((yi > y) !== (yj > y)) &&
                            (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
            if (intersect) inside = !inside;
        }
        return inside;
    }
    
    // Example usage:
    const polygon = [[1,1], [1,3], [3,3], [3,1]];
    const point = [2,2];
    console.log(pointInPolygon(point, polygon)); // true
    

    The above code tests if a GeoJSON Position is within a polygon ring. This can be expanded out to support polygons with holes, and multipolygons if needed as follows:

    //Takes in a GeoJSON Position and the coordinates of a Polygon object (number[][][]).
    function pointInPolygonWithHoles(point, polygonCoords) {
    	if(!polygonCoords || polygonCoords.length === 0) {
    		return false;
    	}
    	
    	//The first ring of a polygon should be the exterior ring and all other rings are holes. 
    	//So test that the point is in the first ring and not in the others.
    	
    	let isWithin = pointInPolygon(point, polygonCoords[0]);
    	
    	if(isWithin && polygonCoords.length > 1) {
    		//Check the holes.
    		for(let i = 1; i < polygonCoords.length; i++){
    			if(pointInPolygon(point, polygonCoords[i])){
    				//If we make it here, the point is in the hole and thus, not in the polyogn. We can fail quickly now and don't need to test the other polygons.
    				isWithin = false;
    				break;
    			}
    		}
    	}
    	
    	return isWithin;
    }
    
    //Takes in a GeoJSON Position and the coordinates of a MultiPolygon object (number[][][][]).
    function pointInMultiPolygon(point, multiPolygonCoords) {
    	if(!multiPolygonCoords || multiPolygonCoords.length === 0) {
    		return false;
    	}
    
    	//If the point is in any polygon, then it's successful. 
    	for(let i = 1; i < multiPolygonCoords.length; i++){
    		if(pointInPolygonWithHoles(point, multiPolygonCoords[i])){
    			return true;
    		}
    	}
    	
    	return false;
    }
    
    //Takes in a GeoJSON Position and any GeoJson Geometry object.
    function pointInGeometry(point, geometry){
    	if(geometry.type === 'Polygon'){
    		return pointInPolygonWithHoles(geometry.coordinates);
    	} else if(geometry.type === 'MultiPolygon'){
    		return pointInMultiPolygon(geometry.coordinates);	
    	}
    	
    	//Lines and Point types should fail.
    	return false;
    }
    

    If you are using a different programming language, there are many spatial math libraries available depending on your target language. For example, if using .NET, the NET topology suite is popular: https://github.com/NetTopologySuite/NetTopologySuite

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.