利用geotools-v10.3获得指定经纬度的域的值

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.filter.text.cql2.CQLException;
import org.geotools.filter.text.ecql.ECQL;
import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.filter.Filter;

/**
 * @author zhangdapeng
 * @version 1.0,2013年12月23日
 * @since 1.0
 */
public class QueryShpField {
	private ShapefileDataStore shpDataStore = null;
	private SimpleFeatureSource source=null;

	/**
	 * 
	 */
	public QueryShpField() {
	}

	public SimpleFeatureSource getShpStore(String path) throws IOException, CQLException {
		//long time1 = System.currentTimeMillis();
		// shp文件路径
		File file = new File(path);
		ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
		Map<String, Serializable> params = new HashMap<String, Serializable>();
		params.put("url", file.toURI().toURL());
		params.put("cache and reuse memory maps", Boolean.TRUE);
		params.put("memory mapped buffer", Boolean.TRUE);

		shpDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);

		shpDataStore.setCharset(Charset.forName("GBK"));
		//System.out.println(System.currentTimeMillis() - time1 + "读取shp文件时间");
		// 文件名称

		String typeName = shpDataStore.getTypeNames()[0];

		this.source = shpDataStore.getFeatureSource(typeName);
		return source;
	}

	/**
	 * 返回field的值,为空返回为空。
	 * @param longitude
	 * @param latitude
	 * @param field
	 * @return
	 * @throws IOException
	 * @throws CQLException
	 */
	public String queryField(String longitude, String latitude, String field) throws IOException, CQLException {
		//long time1 = System.currentTimeMillis();
		Filter filter = ECQL.toFilter("INTERSECTS(the_geom, POINT(" + longitude + " " + latitude + "))");
		SimpleFeatureCollection features = source.getFeatures(filter);
		SimpleFeatureIterator iterator = features.features();
		try {
			while (iterator.hasNext()) {
				SimpleFeature feature = iterator.next();
				Collection<Property> p = feature.getProperties();
				for (Iterator<Property> it = p.iterator(); it.hasNext();) {
					Property pro = it.next();
					if (pro.getName().toString().toLowerCase().equals(field.toLowerCase())) {
						return pro.getValue().toString();
					}
				}
			}
		} finally {
			iterator.close();
		}

		//System.out.println(System.currentTimeMillis() - time1 + "读取shp文件时间");
		return "";
	}

	

}

转载自:https://blog.csdn.net/philosophyatmath/article/details/37927517

You may also like...

退出移动版