zoukankan      html  css  js  c++  java
  • wkt转换geojson

    应用配置

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    	<modelVersion>4.0.0</modelVersion>
    
    	<groupId>com.lilei.geojson</groupId>
    	<artifactId>wkt2geojson</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<packaging>jar</packaging>
    
    	<name>wkt2geojson</name>
    	<url>http://maven.apache.org</url>
    
    	<properties>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    	</properties>
    
    	<dependencies>
    		<dependency>
    			<groupId>junit</groupId>
    			<artifactId>junit</artifactId>
    			<version>3.8.1</version>
    			<scope>test</scope>
    		</dependency>
    
    		<!-- https://mvnrepository.com/artifact/org.geotools/gt-geojson -->
    		<dependency>
    			<groupId>org.geotools</groupId>
    			<artifactId>gt-geojson</artifactId>
    			<version>12.0</version>
    		</dependency>
    
    
    
    	</dependencies>
    
    
    	<repositories>
    		<repository>
    			<id>nexus2</id>
    			<name>internal nexus repository2</name>
    			<url>http://central.maven.org/maven2/</url>
    		</repository>
    
    		<repository>
    			<id>maven2-repository.dev.java.net</id>
    			<name>Java.net repository</name>
    			<url>http://download.java.net/maven/2</url>
    		</repository>
    		<repository>
    			<id>osgeo</id>
    			<name>Open Source Geospatial Foundation Repository</name>
    			<url>http://download.osgeo.org/webdav/geotools/</url>
    		</repository>
    		<repository>
    			<snapshots>
    				<enabled>true</enabled>
    			</snapshots>
    			<id>boundless</id>
    			<name>Boundless Maven Repository</name>
    			<url>http://repo.boundlessgeo.com/main</url>
    		</repository>
    
    
    
    	</repositories>
    
    
    </project>
    

      

    程序

    package com.lilei.geojson.wkt2geojson;
    
    import java.io.IOException;
    import java.io.Reader;
    import java.io.StringReader;
    import java.io.StringWriter;
    import java.util.HashMap;
    import java.util.Map;
    
    import org.geotools.geojson.geom.GeometryJSON;
    import org.json.simple.JSONObject;
    
    import com.vividsolutions.jts.geom.Geometry;
    import com.vividsolutions.jts.io.WKTReader;
    
    /**
     * Hello world!
     *
     */
    public class App {
    	public static void main(String[] args) {
    		
    		String wkt = "POINT(114.298456 30.553544)";
    		
    		Map<String,Object> map = new HashMap<String,Object>();
    		
    		map.put("name", "北清路");
    		
    		map.put("author","李雷");
    		
    		String json = geoToJson(wkt,map);
    		
    		System.out.println(jsonToWkt(json));
    		
    	}
    
    	//
    	public static String geoToJson(String wkt,Map<String,Object> map) {
    		String json = null;
    		try {
    			WKTReader reader = new WKTReader();
    			Geometry geometry = reader.read(wkt);
    			StringWriter writer = new StringWriter();
    			GeometryJSON g = new GeometryJSON();
    			g.write(geometry, writer);
    			
    			map.put("geometry", writer);
    			
    			json = JSONObject.toJSONString(map);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		return json;
    	}
    
    	public static String jsonToWkt(String geoJson) {
    		String wkt = null;
    		GeometryJSON gjson = new GeometryJSON();
    		Reader reader = new StringReader(geoJson);
    		try {
    			Geometry geometry = gjson.read(reader);
    			
    			wkt = geometry.toText();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		return wkt;
    	}
    
    }
    

      

  • 相关阅读:
    ElasticSearch——分页查询
    Canal——写入到ES中数据错乱
    HBase管理与监控——WebUI
    '$.browser.msie' 为空或不是对象
    input file 获取不到Request.Files 解决办法
    .NET读取服务器或本地文件
    .NET将服务器文件导出
    .Net 中HashTable,HashMap 和 Dictionary<key,value> 和List<T>和DataTable的比较
    JavaScript跨域调用基于JSON的RESTful API
    WCF项目问题2-无法激活服务,因为它需要 ASP.NET 兼容性。没有未此应用程序启用 ASP.NET 兼容性。请在 web.config 中启用 ASP.NET 兼容性,或将 AspNetCompatibilityRequirementsAttribute.AspNetCompatibilityRequirementsMode 属性设置为 Required 以外的值。
  • 原文地址:https://www.cnblogs.com/lilei2blog/p/7813384.html
Copyright © 2011-2022 走看看